This commit is contained in:
2023-06-12 17:39:07 +02:00
parent 59f91e2f6b
commit 9854e0c0a8
45 changed files with 4708 additions and 400 deletions

41
lib/src/lights.rs Normal file
View File

@ -0,0 +1,41 @@
use crate::ws2812::Ws2812;
use embassy_rp::pio;
use embassy_sync::mutex::Mutex;
use crate::{rgb::Rgb, util::CS};
pub struct Lights<P: pio::Instance + 'static, const N: usize> {
state: Mutex<CS, State<P, N>>,
}
struct State<P: pio::Instance + 'static, const N: usize> {
colors: [Rgb; N],
driver: Ws2812<P>,
}
impl<P: pio::Instance, const N: usize> Lights<P, N> {
pub const fn new(driver: Ws2812<P>) -> Self {
Lights {
state: Mutex::new(State {
colors: [Rgb::new(0, 0, 0); N],
driver,
}),
}
}
pub fn colors_mut(&mut self) -> &[Rgb; N] {
&self.state.get_mut().colors
}
/// Run a function to update the colors, and then immediately refresh the LEDs.
pub async fn update(&self, f: impl FnOnce(&mut [Rgb; N])) {
let State { colors, driver } = &mut *self.state.lock().await;
f(colors);
driver.write(colors).await
}
/// Update the LEDs with the currently set colors.
pub async fn refresh(&self) {
self.update(|_| ()).await;
}
}