Skip to content
php-baremetal REGISTRY
Publish a package

php-baremetal/ext-led

bm-ext MIT

Baremetal\Led\Device and the addressable-LED driver framework. Chips are packages registering into the led_drivers registry; the platform side is a ledstrip-hal package.

bus: led

ext-led · Recipe

Rainbow strip

A rotating HSV rainbow on a strip of WS2812 pixels: fill the buffer with hsv(), send it with show(), shift the hue each frame.

A 30-pixel WS2812 strip with its data line on GPIO 5 of an ESP32-S3-DevKitC-1, powered from its own 5 V supply with the ground shared with the board. Change the pin and PIXELS to match your strip.

json composer.json
{
  "name": "acme/rainbow-strip",
  "type": "project",
  "repositories": [
    { "type": "composer", "url": "https://repository.php-baremetal.com", "canonical": true }
  ],
  "require": {
    "php-baremetal/conductor": "^1.0",
    "php-baremetal/ext-sys": "^1.0",
    "php-baremetal/led-ws2812": "^1.0"
  },
  "config": {
    "allow-plugins": { "php-baremetal/conductor": true },
    "platform": { "php": "8.5.11" }
  },
  "extra": {
    "baremetal": {
      "sdk": "esp32@^2.0",
      "board": "espressif-esp32-s3-devkitc-1-n8r8",
      "runtime": "init-loop",
      "storage": "embedded"
    }
  }
}

The script

php project-src/index.php
<?php
use Baremetal\Led\Driver\Ws2812;

const PIN = 5;
const PIXELS = 30;
const BRIGHT = 48;         // 0 to 255

$strip = new Ws2812(PIN, PIXELS);   // GRB, the usual order for strips

function loop(int $tick): void
{
    global $strip;

    $shift = ($tick * 4) % 360;
    for ($i = 0; $i < PIXELS; $i++) {
        $strip->hsv($i, $shift + intdiv($i * 360, PIXELS), 255, BRIGHT);
    }
    $strip->show();
    delay(20);
}
  • Each frame writes all 30 pixels into the buffer, then sends them with one show().
  • Hue values above 359 wrap, so $shift + ... needs no modulo.
  • If the colours come out swapped (red shows green), the strip is in RGB order: pass Ws2812::RGB.
  • A 3.3 V data line usually drives a 5 V strip over a short wire; for long runs add a level shifter.

Related