@@ -14,13 +14,34 @@ var errUnknownClockSpeed = errors.New("ws2812: unknown CPU clock speed")
14
14
15
15
// Device wraps a pin object for an easy driver interface.
16
16
type Device struct {
17
- Pin machine.Pin
17
+ Pin machine.Pin
18
+ writeColorFunc func (Device , []color.RGBA ) error
18
19
}
19
20
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 .
22
23
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
+ }
24
45
}
25
46
26
47
// Write the raw bitstring out using the WS2812 protocol.
@@ -32,12 +53,26 @@ func (d Device) Write(buf []byte) (n int, err error) {
32
53
}
33
54
34
55
// 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 ) {
37
71
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
41
76
}
42
- return nil
77
+ return
43
78
}
0 commit comments