Skip to content

Commit

Permalink
Merge pull request #36 from chinoto/pr30
Browse files Browse the repository at this point in the history
support smart-leds-trait 0.2 and 0.3
  • Loading branch information
9names authored Dec 15, 2024
2 parents 7254ad4 + 66c18ca commit b6f8495
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ embedded-hal = "0.2.5"
fugit = "0.3.5"
rp2040-hal = "0.10"
pio = "0.2.0"
smart-leds-trait = "0.2.1"
smart-leds-trait = "0.3"
smart-leds-trait-0-2 = { package = "smart-leds-trait", version = "0.2.1" }
nb = "1.0.0"
cortex-m = "0.7.3"
50 changes: 47 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use rp2040_hal::{
pio::{PIOExt, StateMachineIndex, Tx, UninitStateMachine, PIO},
};
use smart_leds_trait::SmartLedsWrite;
use smart_leds_trait_0_2::SmartLedsWrite as SmartLedsWrite02;

/// This is the WS2812 PIO Driver.
///
Expand Down Expand Up @@ -166,7 +167,7 @@ where
/// PIO FIFO until all data has been transmitted to the LED chain.
fn write<T, J>(&mut self, iterator: T) -> Result<(), ()>
where
T: Iterator<Item = J>,
T: IntoIterator<Item = J>,
J: Into<Self::Color>,
{
for item in iterator {
Expand All @@ -182,6 +183,30 @@ where
}
}

impl<P, SM, I> SmartLedsWrite02 for Ws2812Direct<P, SM, I>
where
I: AnyPin<Function = P::PinFunction>,
P: PIOExt,
SM: StateMachineIndex,
{
type Color = smart_leds_trait::RGB8;
type Error = ();
/// If you call this function, be advised that you will have to wait
/// at least 60 microseconds between calls of this function!
/// That means, either you get hold on a timer and the timing
/// requirements right your self, or rather use [Ws2812].
///
/// Please bear in mind, that it still blocks when writing into the
/// PIO FIFO until all data has been transmitted to the LED chain.
fn write<T, J>(&mut self, iterator: T) -> Result<(), ()>
where
T: Iterator<Item = J>,
J: Into<Self::Color>,
{
SmartLedsWrite::write(self, iterator)
}
}

/// Instance of a WS2812 LED chain.
///
/// Use the [Ws2812::write] method to update the WS2812 LED chain.
Expand Down Expand Up @@ -256,7 +281,7 @@ where
type Error = ();
fn write<T, J>(&mut self, iterator: T) -> Result<(), ()>
where
T: Iterator<Item = J>,
T: IntoIterator<Item = J>,
J: Into<Self::Color>,
{
self.driver.tx.clear_stalled_flag();
Expand All @@ -265,6 +290,25 @@ where
self.cd.start(60u32.micros());
let _ = nb::block!(self.cd.wait());

self.driver.write(iterator)
SmartLedsWrite::write(&mut self.driver, iterator)
}
}

impl<P, SM, I, C> SmartLedsWrite02 for Ws2812<P, SM, C, I>
where
C: CountDown,
C::Time: From<MicrosDurationU32>,
I: AnyPin<Function = P::PinFunction>,
P: PIOExt,
SM: StateMachineIndex,
{
type Color = smart_leds_trait::RGB8;
type Error = ();
fn write<T, J>(&mut self, iterator: T) -> Result<(), ()>
where
T: IntoIterator<Item = J>,
J: Into<Self::Color>,
{
SmartLedsWrite::write(self, iterator)
}
}

0 comments on commit b6f8495

Please sign in to comment.