Skip to content

Commit 85769ca

Browse files
Write bytes individually
1 parent b2b1104 commit 85769ca

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/lib.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ where
2626
}
2727
/// Write a single color for ws2812 devices
2828
#[cfg(feature = "slow")]
29-
fn write_color(&mut self, data: Color) {
30-
let mut serial_bits = (data.g as u32) << 16 | (data.r as u32) << 8 | (data.b as u32) << 0;
31-
for _ in 0..24 {
32-
if (serial_bits & 0x00800000) != 0 {
29+
fn write_byte(&mut self, data: u8) {
30+
for _ in 0..8 {
31+
if (data & 0x80) != 0 {
3332
block!(self.timer.wait()).ok();
3433
self.pin.set_high();
3534
block!(self.timer.wait()).ok();
@@ -42,15 +41,14 @@ where
4241
block!(self.timer.wait()).ok();
4342
block!(self.timer.wait()).ok();
4443
}
45-
serial_bits <<= 1;
44+
data <<= 1;
4645
}
4746
}
4847

4948
#[cfg(not(feature = "slow"))]
50-
fn write_color(&mut self, data: Color) {
51-
let mut serial_bits = (data.g as u32) << 16 | (data.r as u32) << 8 | (data.b as u32) << 0;
52-
for _ in 0..24 {
53-
if (serial_bits & 0x00800000) != 0 {
49+
fn write_byte(&mut self, data: u8) {
50+
for _ in 0..8 {
51+
if (data & 0x80) != 0 {
5452
block!(self.timer.wait()).ok();
5553
self.pin.set_high();
5654
block!(self.timer.wait()).ok();
@@ -63,7 +61,7 @@ where
6361
self.pin.set_low();
6462
block!(self.timer.wait()).ok();
6563
}
66-
serial_bits <<= 1;
64+
data <<= 1;
6765
}
6866
}
6967
}
@@ -81,7 +79,9 @@ where
8179
T: Iterator<Item = Color>,
8280
{
8381
for item in iterator {
84-
self.write_color(item);
82+
self.write_byte(item.g);
83+
self.write_byte(item.r);
84+
self.write_byte(item.b);
8585
}
8686
// Get a timeout period of 300 ns
8787
for _ in 0..900 {

0 commit comments

Comments
 (0)