Skip to content

Commit

Permalink
Upgrade to embedded-hal 1.0.0
Browse files Browse the repository at this point in the history
There was one breaking change that will propergate to a breaking change for end users. CountDown is no longer a trait in embedded_hal 1 and there is no replacment. There are two main paths we could take here: using the CountDown timer from rp2040-hal crate or convert to DelayNs which is what embedded_hal suggested since we only actually use it for a 60micro second delay. This does mean that you need to pass in the Timer instance rather than a CountDown instead but overall keeps the code a bit more generic and requires fewer trait bounds.

It would be nice to rename the generics from C to T as they really now represent the Timer rather than CountDown but T is already used by some iterators later on and renaming those to I conflicts with other generics - so i opted to avoid the renaming bikeshedding in this completely.
  • Loading branch information
mdaffin committed Jan 4, 2025
1 parent 17be8aa commit 5e8eced
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ documentation = "https://docs.rs/ws2812-pio"
repository = "https://github.com/rp-rs/ws2812-pio-rs/"

[dependencies]
embedded-hal = "0.2.5"
embedded-hal = "1.0.0"
fugit = "0.3.5"
rp2040-hal = "0.11"
pio = "0.2.0"
Expand Down
24 changes: 10 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
//! Bear in mind that you will have to take care of timing requirements
//! yourself then.
use embedded_hal::timer::CountDown;
use fugit::{ExtU32, HertzU32, MicrosDurationU32};
use embedded_hal::delay::DelayNs;
use fugit::HertzU32;
use rp2040_hal::{
gpio::AnyPin,
pio::{PIOExt, StateMachineIndex, Tx, UninitStateMachine, PIO},
Expand Down Expand Up @@ -239,18 +239,18 @@ where
///```
pub struct Ws2812<P, SM, C, I>
where
C: CountDown,
C: DelayNs,
I: AnyPin<Function = P::PinFunction>,
P: PIOExt,
SM: StateMachineIndex,
{
driver: Ws2812Direct<P, SM, I>,
cd: C,
timer: C,
}

impl<P, SM, C, I> Ws2812<P, SM, C, I>
where
C: CountDown,
C: DelayNs,
I: AnyPin<Function = P::PinFunction>,
P: PIOExt,
SM: StateMachineIndex,
Expand All @@ -261,18 +261,17 @@ where
pio: &mut PIO<P>,
sm: UninitStateMachine<(P, SM)>,
clock_freq: fugit::HertzU32,
cd: C,
timer: C,
) -> Ws2812<P, SM, C, I> {
let driver = Ws2812Direct::new(pin, pio, sm, clock_freq);

Self { driver, cd }
Self { driver, timer }
}
}

impl<P, SM, I, C> SmartLedsWrite for Ws2812<P, SM, C, I>
where
C: CountDown,
C::Time: From<MicrosDurationU32>,
C: DelayNs,
I: AnyPin<Function = P::PinFunction>,
P: PIOExt,
SM: StateMachineIndex,
Expand All @@ -287,17 +286,14 @@ where
self.driver.tx.clear_stalled_flag();
while !self.driver.tx.is_empty() && !self.driver.tx.has_stalled() {}

self.cd.start(60u32.micros());
let _ = nb::block!(self.cd.wait());

self.timer.delay_us(60);
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>,
C: DelayNs,
I: AnyPin<Function = P::PinFunction>,
P: PIOExt,
SM: StateMachineIndex,
Expand Down

0 comments on commit 5e8eced

Please sign in to comment.