Skip to content

Commit 544cd43

Browse files
gandarezclaude
andcommitted
ws2812: use PIO on RP2040/RP2350 transparently via build tags
Instead of adding a new Strip type, 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. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent e569fc6 commit 544cd43

8 files changed

Lines changed: 49 additions & 170 deletions

File tree

examples/ws2812-strip/main.go

Lines changed: 0 additions & 36 deletions
This file was deleted.

smoketest.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ tinygo build -size short -o ./build/test.bin -target=m5stamp-c3 ./examp
9393
tinygo build -size short -o ./build/test.hex -target=feather-nrf52840 ./examples/is31fl3731/main.go
9494
tinygo build -size short -o ./build/test.hex -target=arduino ./examples/ws2812
9595
tinygo build -size short -o ./build/test.hex -target=digispark ./examples/ws2812
96-
tinygo build -size short -o ./build/test.bin -target=pico2 ./examples/ws2812-strip
9796
tinygo build -size short -o ./build/test.hex -target=trinket-m0 ./examples/bme280/main.go
9897
tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/microphone/main.go
9998
tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/buzzer/main.go

ws2812/strip.go

Lines changed: 0 additions & 63 deletions
This file was deleted.

ws2812/strip_other.go

Lines changed: 0 additions & 20 deletions
This file was deleted.

ws2812/strip_rp2.go

Lines changed: 0 additions & 39 deletions
This file was deleted.

ws2812/ws2812.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// Package ws2812 implements a driver for WS2812 and SK6812 RGB LED strips.
22
//
3-
// For low-level control, use Device with NewWS2812 or NewSK6812.
4-
// For a higher-level API with pixel buffering and brightness control,
5-
// use Strip with NewStrip. On RP2040/RP2350, Strip uses PIO for
6-
// hardware-timed control; on other platforms it falls back to bit-banging.
3+
// On RP2040/RP2350, NewWS2812 uses PIO for hardware-timed control.
4+
// On other platforms it uses bit-banging.
75
package ws2812 // import "tinygo.org/x/drivers/ws2812"
86

97
//go:generate go run gen-ws2812.go -arch=cortexm 16 48 64 120 125 150 168 200
@@ -29,14 +27,11 @@ func New(pin machine.Pin) Device {
2927
return NewWS2812(pin)
3028
}
3129

32-
// New returns a new WS2812(RGB) driver.
33-
// It does not touch the pin object: you have
34-
// to configure it as an output pin before calling New.
30+
// NewWS2812 returns a new WS2812(RGB) driver.
31+
// On RP2040/RP2350, it uses PIO for hardware-timed control.
32+
// On other platforms, you must configure the pin as output before calling this.
3533
func NewWS2812(pin machine.Pin) Device {
36-
return Device{
37-
Pin: pin,
38-
writeColorFunc: writeColorsRGB,
39-
}
34+
return newWS2812Device(pin)
4035
}
4136

4237
// New returns a new SK6812(RGBA) driver.

ws2812/ws2812_init_other.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//go:build !rp2040 && !rp2350
2+
3+
package ws2812
4+
5+
import "machine"
6+
7+
// newWS2812Device creates a WS2812 device using the bit-bang driver.
8+
func newWS2812Device(pin machine.Pin) Device {
9+
return Device{Pin: pin, writeColorFunc: writeColorsRGB}
10+
}

ws2812/ws2812_rp2_pio.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//go:build rp2040 || rp2350
2+
3+
package ws2812
4+
5+
import (
6+
"image/color"
7+
"machine"
8+
9+
pio "github.com/tinygo-org/pio/rp2-pio"
10+
"github.com/tinygo-org/pio/rp2-pio/piolib"
11+
)
12+
13+
// newWS2812Device creates a WS2812 device using PIO for hardware-timed control.
14+
// If PIO initialization fails, it falls back to the bit-bang driver.
15+
func newWS2812Device(pin machine.Pin) Device {
16+
sm, err := pio.PIO0.ClaimStateMachine()
17+
if err != nil {
18+
return Device{Pin: pin, writeColorFunc: writeColorsRGB}
19+
}
20+
ws, err := piolib.NewWS2812B(sm, pin)
21+
if err != nil {
22+
return Device{Pin: pin, writeColorFunc: writeColorsRGB}
23+
}
24+
return Device{
25+
Pin: pin,
26+
writeColorFunc: func(_ Device, buf []color.RGBA) error {
27+
for _, c := range buf {
28+
ws.PutRGB(c.R, c.G, c.B)
29+
}
30+
return nil
31+
},
32+
}
33+
}

0 commit comments

Comments
 (0)