@@ -27,44 +27,46 @@ var startFrame = []byte{0x00, 0x00, 0x00, 0x00}
27
27
type Device struct {
28
28
bus drivers.SPI
29
29
Order int
30
+ buf [4 ]byte
30
31
}
31
32
32
33
// New returns a new APA102 driver. Pass in a fully configured SPI bus.
33
- func New (b drivers.SPI ) Device {
34
- return Device {bus : b , Order : BGR }
34
+ func New (b drivers.SPI ) * Device {
35
+ return & Device {bus : b , Order : BGR }
35
36
}
36
37
37
38
// NewSoftwareSPI returns a new APA102 driver that will use a software based
38
39
// implementation of the SPI protocol.
39
- func NewSoftwareSPI (sckPin , sdoPin machine.Pin , delay uint32 ) Device {
40
+ func NewSoftwareSPI (sckPin , sdoPin machine.Pin , delay uint32 ) * Device {
40
41
return New (& bbSPI {SCK : sckPin , SDO : sdoPin , Delay : delay })
41
42
}
42
43
43
44
// WriteColors writes the given RGBA color slice out using the APA102 protocol.
44
45
// The A value (Alpha channel) is used for brightness, set to 0xff (255) for maximum.
45
- func (d Device ) WriteColors (cs []color.RGBA ) (n int , err error ) {
46
+ func (d * Device ) WriteColors (cs []color.RGBA ) (n int , err error ) {
46
47
d .startFrame ()
47
48
48
49
// write data
49
50
for _ , c := range cs {
50
51
// brightness is scaled to 5 bit value
51
- d .bus . Transfer ( 0xe0 | (c .A >> 3 ) )
52
+ d .buf [ 0 ] = 0xe0 | (c .A >> 3 )
52
53
53
54
// set the colors
54
55
switch d .Order {
55
56
case BRG :
56
- d .bus . Transfer ( c .B )
57
- d .bus . Transfer ( c .R )
58
- d .bus . Transfer ( c .G )
57
+ d .buf [ 1 ] = c .B
58
+ d .buf [ 2 ] = c .R
59
+ d .buf [ 3 ] = c .G
59
60
case GRB :
60
- d .bus . Transfer ( c .G )
61
- d .bus . Transfer ( c .R )
62
- d .bus . Transfer ( c .B )
61
+ d .buf [ 1 ] = c .G
62
+ d .buf [ 2 ] = c .R
63
+ d .buf [ 3 ] = c .B
63
64
case BGR :
64
- d .bus . Transfer ( c .B )
65
- d .bus . Transfer ( c .G )
66
- d .bus . Transfer ( c .R )
65
+ d .buf [ 1 ] = c .B
66
+ d .buf [ 2 ] = c .G
67
+ d .buf [ 3 ] = c .R
67
68
}
69
+ d .bus .Tx (d .buf [:], nil )
68
70
}
69
71
70
72
d .endFrame (len (cs ))
@@ -73,7 +75,7 @@ func (d Device) WriteColors(cs []color.RGBA) (n int, err error) {
73
75
}
74
76
75
77
// Write the raw bytes using the APA102 protocol.
76
- func (d Device ) Write (buf []byte ) (n int , err error ) {
78
+ func (d * Device ) Write (buf []byte ) (n int , err error ) {
77
79
d .startFrame ()
78
80
d .bus .Tx (buf , nil )
79
81
d .endFrame (len (buf ) / 4 )
@@ -82,14 +84,14 @@ func (d Device) Write(buf []byte) (n int, err error) {
82
84
}
83
85
84
86
// startFrame sends the start bytes for a strand of LEDs.
85
- func (d Device ) startFrame () {
87
+ func (d * Device ) startFrame () {
86
88
d .bus .Tx (startFrame , nil )
87
89
}
88
90
89
91
// endFrame sends the end frame marker with one extra bit per LED so
90
92
// long strands of LEDs receive the necessary termination for updates.
91
93
// See https://cpldcpu.wordpress.com/2014/11/30/understanding-the-apa102-superled/
92
- func (d Device ) endFrame (count int ) {
94
+ func (d * Device ) endFrame (count int ) {
93
95
for i := 0 ; i < count / 16 ; i ++ {
94
96
d .bus .Transfer (0xff )
95
97
}
0 commit comments