Skip to content

Commit dbc032a

Browse files
committed
ws2812: add PIO support for RP2040/RP2350
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.
1 parent a514169 commit dbc032a

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
module tinygo.org/x/drivers
22

3-
43
go 1.22.1
54

65
toolchain go1.23.1
76

8-
97
require (
108
github.com/eclipse/paho.mqtt.golang v1.2.0
119
github.com/frankban/quicktest v1.10.2
1210
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
1311
github.com/orsinium-labs/tinymath v1.1.0
1412
github.com/soypat/natiu-mqtt v0.5.1
13+
github.com/tinygo-org/pio v0.3.0
1514
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d
1615
golang.org/x/net v0.33.0
1716
tinygo.org/x/tinyfont v0.3.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ github.com/orsinium-labs/tinymath v1.1.0 h1:KomdsyLHB7vE3f1nRAJF2dyf1m/gnM2HxfTe
1717
github.com/orsinium-labs/tinymath v1.1.0/go.mod h1:WPXX6ei3KSXG7JfA03a+ekCYaY9SWN4I+JRl2p6ck+A=
1818
github.com/soypat/natiu-mqtt v0.5.1 h1:rwaDmlvjzD2+3MCOjMZc4QEkDkNwDzbct2TJbpz+TPc=
1919
github.com/soypat/natiu-mqtt v0.5.1/go.mod h1:xEta+cwop9izVCW7xOx2W+ct9PRMqr0gNVkvBPnQTc4=
20+
github.com/tinygo-org/pio v0.3.0 h1:opEnOtw58KGB4RJD3/n/Rd0/djYGX3DeJiXLI6y/yDI=
21+
github.com/tinygo-org/pio v0.3.0/go.mod h1:wf6c6lKZp+pQOzKKcpzchmRuhiMc27ABRuo7KVnaMFU=
2022
github.com/valyala/fastjson v1.6.3/go.mod h1:CLCAqky6SMuOcxStkYQvblddUtoRxhYMGLrsQns1aXY=
2123
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d h1:0olWaB5pg3+oychR51GUVCEsGkeCU/2JxjBgIo4f3M0=
2224
golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c=

ws2812/ws2812.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// Package ws2812 implements a driver for WS2812 and SK6812 RGB LED strips.
2+
//
3+
// On most platforms NewWS2812 uses bit-banging.
4+
// On RP2040/RP2350 it uses PIO for hardware-timed control.
25
package ws2812 // import "tinygo.org/x/drivers/ws2812"
36

47
//go:generate go run gen-ws2812.go -arch=cortexm 16 48 64 120 125 150 168 200
@@ -24,14 +27,11 @@ func New(pin machine.Pin) Device {
2427
return NewWS2812(pin)
2528
}
2629

27-
// New returns a new WS2812(RGB) driver.
28-
// It does not touch the pin object: you have
29-
// 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.
3033
func NewWS2812(pin machine.Pin) Device {
31-
return Device{
32-
Pin: pin,
33-
writeColorFunc: writeColorsRGB,
34-
}
34+
return newWS2812Device(pin)
3535
}
3636

3737
// 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)