platform: smart_leds: rearchitect pixbufs to use color types that are closer to the hardware

This commit is contained in:
Victoria Fischer 2024-11-18 17:21:58 +01:00
parent e4b8863513
commit 5de628f3e0

View File

@ -12,8 +12,29 @@ use smart_leds::brightness;
use std::io;
use rgb::Rgb;
use std::ops::IndexMut;
pub struct SmartLedDisplay<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> {
pub trait HardwarePixel: Rgb8Blend + Clone + Copy + AsMilliwatts + Default + From<Rgb<u8>> {}
impl<T> HardwarePixel for T where T: Rgb8Blend + Clone + Copy + AsMilliwatts + Default + From<Rgb<u8>> {}
pub trait Pixbuf<T: HardwarePixel>: AsMilliwatts + IndexMut<usize, Output=T> {
fn new() -> Self;
fn blank(&mut self);
}
impl<T: HardwarePixel, const PIXEL_NUM: usize> Pixbuf<T> for [T; PIXEL_NUM] {
fn new() -> Self {
[T::default(); PIXEL_NUM]
}
fn blank(&mut self) {
self.fill(T::default())
}
}
pub struct SmartLedDisplay<T: SmartLedsWrite, S: Surface, const PIXEL_NUM: usize> where
T::Color: HardwarePixel,
[T::Color; PIXEL_NUM]: Pixbuf<T::Color> {
surfaces : Option<SurfacePool<S>>,
pixmap: StrideMapping,
target: T,
@ -21,7 +42,9 @@ pub struct SmartLedDisplay<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const
max_mw: u32
}
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> Debug for SmartLedDisplay<T, S, PIXEL_NUM> {
impl<T: SmartLedsWrite, S: Surface, const PIXEL_NUM: usize> Debug for SmartLedDisplay<T, S, PIXEL_NUM> where
T::Color: HardwarePixel,
[T::Color; PIXEL_NUM]: Pixbuf<T::Color> {
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
f.debug_struct("SmartLedDisplay")
.field("total_mw", &self.pixbuf.as_milliwatts())
@ -30,10 +53,12 @@ impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> Deb
}
}
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> SmartLedDisplay<T, S, PIXEL_NUM> {
impl<T: SmartLedsWrite, S: Surface, const PIXEL_NUM: usize> SmartLedDisplay<T, S, PIXEL_NUM> where
T::Color: HardwarePixel,
[T::Color; PIXEL_NUM]: Pixbuf<T::Color> {
pub fn new(target: T, max_mw: u32) -> Self {
SmartLedDisplay {
pixbuf: [Rgb::new(0, 0, 0); PIXEL_NUM],
pixbuf: <[T::Color; PIXEL_NUM]>::new(),
surfaces: Some(SurfacePool::new()),
target,
max_mw,
@ -42,16 +67,21 @@ impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> Sma
}
}
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> AsMilliwatts for SmartLedDisplay<T, S, PIXEL_NUM> {
impl<T, S, const PIXEL_NUM: usize> AsMilliwatts for SmartLedDisplay<T, S, PIXEL_NUM> where
T: SmartLedsWrite,
S: Surface,
T::Color: HardwarePixel,
[T::Color; PIXEL_NUM]: Pixbuf<T::Color> {
fn as_milliwatts(&self) -> u32 {
self.pixbuf.as_milliwatts()
}
}
impl<T, S, const PIXEL_NUM: usize> Surfaces<S> for SmartLedDisplay<T, S, PIXEL_NUM>
where
T: SmartLedsWrite<Color = Rgb<u8>>,
S: Surface {
impl<T, S, const PIXEL_NUM: usize> Surfaces<S> for SmartLedDisplay<T, S, PIXEL_NUM> where
T: SmartLedsWrite,
S: Surface,
T::Color: HardwarePixel,
[T::Color; PIXEL_NUM]: Pixbuf<T::Color> {
fn new_surface(&mut self, area: &Rectangle<u8, Virtual>) -> Result<S, io::Error> {
if let Some(ref mut s) = self.surfaces {
s.new_surface(area)
@ -61,15 +91,21 @@ S: Surface {
}
}
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> Display<S> for SmartLedDisplay<T, S, PIXEL_NUM> {
impl<T, S, const PIXEL_NUM: usize> Display<S> for SmartLedDisplay<T, S, PIXEL_NUM> where
T: SmartLedsWrite,
S: Surface,
T::Color: HardwarePixel,
[T::Color; PIXEL_NUM]: Pixbuf<T::Color>,
Rgb<u8>: From<T::Color> {
fn start_frame(&mut self) {
self.pixbuf.fill(Rgb::new(0, 0, 0));
self.pixbuf.blank();
}
fn end_frame(&mut self) {
let b = brightness_for_mw(self.pixbuf.as_milliwatts(), 255, self.max_mw);
if let Err(_) = self.target.write(brightness(self.pixbuf.iter().cloned(), b).map(|x| { rgb::Bgr::new(x.r, x.g, x.b)})) {
if let Err(_) = self.target.write(brightness(self.pixbuf.iter().cloned().map(|x| { x.into() }), b)) {
//if let Err(_) = self.target.write(self.pixbuf.iter().cloned()) {
panic!("Could not write frame");
}
}
@ -96,13 +132,20 @@ impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> Dis
#[cfg(feature="rmt")]
pub mod rmt {
use esp_idf_svc::hal::prelude::Peripherals;
use ws2812_esp32_rmt_driver::lib_smart_leds::Ws2812Esp32Rmt;
use ws2812_esp32_rmt_driver::lib_smart_leds::LedPixelEsp32Rmt;
use ws2812_esp32_rmt_driver::driver::color::LedPixelColor;
use crate::render::{Display, Surface};
use crate::platform::smart_leds_lib::SmartLedDisplay;
use crate::platform::smart_leds_lib::{Pixbuf, SmartLedDisplay, HardwarePixel};
use crate::platform::DisplayInit;
use rgb::Rgb;
impl DisplayInit for Ws2812Esp32Rmt<'_> {
impl<CSmart, CDev> DisplayInit for LedPixelEsp32Rmt<'_, CSmart, CDev> where
CSmart: HardwarePixel,
[CSmart; 300]: Pixbuf<CSmart>,
CDev: LedPixelColor + From<CSmart>,
Rgb<u8>: From<CSmart>
{
fn new_display<S: Surface>() -> impl Display<S> {
let peripherals = Peripherals::take().unwrap();
let led_pin = peripherals.pins.gpio14;
@ -113,7 +156,7 @@ pub mod rmt {
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
let target = Self::new(channel, led_pin).unwrap();
return SmartLedDisplay::<Ws2812Esp32Rmt, S, 300>::new(target, MAX_POWER_MW);
return SmartLedDisplay::<Self, S, 300>::new(target, MAX_POWER_MW);
}
}
}