Skip to content

Commit 3b183ad

Browse files
authored
added support for SK6812 to WS2812 device (#610)
sk6812: added support for SK6812 to WS2812 device
1 parent 68da68c commit 3b183ad

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

examples/ws2812/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func main() {
1919

2020
neo.Configure(machine.PinConfig{Mode: machine.PinOutput})
2121

22-
ws := ws2812.New(neo)
22+
ws := ws2812.NewWS2812(neo)
2323
rg := false
2424

2525
for {

ws2812/ws2812.go

+45-10
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,34 @@ var errUnknownClockSpeed = errors.New("ws2812: unknown CPU clock speed")
1414

1515
// Device wraps a pin object for an easy driver interface.
1616
type Device struct {
17-
Pin machine.Pin
17+
Pin machine.Pin
18+
writeColorFunc func(Device, []color.RGBA) error
1819
}
1920

20-
// New returns a new WS2812 driver. It does not touch the pin object: you have
21-
// to configure it as an output pin before calling New.
21+
// deprecated, use NewWS2812 or NewSK6812 depending on which device you want.
22+
// calls NewWS2812() to avoid breaking everyone's existing code.
2223
func New(pin machine.Pin) Device {
23-
return Device{pin}
24+
return NewWS2812(pin)
25+
}
26+
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+
func NewWS2812(pin machine.Pin) Device {
31+
return Device{
32+
Pin: pin,
33+
writeColorFunc: writeColorsRGB,
34+
}
35+
}
36+
37+
// New returns a new SK6812(RGBA) driver.
38+
// It does not touch the pin object: you have
39+
// to configure it as an output pin before calling New.
40+
func NewSK6812(pin machine.Pin) Device {
41+
return Device{
42+
Pin: pin,
43+
writeColorFunc: writeColorsRGBA,
44+
}
2445
}
2546

2647
// Write the raw bitstring out using the WS2812 protocol.
@@ -32,12 +53,26 @@ func (d Device) Write(buf []byte) (n int, err error) {
3253
}
3354

3455
// Write the given color slice out using the WS2812 protocol.
35-
// Colors are sent out in the usual GRB format.
36-
func (d Device) WriteColors(buf []color.RGBA) error {
56+
// Colors are sent out in the usual GRB(A) format.
57+
func (d Device) WriteColors(buf []color.RGBA) (err error) {
58+
return d.writeColorFunc(d, buf)
59+
}
60+
61+
func writeColorsRGB(d Device, buf []color.RGBA) (err error) {
62+
for _, color := range buf {
63+
d.WriteByte(color.G) // green
64+
d.WriteByte(color.R) // red
65+
err = d.WriteByte(color.B) // blue
66+
}
67+
return
68+
}
69+
70+
func writeColorsRGBA(d Device, buf []color.RGBA) (err error) {
3771
for _, color := range buf {
38-
d.WriteByte(color.G) // green
39-
d.WriteByte(color.R) // red
40-
d.WriteByte(color.B) // blue
72+
d.WriteByte(color.G) // green
73+
d.WriteByte(color.R) // red
74+
d.WriteByte(color.B) // blue
75+
err = d.WriteByte(color.A) // alpha
4176
}
42-
return nil
77+
return
4378
}

0 commit comments

Comments
 (0)