Skip to content

Commit 993b979

Browse files
committed
WIP2
Signed-off-by: Daniel Schaefer <dhs@frame.work>
1 parent 567dc82 commit 993b979

File tree

1 file changed

+165
-166
lines changed

1 file changed

+165
-166
lines changed

src/lib.rs

+165-166
Original file line numberDiff line numberDiff line change
@@ -13,68 +13,67 @@
1313
//! Bear in mind that you will have to take care of timing requirements
1414
//! yourself then.
1515
16-
use cortex_m;
16+
//use cortex_m;
1717
use fugit::{ExtU32, HertzU32};
1818
use rp2040_hal::{
1919
gpio::{Function, Pin, PinId},
2020
pio::{PIOExt, StateMachineIndex, Tx, UninitStateMachine, PIO},
21-
timer::CountDown,
21+
//timer::CountDown,
2222
};
23-
use smart_leds_trait::SmartLedsWrite;
23+
//use smart_leds_trait::SmartLedsWrite;
2424

25-
/// This is the WS2812 PIO Driver.
26-
///
27-
/// For blocking applications is recommended to use
28-
/// the [Ws2812] struct instead of this raw driver.
29-
///
30-
/// If you use this driver directly, you will need to
31-
/// take care of the timing expectations of the [Ws2812Direct::write]
32-
/// method.
33-
///
34-
/// Typical usage example:
35-
///```ignore
36-
/// use rp2040_hal::clocks::init_clocks_and_plls;
37-
/// let clocks = init_clocks_and_plls(...);
38-
/// let pins = rp2040_hal::gpio::pin::bank0::Pins::new(...);
39-
///
40-
/// let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
41-
/// let mut ws = Ws2812Direct::new(
42-
/// pins.gpio4.into_mode(),
43-
/// &mut pio,
44-
/// sm0,
45-
/// clocks.peripheral_clock.freq(),
46-
/// );
47-
///
48-
/// // Then you will make sure yourself to not write too frequently:
49-
/// loop {
50-
/// use smart_leds::{SmartLedsWrite, RGB8};
51-
/// let color : RGB8 = (255, 0, 255).into();
52-
///
53-
/// ws.write([color].iter().copied()).unwrap();
54-
/// delay_for_at_least_60_microseconds();
55-
/// };
56-
///```
57-
pub struct Ws2812Direct<P, SM, I>
25+
///// This is the WS2812 PIO Driver.
26+
/////
27+
///// For blocking applications is recommended to use
28+
///// the [Ws2812] struct instead of this raw driver.
29+
/////
30+
///// If you use this driver directly, you will need to
31+
///// take care of the timing expectations of the [Ws2812Direct::write]
32+
///// method.
33+
/////
34+
///// Typical usage example:
35+
/////```ignore
36+
///// use rp2040_hal::clocks::init_clocks_and_plls;
37+
///// let clocks = init_clocks_and_plls(...);
38+
///// let pins = rp2040_hal::gpio::pin::bank0::Pins::new(...);
39+
/////
40+
///// let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
41+
///// let mut ws = Ws2812Direct::new(
42+
///// pins.gpio4.into_mode(),
43+
///// &mut pio,
44+
///// sm0,
45+
///// clocks.peripheral_clock.freq(),
46+
///// );
47+
/////
48+
///// // Then you will make sure yourself to not write too frequently:
49+
///// loop {
50+
///// use smart_leds::{SmartLedsWrite, RGB8};
51+
///// let color : RGB8 = (255, 0, 255).into();
52+
/////
53+
///// ws.write([color].iter().copied()).unwrap();
54+
///// delay_for_at_least_60_microseconds();
55+
///// };
56+
/////```
57+
//pub struct Ws2812Direct<P, SM, I>
58+
pub struct Ws2812Direct<P, SM>
5859
where
59-
I: PinId,
60-
P: PIOExt + FunctionConfig,
61-
Function<P>: ValidPinMode<I>,
60+
//I: PinId,
61+
P: PIOExt,
6262
SM: StateMachineIndex,
6363
{
6464
tx: Tx<(P, SM)>,
65-
_pin: Pin<I, Function<P>>,
6665
}
6766

68-
impl<P, SM, I> Ws2812Direct<P, SM, I>
67+
//impl<P, SM, I> Ws2812Direct<P, SM, I>
68+
impl<P, SM> Ws2812Direct<P, SM>
6969
where
70-
I: PinId,
71-
P: PIOExt + FunctionConfig,
72-
Function<P>: ValidPinMode<I>,
70+
//I: PinId,
71+
P: PIOExt,
7372
SM: StateMachineIndex,
7473
{
7574
/// Creates a new instance of this driver.
7675
pub fn new(
77-
pin: Pin<I, Function<P>>,
76+
//pin: Pin<I, Function<P>>,
7877
pio: &mut PIO<P>,
7978
sm: UninitStateMachine<(P, SM)>,
8079
clock_freq: fugit::HertzU32,
@@ -131,7 +130,7 @@ where
131130
// only use TX FIFO
132131
.buffers(rp2040_hal::pio::Buffers::OnlyTx)
133132
// Pin configuration
134-
.side_set_pin_base(I::DYN.num)
133+
//.side_set_pin_base(I::DYN.num)
135134
// OSR config
136135
.out_shift_direction(rp2040_hal::pio::ShiftDirection::Left)
137136
.autopull(true)
@@ -140,130 +139,130 @@ where
140139
.build(sm);
141140

142141
// Prepare pin's direction.
143-
sm.set_pindirs([(I::DYN.num, rp2040_hal::pio::PinDir::Output)]);
142+
//sm.set_pindirs([(I::DYN.num, rp2040_hal::pio::PinDir::Output)]);
144143

145144
sm.start();
146145

147-
Self { tx, _pin: pin }
146+
Self { tx }
148147
}
149148
}
150149

151-
impl<P, SM, I> SmartLedsWrite for Ws2812Direct<P, SM, I>
152-
where
153-
I: PinId,
154-
P: PIOExt + FunctionConfig,
155-
Function<P>: ValidPinMode<I>,
156-
SM: StateMachineIndex,
157-
{
158-
type Color = smart_leds_trait::RGB8;
159-
type Error = ();
160-
/// If you call this function, be advised that you will have to wait
161-
/// at least 60 microseconds between calls of this function!
162-
/// That means, either you get hold on a timer and the timing
163-
/// requirements right your self, or rather use [Ws2812].
164-
///
165-
/// Please bear in mind, that it still blocks when writing into the
166-
/// PIO FIFO until all data has been transmitted to the LED chain.
167-
fn write<T, J>(&mut self, iterator: T) -> Result<(), ()>
168-
where
169-
T: Iterator<Item = J>,
170-
J: Into<Self::Color>,
171-
{
172-
for item in iterator {
173-
let color: Self::Color = item.into();
174-
let word =
175-
(u32::from(color.g) << 24) | (u32::from(color.r) << 16) | (u32::from(color.b) << 8);
176-
177-
while !self.tx.write(word) {
178-
cortex_m::asm::nop();
179-
}
180-
}
181-
Ok(())
182-
}
183-
}
184-
185-
/// Instance of a WS2812 LED chain.
186-
///
187-
/// Use the [Ws2812::write] method to update the WS2812 LED chain.
188-
///
189-
/// Typical usage example:
190-
///```ignore
191-
/// use rp2040_hal::clocks::init_clocks_and_plls;
192-
/// let clocks = init_clocks_and_plls(...);
193-
/// let pins = rp2040_hal::gpio::pin::bank0::Pins::new(...);
194-
///
195-
/// let timer = Timer::new(pac.TIMER, &mut pac.RESETS);
196-
///
197-
/// let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
198-
/// let mut ws = Ws2812::new(
199-
/// pins.gpio4.into_mode(),
200-
/// &mut pio,
201-
/// sm0,
202-
/// clocks.peripheral_clock.freq(),
203-
/// timer.count_down(),
204-
/// );
205-
///
206-
/// loop {
207-
/// use smart_leds::{SmartLedsWrite, RGB8};
208-
/// let color : RGB8 = (255, 0, 255).into();
209-
///
210-
/// ws.write([color].iter().copied()).unwrap();
211-
///
212-
/// // Do other stuff here...
213-
/// };
214-
///```
215-
pub struct Ws2812<'timer, P, SM, I>
216-
where
217-
I: PinId,
218-
P: PIOExt + FunctionConfig,
219-
Function<P>: ValidPinMode<I>,
220-
SM: StateMachineIndex,
221-
{
222-
driver: Ws2812Direct<P, SM, I>,
223-
cd: CountDown<'timer>,
224-
}
150+
//impl<P, SM, I> SmartLedsWrite for Ws2812Direct<P, SM, I>
151+
//where
152+
// I: PinId,
153+
// P: PIOExt + FunctionConfig,
154+
// Function<P>: ValidPinMode<I>,
155+
// SM: StateMachineIndex,
156+
//{
157+
// type Color = smart_leds_trait::RGB8;
158+
// type Error = ();
159+
// /// If you call this function, be advised that you will have to wait
160+
// /// at least 60 microseconds between calls of this function!
161+
// /// That means, either you get hold on a timer and the timing
162+
// /// requirements right your self, or rather use [Ws2812].
163+
// ///
164+
// /// Please bear in mind, that it still blocks when writing into the
165+
// /// PIO FIFO until all data has been transmitted to the LED chain.
166+
// fn write<T, J>(&mut self, iterator: T) -> Result<(), ()>
167+
// where
168+
// T: Iterator<Item = J>,
169+
// J: Into<Self::Color>,
170+
// {
171+
// for item in iterator {
172+
// let color: Self::Color = item.into();
173+
// let word =
174+
// (u32::from(color.g) << 24) | (u32::from(color.r) << 16) | (u32::from(color.b) << 8);
175+
//
176+
// while !self.tx.write(word) {
177+
// cortex_m::asm::nop();
178+
// }
179+
// }
180+
// Ok(())
181+
// }
182+
//}
225183

226-
impl<'timer, P, SM, I> Ws2812<'timer, P, SM, I>
227-
where
228-
I: PinId,
229-
P: PIOExt + FunctionConfig,
230-
Function<P>: ValidPinMode<I>,
231-
SM: StateMachineIndex,
232-
{
233-
/// Creates a new instance of this driver.
234-
pub fn new(
235-
pin: Pin<I, Function<P>>,
236-
pio: &mut PIO<P>,
237-
sm: UninitStateMachine<(P, SM)>,
238-
clock_freq: fugit::HertzU32,
239-
cd: CountDown,
240-
) -> Ws2812<'timer, P, SM, I> {
241-
let driver = Ws2812Direct::new(pin, pio, sm, clock_freq);
242-
243-
Self { driver, cd }
244-
}
245-
}
246-
247-
impl<'timer, P, SM, I> SmartLedsWrite for Ws2812<'timer, P, SM, rp2040_hal::timer::CountDown<'timer>, I>
248-
where
249-
I: PinId,
250-
P: PIOExt + FunctionConfig,
251-
Function<P>: ValidPinMode<I>,
252-
SM: StateMachineIndex,
253-
{
254-
type Color = smart_leds_trait::RGB8;
255-
type Error = ();
256-
fn write<T, J>(&mut self, iterator: T) -> Result<(), ()>
257-
where
258-
T: Iterator<Item = J>,
259-
J: Into<Self::Color>,
260-
{
261-
self.driver.tx.clear_stalled_flag();
262-
while !self.driver.tx.is_empty() && !self.driver.tx.has_stalled() {}
263-
264-
self.cd.start(60u32.micros());
265-
let _ = nb::block!(self.cd.wait());
266-
267-
self.driver.write(iterator)
268-
}
269-
}
184+
///// Instance of a WS2812 LED chain.
185+
/////
186+
///// Use the [Ws2812::write] method to update the WS2812 LED chain.
187+
/////
188+
///// Typical usage example:
189+
/////```ignore
190+
///// use rp2040_hal::clocks::init_clocks_and_plls;
191+
///// let clocks = init_clocks_and_plls(...);
192+
///// let pins = rp2040_hal::gpio::pin::bank0::Pins::new(...);
193+
/////
194+
///// let timer = Timer::new(pac.TIMER, &mut pac.RESETS);
195+
/////
196+
///// let (mut pio, sm0, _, _, _) = pac.PIO0.split(&mut pac.RESETS);
197+
///// let mut ws = Ws2812::new(
198+
///// pins.gpio4.into_mode(),
199+
///// &mut pio,
200+
///// sm0,
201+
///// clocks.peripheral_clock.freq(),
202+
///// timer.count_down(),
203+
///// );
204+
/////
205+
///// loop {
206+
///// use smart_leds::{SmartLedsWrite, RGB8};
207+
///// let color : RGB8 = (255, 0, 255).into();
208+
/////
209+
///// ws.write([color].iter().copied()).unwrap();
210+
/////
211+
///// // Do other stuff here...
212+
///// };
213+
/////```
214+
//pub struct Ws2812<'timer, P, SM, I>
215+
//where
216+
// I: PinId,
217+
// P: PIOExt + FunctionConfig,
218+
// Function<P>: ValidPinMode<I>,
219+
// SM: StateMachineIndex,
220+
//{
221+
// driver: Ws2812Direct<P, SM, I>,
222+
// cd: CountDown<'timer>,
223+
//}
224+
//
225+
//impl<'timer, P, SM, I> Ws2812<'timer, P, SM, I>
226+
//where
227+
// I: PinId,
228+
// P: PIOExt + FunctionConfig,
229+
// Function<P>: ValidPinMode<I>,
230+
// SM: StateMachineIndex,
231+
//{
232+
// /// Creates a new instance of this driver.
233+
// pub fn new(
234+
// pin: Pin<I, Function<P>>,
235+
// pio: &mut PIO<P>,
236+
// sm: UninitStateMachine<(P, SM)>,
237+
// clock_freq: fugit::HertzU32,
238+
// cd: CountDown,
239+
// ) -> Ws2812<'timer, P, SM, I> {
240+
// let driver = Ws2812Direct::new(pin, pio, sm, clock_freq);
241+
//
242+
// Self { driver, cd }
243+
// }
244+
//}
245+
//
246+
//impl<'timer, P, SM, I> SmartLedsWrite for Ws2812<'timer, P, SM, rp2040_hal::timer::CountDown<'timer>, I>
247+
//where
248+
// I: PinId,
249+
// P: PIOExt + FunctionConfig,
250+
// Function<P>: ValidPinMode<I>,
251+
// SM: StateMachineIndex,
252+
//{
253+
// type Color = smart_leds_trait::RGB8;
254+
// type Error = ();
255+
// fn write<T, J>(&mut self, iterator: T) -> Result<(), ()>
256+
// where
257+
// T: Iterator<Item = J>,
258+
// J: Into<Self::Color>,
259+
// {
260+
// self.driver.tx.clear_stalled_flag();
261+
// while !self.driver.tx.is_empty() && !self.driver.tx.has_stalled() {}
262+
//
263+
// self.cd.start(60u32.micros());
264+
// let _ = nb::block!(self.cd.wait());
265+
//
266+
// self.driver.write(iterator)
267+
// }
268+
//}

0 commit comments

Comments
 (0)