@@ -8,7 +8,6 @@ use embedded_hal as hal;
8
8
use hal:: spi:: { Mode , Phase , Polarity , SpiBus } ;
9
9
10
10
use core:: marker:: PhantomData ;
11
- use core:: slice:: from_ref;
12
11
13
12
use smart_leds_trait:: { SmartLedsWrite , RGB8 , RGBW } ;
14
13
50
49
/// You may need to look at the datasheet and your own hal to verify this.
51
50
///
52
51
/// You need to provide a buffer `data`, whoose length is at least 12 * the
53
- /// length of the led strip + 20 byes (or 40 , if using the `mosi_idle_high` feature)
52
+ /// length of the led strip + 140 bytes (or 280 , if using the `mosi_idle_high` feature)
54
53
///
55
54
/// Please ensure that the mcu is pretty fast, otherwise weird timing
56
55
/// issues will occur
74
73
///
75
74
/// You may need to look at the datasheet and your own hal to verify this.
76
75
///
77
- /// You need to provide a buffer `data`, whoose length is at least 12 * the
78
- /// length of the led strip
76
+ /// You need to provide a buffer `data`, whoose length is at least 16 * the
77
+ /// length of the led strip + 140 bytes (or 280, if using the `mosi_idle_high` feature)
79
78
///
80
79
/// Please ensure that the mcu is pretty fast, otherwise weird timing
81
80
/// issues will occur
@@ -114,19 +113,22 @@ where
114
113
Ok ( ( ) )
115
114
}
116
115
117
- fn send_data ( & mut self ) -> Result < ( ) , E > {
118
- if cfg ! ( feature = "mosi_idle_high" ) {
119
- for _ in 0 ..140 {
120
- self . spi . write ( from_ref ( & 0 ) ) ?;
121
- }
122
- }
116
+ /// Add a reset sequence (140 zeroes) to the data buffer
117
+ fn flush ( & mut self ) -> Result < ( ) , Error < E > > {
118
+ const FLUSH_DATA_LEN : usize = 140 ;
119
+ const FLUSH_DATA : & [ u8 ] = & [ 0x00 ; FLUSH_DATA_LEN ] ;
123
120
124
- self . spi . write ( & self . data [ ..self . index ] ) ?;
125
- for _ in 0 ..140 {
126
- self . spi . write ( from_ref ( & 0 ) ) ?;
121
+ if self . index + FLUSH_DATA_LEN > self . data . len ( ) {
122
+ return Err ( Error :: OutOfBounds ) ;
127
123
}
124
+ self . data [ self . index ..( self . index + FLUSH_DATA_LEN ) ] . copy_from_slice ( FLUSH_DATA ) ;
125
+ self . index += FLUSH_DATA_LEN ;
128
126
Ok ( ( ) )
129
127
}
128
+
129
+ fn send_data ( & mut self ) -> Result < ( ) , E > {
130
+ self . spi . write ( & self . data [ ..self . index ] )
131
+ }
130
132
}
131
133
132
134
impl < ' a , SPI , E > SmartLedsWrite for Ws2812 < ' a , SPI >
@@ -143,12 +145,18 @@ where
143
145
{
144
146
self . index = 0 ;
145
147
148
+ if cfg ! ( feature = "mosi_idle_high" ) {
149
+ self . flush ( ) ?;
150
+ }
151
+
146
152
for item in iterator {
147
153
let item = item. into ( ) ;
148
154
self . write_byte ( item. g ) ?;
149
155
self . write_byte ( item. r ) ?;
150
156
self . write_byte ( item. b ) ?;
151
157
}
158
+
159
+ self . flush ( ) ?;
152
160
self . send_data ( ) . map_err ( |e| Error :: Spi ( e) )
153
161
}
154
162
}
@@ -167,13 +175,19 @@ where
167
175
{
168
176
self . index = 0 ;
169
177
178
+ if cfg ! ( feature = "mosi_idle_high" ) {
179
+ self . flush ( ) ?;
180
+ }
181
+
170
182
for item in iterator {
171
183
let item = item. into ( ) ;
172
184
self . write_byte ( item. g ) ?;
173
185
self . write_byte ( item. r ) ?;
174
186
self . write_byte ( item. b ) ?;
175
187
self . write_byte ( item. a . 0 ) ?;
176
188
}
189
+
190
+ self . flush ( ) ?;
177
191
self . send_data ( ) . map_err ( |e| Error :: Spi ( e) )
178
192
}
179
193
}
0 commit comments