mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-20 06:28:59 +00:00
4071028e85
Integrate PIO support directly into the existing Device. NewWS2812() now uses PIO for hardware-timed control on RP2040/RP2350 and falls back to bit-banging on other platforms. No changes to the exported API surface.
34 lines
775 B
Go
34 lines
775 B
Go
//go:build rp2040 || rp2350
|
|
|
|
package ws2812
|
|
|
|
import (
|
|
"image/color"
|
|
"machine"
|
|
|
|
pio "github.com/tinygo-org/pio/rp2-pio"
|
|
"github.com/tinygo-org/pio/rp2-pio/piolib"
|
|
)
|
|
|
|
// newWS2812Device creates a WS2812 device using PIO for hardware-timed control.
|
|
// If PIO initialization fails, it falls back to the bit-bang driver.
|
|
func newWS2812Device(pin machine.Pin) Device {
|
|
sm, err := pio.PIO0.ClaimStateMachine()
|
|
if err != nil {
|
|
return Device{Pin: pin, writeColorFunc: writeColorsRGB}
|
|
}
|
|
ws, err := piolib.NewWS2812B(sm, pin)
|
|
if err != nil {
|
|
return Device{Pin: pin, writeColorFunc: writeColorsRGB}
|
|
}
|
|
return Device{
|
|
Pin: pin,
|
|
writeColorFunc: func(_ Device, buf []color.RGBA) error {
|
|
for _, c := range buf {
|
|
ws.PutRGB(c.R, c.G, c.B)
|
|
}
|
|
return nil
|
|
},
|
|
}
|
|
}
|