<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dept-of-scrapyard-robotics / pwm-rgb-fan example snippets
use DeptOfScrapyardRobotics\PWM\Fans\RGB\RGBPWMFan;
use PhpdaFruit\NeoPixels\Enums\NeoPixelType;
// Create RGB fan controller
$fan = new RGBPWMFan(
'/sys/class/pwm/pwmchip3/pwm0', // PWM device
'/dev/spidev1.0', // SPI device for RGB
NeoPixelType::GRB, // LED color order
2 // Number of LEDs
);
// Set speed and color together
$fan->setSpeedWithColor(50.0, 0x00FF00); // 50% speed, green
// Color indicates speed automatically
$fan->setSpeed(75.0);
$fan->speedIndicator(); // Red-ish color for high speed
// Turn off both fan and RGB
$fan->shutdown();
public function __construct(
string $pwm_path,
string $spi_device_path,
NeoPixelType $pixel_type = NeoPixelType::RGB,
int $no_lights = 2,
int $frequency = 1000
)
$fan = new RGBPWMFan(
'/sys/class/pwm/pwmchip3/pwm0',
'/dev/spidev1.0',
NeoPixelType::GRB,
2,
1000
);
public function setSpeedWithColor(float $speed, int $color): static
public function rainbow(float $speed = 50.0, int $duration_ms = 2000, int $cycles = 3): static
$fan->rainbow(75.0, 2000, 3); // Rainbow at 75% speed
public function spinningRainbow(float $speed = 75.0, int $rotations = 5, int $speed_ms = 500): static
$fan->spinningRainbow(75.0, 5, 500);
public function party(int $duration_ms = 10000): static
$fan->party(10000); // 10 seconds of party mode
public function shutdown(int $duration_ms = 2000): static
$fan->shutdown(3000); // 3-second fade to off
public function getRGBChannel(): PixelChannel|DoubleDots
// Access DoubleDots methods
$fan->getRGBChannel()->split(0xFF0000, 0x0000FF)->show(); // Red/Blue
$fan->getRGBChannel()->alternate(0x00FF00, 5); // Green flashing
$fan->getRGBChannel()->crossfade(0xFF00FF, 0x00FFFF); // Color morph
// Access PixelChannel methods
$fan->getRGBChannel()->fill(0xFFFFFF)->show(); // White
$fan->getRGBChannel()->rotate(1)->show(); // Rotate pixels
use DeptOfScrapyardRobotics\PWM\Fans\RGB\RGBPWMFan;
use PhpdaFruit\NeoPixels\Enums\NeoPixelType;
$fan = new RGBPWMFan(
'/sys/class/pwm/pwmchip3/pwm0',
'/dev/spidev1.0',
NeoPixelType::GRB,
2
);
// Set speed with color
$fan->setSpeedWithColor(50.0, 0x0000FF); // 50%, blue
sleep(2);
// Use speed indicator
$fan->setSpeed(75.0);
$fan->speedIndicator(); // Automatic color
sleep(2);
// Graceful shutdown
$fan->shutdown();
use DeptOfScrapyardRobotics\PWM\Fans\RGB\RGBPWMFan;
use PhpdaFruit\NeoPixels\Enums\NeoPixelType;
$fan = new RGBPWMFan(
'/sys/class/pwm/pwmchip3/pwm0',
'/dev/spidev1.0',
NeoPixelType::GRB,
2
);
// Monitor temperature with visual feedback
while (true) {
$temp = $fan->getCPUTemp();
$fan->temperatureVisual($temp, 40.0, 80.0);
echo "Temp: {$temp}°C, Fan: {$fan->getSpeed()}%\n";
sleep(2);
}
$fan = new RGBPWMFan(
'/sys/class/pwm/pwmchip3/pwm0',
'/dev/spidev1.0',
NeoPixelType::GRB,
2
);
// Set fan speed separately
$fan->setSpeed(60.0);
// Use DoubleDots methods directly
$rgb = $fan->getRGBChannel();
$rgb->split(0xFF0000, 0x0000FF)->show(); // Red left, blue right
sleep(2);
$rgb->alternate(0x00FF00, 5); // Green alternating
sleep(2);
$rgb->crossfade(0xFF00FF, 0x00FFFF, 2, 2000); // Purple to cyan
$fan = new RGBPWMFan($pwm, $spi, NeoPixelType::GRB, 2); // Try GRB
$fan = new RGBPWMFan($pwm, $spi, NeoPixelType::RGB, 2); // Or RGB
bash
# Example 1: Basic combined control
sudo php examples/01_basic_combined.php
# Example 2: Temperature-based RGB control
sudo php examples/02_temperature_rgb.php
# Example 3: Advanced RGB effects
sudo php examples/03_fan_effects.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.