Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to use the overhauled GPIO API #25

Merged
merged 2 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/rp-rs/ws2812-pio-rs/"
[dependencies]
embedded-hal = "0.2.5"
fugit = "0.3.5"
rp2040-hal = "0.9.0"
rp2040-hal = "0.9.0-alpha.1"
pio = "0.2.0"
smart-leds-trait = "0.2.1"
nb = "1.0.0"
Expand Down
45 changes: 20 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use cortex_m;
use embedded_hal::timer::CountDown;
use fugit::{ExtU32, HertzU32};
use rp2040_hal::{
gpio::{Function, FunctionConfig, Pin, PinId, ValidPinMode},
gpio::AnyPin,
pio::{PIOExt, StateMachineIndex, Tx, UninitStateMachine, PIO},
};
use smart_leds_trait::SmartLedsWrite;
Expand Down Expand Up @@ -56,25 +56,23 @@ use smart_leds_trait::SmartLedsWrite;
///```
pub struct Ws2812Direct<P, SM, I>
where
I: PinId,
P: PIOExt + FunctionConfig,
Function<P>: ValidPinMode<I>,
I: AnyPin<Function = P::PinFunction>,
P: PIOExt,
SM: StateMachineIndex,
{
tx: Tx<(P, SM)>,
_pin: Pin<I, Function<P>>,
_pin: I,
}

impl<P, SM, I> Ws2812Direct<P, SM, I>
where
I: PinId,
P: PIOExt + FunctionConfig,
Function<P>: ValidPinMode<I>,
I: AnyPin<Function = P::PinFunction>,
P: PIOExt,
SM: StateMachineIndex,
{
/// Creates a new instance of this driver.
pub fn new(
pin: Pin<I, Function<P>>,
pin: I,
pio: &mut PIO<P>,
sm: UninitStateMachine<(P, SM)>,
clock_freq: fugit::HertzU32,
Expand Down Expand Up @@ -127,11 +125,12 @@ where
let int: u16 = int as u16;
let frac: u8 = frac as u8;

let pin = pin.into();
let (mut sm, _, tx) = rp2040_hal::pio::PIOBuilder::from_program(installed)
// only use TX FIFO
.buffers(rp2040_hal::pio::Buffers::OnlyTx)
// Pin configuration
.side_set_pin_base(I::DYN.num)
.side_set_pin_base(pin.id().num)
// OSR config
.out_shift_direction(rp2040_hal::pio::ShiftDirection::Left)
.autopull(true)
Expand All @@ -140,19 +139,18 @@ where
.build(sm);

// Prepare pin's direction.
sm.set_pindirs([(I::DYN.num, rp2040_hal::pio::PinDir::Output)]);
sm.set_pindirs([(pin.id().num, rp2040_hal::pio::PinDir::Output)]);

sm.start();

Self { tx, _pin: pin }
Self { tx, _pin: I::from(pin) }
}
}

impl<P, SM, I> SmartLedsWrite for Ws2812Direct<P, SM, I>
where
I: PinId,
P: PIOExt + FunctionConfig,
Function<P>: ValidPinMode<I>,
I: AnyPin<Function = P::PinFunction>,
P: PIOExt,
SM: StateMachineIndex,
{
type Color = smart_leds_trait::RGB8;
Expand Down Expand Up @@ -214,10 +212,9 @@ where
///```
pub struct Ws2812<P, SM, C, I>
where
I: PinId,
C: CountDown,
P: PIOExt + FunctionConfig,
Function<P>: ValidPinMode<I>,
I: AnyPin<Function = P::PinFunction>,
P: PIOExt,
SM: StateMachineIndex,
{
driver: Ws2812Direct<P, SM, I>,
Expand All @@ -226,15 +223,14 @@ where

impl<P, SM, C, I> Ws2812<P, SM, C, I>
where
I: PinId,
C: CountDown,
P: PIOExt + FunctionConfig,
Function<P>: ValidPinMode<I>,
I: AnyPin<Function = P::PinFunction>,
P: PIOExt,
SM: StateMachineIndex,
{
/// Creates a new instance of this driver.
pub fn new(
pin: Pin<I, Function<P>>,
pin: I,
pio: &mut PIO<P>,
sm: UninitStateMachine<(P, SM)>,
clock_freq: fugit::HertzU32,
Expand All @@ -248,9 +244,8 @@ where

impl<'timer, P, SM, I> SmartLedsWrite for Ws2812<P, SM, rp2040_hal::timer::CountDown<'timer>, I>
where
I: PinId,
P: PIOExt + FunctionConfig,
Function<P>: ValidPinMode<I>,
I: AnyPin<Function = P::PinFunction>,
P: PIOExt,
SM: StateMachineIndex,
{
type Color = smart_leds_trait::RGB8;
Expand Down