Skip to content

Commit b2b1104

Browse files
Add a slow feature
1 parent 41fc4a2 commit b2b1104

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ edition = "2018"
88
nb = "0.1.1"
99
smart-leds-trait = {git = "https://github.com/smart-leds-rs/smart-leds-trait"}
1010

11+
[features]
12+
slow = []
13+
1114
[dependencies.embedded-hal]
1215
features = ["unproven"]
1316
version = "0.2.1"

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Ws2812 lib with timer based delays
2+
3+
If your timer/micro is to slow (e.g. all leds are white), you may wish to enable
4+
the `slow` feature. It will remove any delay for the high part of the zero bits.
5+
This may be too short for some led strips, which may display wrong data. In that
6+
case, you might want to clock higher or use another driver.

src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@ where
2525
Self { timer, pin }
2626
}
2727
/// Write a single color for ws2812 devices
28+
#[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 {
33+
block!(self.timer.wait()).ok();
34+
self.pin.set_high();
35+
block!(self.timer.wait()).ok();
36+
block!(self.timer.wait()).ok();
37+
self.pin.set_low();
38+
} else {
39+
block!(self.timer.wait()).ok();
40+
self.pin.set_high();
41+
self.pin.set_low();
42+
block!(self.timer.wait()).ok();
43+
block!(self.timer.wait()).ok();
44+
}
45+
serial_bits <<= 1;
46+
}
47+
}
48+
49+
#[cfg(not(feature = "slow"))]
2850
fn write_color(&mut self, data: Color) {
2951
let mut serial_bits = (data.g as u32) << 16 | (data.r as u32) << 8 | (data.b as u32) << 0;
3052
for _ in 0..24 {

0 commit comments

Comments
 (0)