3
3
//! - For usage with `smart-leds`
4
4
//! - Implements the `SmartLedsWrite` trait
5
5
//!
6
- //! Needs a type implementing the `spi::FullDuplex ` trait.
6
+ //! Needs a type implementing the `spi::SpiBus ` trait.
7
7
//!
8
8
//! The spi peripheral should run at 2MHz to 3.8 MHz
9
9
@@ -16,14 +16,13 @@ use embedded_hal as hal;
16
16
17
17
pub mod prerendered;
18
18
19
- use hal:: spi:: { FullDuplex , Mode , Phase , Polarity } ;
19
+ use hal:: spi:: { Mode , Phase , Polarity , SpiBus } ;
20
20
21
21
use core:: marker:: PhantomData ;
22
+ use core:: slice:: from_ref;
22
23
23
24
use smart_leds_trait:: { SmartLedsWrite , RGB8 , RGBW } ;
24
25
25
- use nb:: block;
26
-
27
26
/// SPI mode that can be used for this crate
28
27
///
29
28
/// Provided for convenience
@@ -45,7 +44,7 @@ pub struct Ws2812<SPI, DEVICE = devices::Ws2812> {
45
44
46
45
impl < SPI , E > Ws2812 < SPI >
47
46
where
48
- SPI : FullDuplex < u8 , Error = E > ,
47
+ SPI : SpiBus < u8 , Error = E > ,
49
48
{
50
49
/// Use ws2812 devices via spi
51
50
///
65
64
66
65
impl < SPI , E > Ws2812 < SPI , devices:: Sk6812w >
67
66
where
68
- SPI : FullDuplex < u8 , Error = E > ,
67
+ SPI : SpiBus < u8 , Error = E > ,
69
68
{
70
69
/// Use sk6812w devices via spi
71
70
///
87
86
88
87
impl < SPI , D , E > Ws2812 < SPI , D >
89
88
where
90
- SPI : FullDuplex < u8 , Error = E > ,
89
+ SPI : SpiBus < u8 , Error = E > ,
91
90
{
92
91
/// Write a single byte for ws2812 devices
93
92
fn write_byte ( & mut self , mut data : u8 ) -> Result < ( ) , E > {
97
96
let patterns = [ 0b1000_1000 , 0b1000_1110 , 0b11101000 , 0b11101110 ] ;
98
97
for _ in 0 ..4 {
99
98
let bits = ( data & 0b1100_0000 ) >> 6 ;
100
- block ! ( self . spi. send( patterns[ bits as usize ] ) ) ?;
101
- block ! ( self . spi. read( ) ) . ok ( ) ;
99
+ self . spi . write ( from_ref ( & patterns[ bits as usize ] ) ) ?;
102
100
data <<= 2 ;
103
101
}
104
102
Ok ( ( ) )
@@ -107,16 +105,15 @@ where
107
105
fn flush ( & mut self ) -> Result < ( ) , E > {
108
106
// Should be > 300μs, so for an SPI Freq. of 3.8MHz, we have to send at least 1140 low bits or 140 low bytes
109
107
for _ in 0 ..140 {
110
- block ! ( self . spi. send( 0 ) ) ?;
111
- block ! ( self . spi. read( ) ) . ok ( ) ;
108
+ self . spi . write ( from_ref ( & 0 ) ) ?;
112
109
}
113
110
Ok ( ( ) )
114
111
}
115
112
}
116
113
117
114
impl < SPI , E > SmartLedsWrite for Ws2812 < SPI >
118
115
where
119
- SPI : FullDuplex < u8 , Error = E > ,
116
+ SPI : SpiBus < u8 , Error = E > ,
120
117
{
121
118
type Error = E ;
122
119
type Color = RGB8 ;
@@ -126,10 +123,6 @@ where
126
123
T : IntoIterator < Item = I > ,
127
124
I : Into < Self :: Color > ,
128
125
{
129
- // We introduce an offset in the fifo here, so there's always one byte in transit
130
- // Some MCUs (like the stm32f1) only a one byte fifo, which would result
131
- // in overrun error if two bytes need to be stored
132
- block ! ( self . spi. send( 0 ) ) ?;
133
126
if cfg ! ( feature = "mosi_idle_high" ) {
134
127
self . flush ( ) ?;
135
128
}
@@ -141,15 +134,13 @@ where
141
134
self . write_byte ( item. b ) ?;
142
135
}
143
136
self . flush ( ) ?;
144
- // Now, resolve the offset we introduced at the beginning
145
- block ! ( self . spi. read( ) ) ?;
146
137
Ok ( ( ) )
147
138
}
148
139
}
149
140
150
141
impl < SPI , E > SmartLedsWrite for Ws2812 < SPI , devices:: Sk6812w >
151
142
where
152
- SPI : FullDuplex < u8 , Error = E > ,
143
+ SPI : SpiBus < u8 , Error = E > ,
153
144
{
154
145
type Error = E ;
155
146
type Color = RGBW < u8 , u8 > ;
@@ -159,10 +150,6 @@ where
159
150
T : IntoIterator < Item = I > ,
160
151
I : Into < Self :: Color > ,
161
152
{
162
- // We introduce an offset in the fifo here, so there's always one byte in transit
163
- // Some MCUs (like the stm32f1) only a one byte fifo, which would result
164
- // in overrun error if two bytes need to be stored
165
- block ! ( self . spi. send( 0 ) ) ?;
166
153
if cfg ! ( feature = "mosi_idle_high" ) {
167
154
self . flush ( ) ?;
168
155
}
@@ -175,8 +162,6 @@ where
175
162
self . write_byte ( item. a . 0 ) ?;
176
163
}
177
164
self . flush ( ) ?;
178
- // Now, resolve the offset we introduced at the beginning
179
- block ! ( self . spi. read( ) ) ?;
180
165
Ok ( ( ) )
181
166
}
182
167
}
0 commit comments