1. Go to this page and download the library: Download phpdafruit/neopixel-php 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/ */
phpdafruit / neopixel-php example snippets
use PhpdaFruit\NeoPixels\PixelChannel;
use PhpdaFruit\NeoPixels\Enums\NeoPixelType;
use PhpdaFruit\NeoPixels\Enums\SPIDevice;
// Create a 30-LED strip on SPI device 0.0 with GRB color order
$strip = new PixelChannel(30, SPIDevice::SPI_0_0->value, NeoPixelType::GRB);
// Initialize the device
$strip->begin();
// Set all pixels to red
$strip->fill(0xFF0000)->show();
// Set individual pixel (pixel 10 to green)
$strip->setPixelColor(10, 0, 255, 0);
$strip->show();
// Control brightness (0-255)
$strip->setBrightness(128)->show();
// Clear all pixels
$strip->clear()->show();
use PhpdaFruit\NeoPixels\PixelBus;
use PhpdaFruit\NeoPixels\PixelChannel;
use PhpdaFruit\NeoPixels\Enums\SPIDevice;
use PhpdaFruit\NeoPixels\Enums\NeoPixelType;
// Create a bus with multiple channels
$bus = new PixelBus([
'front_strip' => new PixelChannel(30, SPIDevice::SPI_0_0->value, NeoPixelType::GRB),
'rear_strip' => new PixelChannel(30, SPIDevice::SPI_0_1->value, NeoPixelType::GRB),
'cpu_fan' => new PixelChannel(12, SPIDevice::SPI_1_0->value, NeoPixelType::RGB),
]);
// Initialize all devices
$bus->broadcast('begin');
// Fill all devices with red
$bus->fillAll(0xFF0000)->showAll();
// Control individual channels
$bus->useSource('front_strip', 'fill', [0x00FF00]);
$bus->useSource('cpu_fan', 'fill', [0x0000FF]);
$bus->showAll();
// Broadcast brightness to all channels
$bus->setBrightnessAll(128)->showAll();
// Clear all devices
$bus->clearAll()->showAll();
use PhpdaFruit\NeoPixels\DeviceShapes\SingleDiode;
use PhpdaFruit\NeoPixels\Enums\SPIDevice;
$led = new SingleDiode(SPIDevice::SPI_0_1->value);
// Basic control
$led->on(0xFF0000); // Turn on red
$led->off(); // Turn off
$led->setColor(0x00FF00); // Set color without updating
// Effects
$led->blink(0xFF0000, 3, 200); // Blink red 3 times, 200ms intervals
$led->pulse(0x0000FF, 2, 1500); // Blue breathing effect, 2 cycles
$led->strobe(0xFFFFFF, 10, 50); // Fast white strobe
// Semantic status indicators
$led->status('success'); // Green double-blink
$led->status('error'); // Red double-blink
$led->status('warning'); // Yellow double-blink
$led->status('info'); // Blue double-blink
use PhpdaFruit\NeoPixels\DeviceShapes\DoubleDots;
use PhpdaFruit\NeoPixels\Enums\SPIDevice;
use PhpdaFruit\NeoPixels\Enums\NeoPixelType;
$fan_lights = new DoubleDots(SPIDevice::SPI_1_0->value, NeoPixelType::GRB);
// Position control
$fan_lights->setLeft(0xFF0000); // Left LED red
$fan_lights->setRight(0x0000FF); // Right LED blue
$fan_lights->split(0xFF0000, 0x0000FF); // Different colors
$fan_lights->mirror(0x00FF00); // Both same green
// Animations
$fan_lights->alternate(0xFF00FF, 5); // Alternate between LEDs 5 times
$fan_lights->spin(0x00FFFF, 3, 500); // Simulate spinning effect
$fan_lights->bounce(0xFFFF00, 5); // Ping-pong effect
$fan_lights->crossfade(0xFF0000, 0x0000FF, 2, 2000); // Fade between colors
use PhpdaFruit\NeoPixels\DeviceShapes\RGBStrip;
use PhpdaFruit\NeoPixels\Enums\SPIDevice;
$strip = new RGBStrip(30, SPIDevice::SPI_0_0->value);
// Fill patterns
$strip->fillGradient(0xFF0000, 0x0000FF); // Red to blue gradient
$strip->rainbow(); // Static rainbow
// Directional effects
$strip->centerOut(0x00FF00, 1000); // Expand from center
$strip->edgesIn(0xFF00FF, 1000); // Collapse from edges
// Animated effects
$strip->wave(0x00FFFF, 3, 1000); // Cyan wave
$strip->sparkle(0xFFFFFF, 2000, 3); // White sparkles
$strip->theaterChase(0xFFFF00, 3, 10); // Yellow chase effect
$strip->rainbowCycle(5, 50); // Animated rainbow
$strip->fire(3000); // Fire simulation
$strip->comet(0x00FFFF, 3, 5); // Cyan comets with tail
use PhpdaFruit\NeoPixels\PixelChannel;
class CustomRing extends PixelChannel
{
public function __construct(string $devicePath = '/dev/spidev0.0')
{
// Define your LED count and type
parent::__construct(16, $devicePath, NeoPixelType::GRB);
}
public function spinClockwise(int $color = 0xFFFFFF, int $speed_ms = 50): static
{
for ($i = 0; $i < $this->getPixelCount(); $i++) {
$this->clear();
$this->setPixelColor($i, ($color >> 16) & 0xFF, ($color >> 8) & 0xFF, $color & 0xFF);
$this->show();
usleep($speed_ms * 1000);
}
return $this;
}
public function breatheSection(int $start, int $end, int $color, int $duration_ms = 2000): static
{
// Your custom animation logic
return $this;
}
}
use PhpdaFruit\NeoPixels\DeviceShapes\RGBStrip;
use PhpdaFruit\NeoPixels\Enums\Animation;
use PhpdaFruit\NeoPixels\AnimationRegistry;
// Initialize animation system
AnimationRegistry::initialize();
$strip = new RGBStrip(30, '/dev/spidev0.0');
$strip->begin();
// Play a built-in animation
$strip->animate(Animation::RAINBOW, 5000); // 5 seconds
$strip->animate(Animation::FIRE_FLICKER, 10000, [
'intensity' => 0.9,
'cooling' => 60
]);
use PhpdaFruit\NeoPixels\Contracts\Animations\Visualizations\AnimationVisualization;
use PhpdaFruit\NeoPixels\Enums\Animation;
use PhpdaFruit\NeoPixels\PixelChannel;
class MyCustomVisualization implements AnimationVisualization
{
public function run(PixelChannel $channel, int $duration_ms, array $options = []): void
{
// Your animation logic here
}
public function getAnimationType(): Animation
{
return Animation::CUSTOM;
}
public function getName(): string
{
return 'My Custom Animation';
}
public function getDefaultOptions(): array
{
return ['speed' => 100, 'color' => 0xFF0000];
}
public function isCompatible(PixelChannel $channel): bool
{
return true; // Or add specific
use PhpdaFruit\NeoPixels\AnimationFactory;
AnimationFactory::register(Animation::CUSTOM, MyCustomVisualization::class);
use PhpdaFruit\NeoPixels\Enums\NeoPixelType;
NeoPixelType::RGB // Red, Green, Blue (no white)
NeoPixelType::GRB // Green, Red, Blue (most common, e.g., WS2812B)
NeoPixelType::RGBW // Red, Green, Blue, White
NeoPixelType::GRBW // Green, Red, Blue, White
// Using the enum
$strip = new PixelChannel(30, SPIDevice::SPI_0_0->value, NeoPixelType::GRB);
// Update color order later
$strip->updateType(NeoPixelType::RGBW);
// With enum (recommended)
$strip = new PixelChannel(30, SPIDevice::SPI_0_0->value, NeoPixelType::GRB);
// Without enum (also valid)
$strip = new PixelChannel(30, '/dev/spidev0.0', NeoPixelType::GRB);
$strip->updateType(NeoPixelType::GRB); // Most common
$strip->updateType(NeoPixelType::RGB); // Alternative
bash
composer
tests/
├── Pest.php # Test configuration
├── Unit/
│ ├── PixelChannelTest.php # PixelChannel unit tests
│ └── PixelBusTest.php # PixelBus unit tests
└── Integration/
└── HardwareTest.php # Real hardware integration tests
bash
# Example 1: Basic single channel usage
php examples/01_single_channel_basic.php
# Example 2: Multi-channel control with PixelBus
php examples/02_pixel_bus_multi_channel.php
# Example 3: Advanced multi-device animations
php examples/03_advanced_animations.php
# Example 4: New convenience methods
php examples/04_new_convenience_methods.php
# Example 5: Device shapes demonstrations
php examples/05_device_shapes_demo.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.