@@ -23,6 +23,12 @@ pub const MODE: Mode = Mode {
23
23
phase : Phase :: CaptureOnFirstTransition ,
24
24
} ;
25
25
26
+ #[ derive( Debug ) ]
27
+ pub enum Error < E > {
28
+ OutOfBounds ,
29
+ Spi ( E ) ,
30
+ }
31
+
26
32
pub mod devices {
27
33
pub struct Ws2812 ;
28
34
pub struct Sk6812w ;
@@ -92,17 +98,22 @@ where
92
98
SPI : FullDuplex < u8 , Error = E > ,
93
99
{
94
100
/// Write a single byte for ws2812 devices
95
- fn write_byte ( & mut self , mut data : u8 ) {
101
+ fn write_byte ( & mut self , mut data : u8 ) -> Result < ( ) , Error < E > > {
96
102
// Send two bits in one spi byte. High time first, then the low time
97
103
// The maximum for T0H is 500ns, the minimum for one bit 1063 ns.
98
104
// These result in the upper and lower spi frequency limits
99
105
let patterns = [ 0b1000_1000 , 0b1000_1110 , 0b11101000 , 0b11101110 ] ;
106
+
107
+ if self . index > self . data . len ( ) - 4 {
108
+ return Err ( Error :: OutOfBounds ) ;
109
+ }
100
110
for _ in 0 ..4 {
101
111
let bits = ( data & 0b1100_0000 ) >> 6 ;
102
112
self . data [ self . index ] = patterns[ bits as usize ] ;
103
113
self . index += 1 ;
104
114
data <<= 2 ;
105
115
}
116
+ Ok ( ( ) )
106
117
}
107
118
108
119
fn send_data ( & mut self ) -> Result < ( ) , E > {
@@ -134,10 +145,10 @@ impl<'a, SPI, E> SmartLedsWrite for Ws2812<'a, SPI>
134
145
where
135
146
SPI : FullDuplex < u8 , Error = E > ,
136
147
{
137
- type Error = E ;
148
+ type Error = Error < E > ;
138
149
type Color = RGB8 ;
139
150
/// Write all the items of an iterator to a ws2812 strip
140
- fn write < T , I > ( & mut self , iterator : T ) -> Result < ( ) , E >
151
+ fn write < T , I > ( & mut self , iterator : T ) -> Result < ( ) , Error < E > >
141
152
where
142
153
T : Iterator < Item = I > ,
143
154
I : Into < Self :: Color > ,
@@ -146,22 +157,22 @@ where
146
157
147
158
for item in iterator {
148
159
let item = item. into ( ) ;
149
- self . write_byte ( item. g ) ;
150
- self . write_byte ( item. r ) ;
151
- self . write_byte ( item. b ) ;
160
+ self . write_byte ( item. g ) ? ;
161
+ self . write_byte ( item. r ) ? ;
162
+ self . write_byte ( item. b ) ? ;
152
163
}
153
- self . send_data ( )
164
+ self . send_data ( ) . map_err ( |e| Error :: Spi ( e ) )
154
165
}
155
166
}
156
167
157
168
impl < ' a , SPI , E > SmartLedsWrite for Ws2812 < ' a , SPI , devices:: Sk6812w >
158
169
where
159
170
SPI : FullDuplex < u8 , Error = E > ,
160
171
{
161
- type Error = E ;
172
+ type Error = Error < E > ;
162
173
type Color = RGBW < u8 , u8 > ;
163
174
/// Write all the items of an iterator to a ws2812 strip
164
- fn write < T , I > ( & mut self , iterator : T ) -> Result < ( ) , E >
175
+ fn write < T , I > ( & mut self , iterator : T ) -> Result < ( ) , Error < E > >
165
176
where
166
177
T : Iterator < Item = I > ,
167
178
I : Into < Self :: Color > ,
@@ -170,11 +181,11 @@ where
170
181
171
182
for item in iterator {
172
183
let item = item. into ( ) ;
173
- self . write_byte ( item. g ) ;
174
- self . write_byte ( item. r ) ;
175
- self . write_byte ( item. b ) ;
176
- self . write_byte ( item. a . 0 ) ;
184
+ self . write_byte ( item. g ) ? ;
185
+ self . write_byte ( item. r ) ? ;
186
+ self . write_byte ( item. b ) ? ;
187
+ self . write_byte ( item. a . 0 ) ? ;
177
188
}
178
- self . send_data ( )
189
+ self . send_data ( ) . map_err ( |e| Error :: Spi ( e ) )
179
190
}
180
191
}
0 commit comments