Skip to content

Commit 42d348f

Browse files
Add support for embedded_hal v 1.0.0
1 parent 4780d07 commit 42d348f

File tree

3 files changed

+22
-50
lines changed

3 files changed

+22
-50
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ readme = "README.md"
1515
repository = "https://github.com/smart-leds-rs/ws2812-spi-rs"
1616

1717
[dependencies]
18-
nb = "1.1.0"
1918
smart-leds-trait = "0.3.0"
20-
embedded-hal = "0.2.7"
19+
embedded-hal = "1.0.0"
2120

2221
[features]
2322
mosi_idle_high = []

src/lib.rs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! - For usage with `smart-leds`
44
//! - Implements the `SmartLedsWrite` trait
55
//!
6-
//! Needs a type implementing the `spi::FullDuplex` trait.
6+
//! Needs a type implementing the `spi::SpiBus` trait.
77
//!
88
//! The spi peripheral should run at 2MHz to 3.8 MHz
99
@@ -16,14 +16,13 @@ use embedded_hal as hal;
1616

1717
pub mod prerendered;
1818

19-
use hal::spi::{FullDuplex, Mode, Phase, Polarity};
19+
use hal::spi::{Mode, Phase, Polarity, SpiBus};
2020

2121
use core::marker::PhantomData;
22+
use core::slice::from_ref;
2223

2324
use smart_leds_trait::{SmartLedsWrite, RGB8, RGBW};
2425

25-
use nb::block;
26-
2726
/// SPI mode that can be used for this crate
2827
///
2928
/// Provided for convenience
@@ -45,7 +44,7 @@ pub struct Ws2812<SPI, DEVICE = devices::Ws2812> {
4544

4645
impl<SPI, E> Ws2812<SPI>
4746
where
48-
SPI: FullDuplex<u8, Error = E>,
47+
SPI: SpiBus<u8, Error = E>,
4948
{
5049
/// Use ws2812 devices via spi
5150
///
@@ -65,7 +64,7 @@ where
6564

6665
impl<SPI, E> Ws2812<SPI, devices::Sk6812w>
6766
where
68-
SPI: FullDuplex<u8, Error = E>,
67+
SPI: SpiBus<u8, Error = E>,
6968
{
7069
/// Use sk6812w devices via spi
7170
///
@@ -87,7 +86,7 @@ where
8786

8887
impl<SPI, D, E> Ws2812<SPI, D>
8988
where
90-
SPI: FullDuplex<u8, Error = E>,
89+
SPI: SpiBus<u8, Error = E>,
9190
{
9291
/// Write a single byte for ws2812 devices
9392
fn write_byte(&mut self, mut data: u8) -> Result<(), E> {
@@ -97,8 +96,7 @@ where
9796
let patterns = [0b1000_1000, 0b1000_1110, 0b11101000, 0b11101110];
9897
for _ in 0..4 {
9998
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]))?;
102100
data <<= 2;
103101
}
104102
Ok(())
@@ -107,16 +105,15 @@ where
107105
fn flush(&mut self) -> Result<(), E> {
108106
// 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
109107
for _ in 0..140 {
110-
block!(self.spi.send(0))?;
111-
block!(self.spi.read()).ok();
108+
self.spi.write(from_ref(&0))?;
112109
}
113110
Ok(())
114111
}
115112
}
116113

117114
impl<SPI, E> SmartLedsWrite for Ws2812<SPI>
118115
where
119-
SPI: FullDuplex<u8, Error = E>,
116+
SPI: SpiBus<u8, Error = E>,
120117
{
121118
type Error = E;
122119
type Color = RGB8;
@@ -126,10 +123,6 @@ where
126123
T: IntoIterator<Item = I>,
127124
I: Into<Self::Color>,
128125
{
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))?;
133126
if cfg!(feature = "mosi_idle_high") {
134127
self.flush()?;
135128
}
@@ -141,15 +134,13 @@ where
141134
self.write_byte(item.b)?;
142135
}
143136
self.flush()?;
144-
// Now, resolve the offset we introduced at the beginning
145-
block!(self.spi.read())?;
146137
Ok(())
147138
}
148139
}
149140

150141
impl<SPI, E> SmartLedsWrite for Ws2812<SPI, devices::Sk6812w>
151142
where
152-
SPI: FullDuplex<u8, Error = E>,
143+
SPI: SpiBus<u8, Error = E>,
153144
{
154145
type Error = E;
155146
type Color = RGBW<u8, u8>;
@@ -159,10 +150,6 @@ where
159150
T: IntoIterator<Item = I>,
160151
I: Into<Self::Color>,
161152
{
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))?;
166153
if cfg!(feature = "mosi_idle_high") {
167154
self.flush()?;
168155
}
@@ -175,8 +162,6 @@ where
175162
self.write_byte(item.a.0)?;
176163
}
177164
self.flush()?;
178-
// Now, resolve the offset we introduced at the beginning
179-
block!(self.spi.read())?;
180165
Ok(())
181166
}
182167
}

src/prerendered.rs

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
66
use embedded_hal as hal;
77

8-
use hal::spi::{FullDuplex, Mode, Phase, Polarity};
8+
use hal::spi::{Mode, Phase, Polarity, SpiBus};
99

1010
use core::marker::PhantomData;
11+
use core::slice::from_ref;
1112

1213
use smart_leds_trait::{SmartLedsWrite, RGB8, RGBW};
1314

14-
use nb;
15-
use nb::block;
16-
1715
/// SPI mode that can be used for this crate
1816
///
1917
/// Provided for convenience
@@ -43,7 +41,7 @@ pub struct Ws2812<'a, SPI, DEVICE = devices::Ws2812> {
4341

4442
impl<'a, SPI, E> Ws2812<'a, SPI>
4543
where
46-
SPI: FullDuplex<u8, Error = E>,
44+
SPI: SpiBus<u8, Error = E>,
4745
{
4846
/// Use ws2812 devices via spi
4947
///
@@ -68,7 +66,7 @@ where
6866

6967
impl<'a, SPI, E> Ws2812<'a, SPI, devices::Sk6812w>
7068
where
71-
SPI: FullDuplex<u8, Error = E>,
69+
SPI: SpiBus<u8, Error = E>,
7270
{
7371
/// Use sk6812w devices via spi
7472
///
@@ -95,7 +93,7 @@ where
9593

9694
impl<'a, SPI, D, E> Ws2812<'a, SPI, D>
9795
where
98-
SPI: FullDuplex<u8, Error = E>,
96+
SPI: SpiBus<u8, Error = E>,
9997
{
10098
/// Write a single byte for ws2812 devices
10199
fn write_byte(&mut self, mut data: u8) -> Result<(), Error<E>> {
@@ -117,33 +115,23 @@ where
117115
}
118116

119117
fn send_data(&mut self) -> Result<(), E> {
120-
// We introduce an offset in the fifo here, so there's always one byte in transit
121-
// Some MCUs (like the stm32f1) only a one byte fifo, which would result
122-
// in overrun error if two bytes need to be stored
123-
block!(self.spi.send(0))?;
124118
if cfg!(feature = "mosi_idle_high") {
125119
for _ in 0..140 {
126-
block!(self.spi.send(0))?;
127-
block!(self.spi.read())?;
120+
self.spi.write(from_ref(&0))?;
128121
}
129122
}
130-
for b in self.data[..self.index].iter() {
131-
block!(self.spi.send(*b))?;
132-
block!(self.spi.read())?;
133-
}
123+
124+
self.spi.write(&self.data[..self.index])?;
134125
for _ in 0..140 {
135-
block!(self.spi.send(0))?;
136-
block!(self.spi.read())?;
126+
self.spi.write(from_ref(&0))?;
137127
}
138-
// Now, resolve the offset we introduced at the beginning
139-
block!(self.spi.read())?;
140128
Ok(())
141129
}
142130
}
143131

144132
impl<'a, SPI, E> SmartLedsWrite for Ws2812<'a, SPI>
145133
where
146-
SPI: FullDuplex<u8, Error = E>,
134+
SPI: SpiBus<u8, Error = E>,
147135
{
148136
type Error = Error<E>;
149137
type Color = RGB8;
@@ -167,7 +155,7 @@ where
167155

168156
impl<'a, SPI, E> SmartLedsWrite for Ws2812<'a, SPI, devices::Sk6812w>
169157
where
170-
SPI: FullDuplex<u8, Error = E>,
158+
SPI: SpiBus<u8, Error = E>,
171159
{
172160
type Error = Error<E>;
173161
type Color = RGBW<u8, u8>;

0 commit comments

Comments
 (0)