Skip to content

Implement SmartLedsWriteAsync #4140

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 26 additions & 4 deletions embassy-rp/src/pio_programs/ws2812.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! [ws2812](https://www.sparkfun.com/datasheets/LCD/HD44780.pdf)

use core::convert::Infallible;

use embassy_time::Timer;
use fixed::types::U24F8;
use smart_leds::RGB8;
use smart_leds::{SmartLedsWriteAsync, RGB8};

use crate::clocks::clk_sys_freq;
use crate::dma::{AnyChannel, Channel};
Expand Down Expand Up @@ -97,11 +99,17 @@ impl<'d, P: Instance, const S: usize, const N: usize> PioWs2812<'d, P, S, N> {
}

/// Write a buffer of [smart_leds::RGB8] to the ws2812 string
pub async fn write(&mut self, colors: &[RGB8; N]) {
pub async fn write<T, I>(&mut self, iterator: T)
where
T: IntoIterator<Item = I>,
I: Into<RGB8>,
{
// Precompute the word bytes from the colors
let mut words = [0u32; N];
for i in 0..N {
let word = (u32::from(colors[i].g) << 24) | (u32::from(colors[i].r) << 16) | (u32::from(colors[i].b) << 8);

for (i, c) in iterator.into_iter().enumerate() {
let color = c.into();
let word = (u32::from(color.g) << 24) | (u32::from(color.r) << 16) | (u32::from(color.b) << 8);
words[i] = word;
}

Expand All @@ -111,3 +119,17 @@ impl<'d, P: Instance, const S: usize, const N: usize> PioWs2812<'d, P, S, N> {
Timer::after_micros(55).await;
}
}

impl<'d, P: Instance, const S: usize, const N: usize> SmartLedsWriteAsync for PioWs2812<'d, P, S, N> {
type Error = Infallible;
type Color = RGB8;

async fn write<T, I>(&mut self, iterator: T) -> Result<(), Self::Error>
where
T: IntoIterator<Item = I>,
I: Into<RGB8>,
{
self.write(iterator).await;
Ok(())
}
}
4 changes: 2 additions & 2 deletions examples/rp/src/bin/pio_ws2812.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async fn main(_spawner: Spawner) {
// Thing plus: 8
// Adafruit Feather: 16; Adafruit Feather+RFM95: 4
let program = PioWs2812Program::new(&mut common);
let mut ws2812 = PioWs2812::new(&mut common, sm0, p.DMA_CH0, p.PIN_16, &program);
let mut ws2812: PioWs2812<'_, PIO0, 0, NUM_LEDS> = PioWs2812::new(&mut common, sm0, p.DMA_CH0, p.PIN_16, &program);

// Loop forever making RGB values and pushing them out to the WS2812.
let mut ticker = Ticker::every(Duration::from_millis(10));
Expand All @@ -60,7 +60,7 @@ async fn main(_spawner: Spawner) {
data[i] = wheel((((i * 256) as u16 / NUM_LEDS as u16 + j as u16) & 255) as u8);
debug!("R: {} G: {} B: {}", data[i].r, data[i].g, data[i].b);
}
ws2812.write(&data).await;
ws2812.write(data.into_iter()).await;

ticker.next().await;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/rp235x/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ embedded-graphics = "0.8.1"
mipidsi = "0.8.0"
display-interface = "0.5.0"
byte-slice-cast = { version = "1.2.0", default-features = false }
smart-leds = "0.3.0"
smart-leds = "0.4.0"
heapless = "0.8"
usbd-hid = "0.8.1"

Expand Down
4 changes: 2 additions & 2 deletions examples/rp235x/src/bin/pio_ws2812.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async fn main(_spawner: Spawner) {
// Thing plus: 8
// Adafruit Feather: 16; Adafruit Feather+RFM95: 4
let program = PioWs2812Program::new(&mut common);
let mut ws2812 = PioWs2812::new(&mut common, sm0, p.DMA_CH0, p.PIN_16, &program);
let mut ws2812: PioWs2812<'_, PIO0, 0, NUM_LEDS> = PioWs2812::new(&mut common, sm0, p.DMA_CH0, p.PIN_16, &program);

// Loop forever making RGB values and pushing them out to the WS2812.
let mut ticker = Ticker::every(Duration::from_millis(10));
Expand All @@ -60,7 +60,7 @@ async fn main(_spawner: Spawner) {
data[i] = wheel((((i * 256) as u16 / NUM_LEDS as u16 + j as u16) & 255) as u8);
debug!("R: {} G: {} B: {}", data[i].r, data[i].g, data[i].b);
}
ws2812.write(&data).await;
ws2812.write(data.into_iter()).await;

ticker.next().await;
}
Expand Down