Skip to content

Commit 293e698

Browse files
committed
Include reset sequences in prerendered transactions
1 parent 4543138 commit 293e698

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

src/prerendered.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use embedded_hal as hal;
88
use hal::spi::{Mode, Phase, Polarity, SpiBus};
99

1010
use core::marker::PhantomData;
11-
use core::slice::from_ref;
1211

1312
use smart_leds_trait::{SmartLedsWrite, RGB8, RGBW};
1413

@@ -50,7 +49,7 @@ where
5049
/// You may need to look at the datasheet and your own hal to verify this.
5150
///
5251
/// 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)
5453
///
5554
/// Please ensure that the mcu is pretty fast, otherwise weird timing
5655
/// issues will occur
@@ -74,8 +73,8 @@ where
7473
///
7574
/// You may need to look at the datasheet and your own hal to verify this.
7675
///
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)
7978
///
8079
/// Please ensure that the mcu is pretty fast, otherwise weird timing
8180
/// issues will occur
@@ -114,19 +113,22 @@ where
114113
Ok(())
115114
}
116115

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];
123120

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);
127123
}
124+
self.data[self.index..(self.index + FLUSH_DATA_LEN)].copy_from_slice(FLUSH_DATA);
125+
self.index += FLUSH_DATA_LEN;
128126
Ok(())
129127
}
128+
129+
fn send_data(&mut self) -> Result<(), E> {
130+
self.spi.write(&self.data[..self.index])
131+
}
130132
}
131133

132134
impl<'a, SPI, E> SmartLedsWrite for Ws2812<'a, SPI>
@@ -143,12 +145,18 @@ where
143145
{
144146
self.index = 0;
145147

148+
if cfg!(feature = "mosi_idle_high") {
149+
self.flush()?;
150+
}
151+
146152
for item in iterator {
147153
let item = item.into();
148154
self.write_byte(item.g)?;
149155
self.write_byte(item.r)?;
150156
self.write_byte(item.b)?;
151157
}
158+
159+
self.flush()?;
152160
self.send_data().map_err(|e| Error::Spi(e))
153161
}
154162
}
@@ -167,13 +175,19 @@ where
167175
{
168176
self.index = 0;
169177

178+
if cfg!(feature = "mosi_idle_high") {
179+
self.flush()?;
180+
}
181+
170182
for item in iterator {
171183
let item = item.into();
172184
self.write_byte(item.g)?;
173185
self.write_byte(item.r)?;
174186
self.write_byte(item.b)?;
175187
self.write_byte(item.a.0)?;
176188
}
189+
190+
self.flush()?;
177191
self.send_data().map_err(|e| Error::Spi(e))
178192
}
179193
}

0 commit comments

Comments
 (0)