PHP code example of dept-of-scrapyard-robotics / pwm-rgb-fan

1. Go to this page and download the library: Download dept-of-scrapyard-robotics/pwm-rgb-fan library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?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

$fan->setSpeedWithColor(60.0, 0xFF00FF);  // 60% speed, purple

public function speedIndicator(): static

$fan->setSpeed(75.0);
$fan->speedIndicator();  // RGB shows red-ish color

public function temperatureVisual(float $temp, float $minTemp = 40.0, float $maxTemp = 80.0): static

$temp = $fan->getCPUTemp();
$fan->temperatureVisual($temp, 40.0, 80.0);  // Fan + RGB respond to temp

public function breathe(int $color = 0x00FFFF, int $cycles = 3, int $duration_ms = 2000, float $minSpeed = 30.0, float $maxSpeed = 80.0): static

$fan->breathe(0x00FFFF, 3, 2000, 30.0, 80.0);  // Cyan breathing

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);
}

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
);

// Breathing effect
$fan->breathe(0x00FFFF, 3, 2000);  // Cyan, 3 cycles

// Rainbow
$fan->rainbow(60.0, 2000, 2);  // 60% speed, 2 cycles

// Spinning rainbow
$fan->spinningRainbow(75.0, 5, 500);  // 5 rotations

// Party mode
$fan->party(5000);  // 5 seconds

// Shutdown
$fan->shutdown(3000);

$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