platform: smart_leds: simplify trait hierarchy and push pixel type conversion even closer towards the hardware

This commit is contained in:
Victoria Fischer 2024-11-24 23:53:43 +01:00
parent a76d040ff6
commit 04f5575ba6
2 changed files with 138 additions and 68 deletions

View File

@ -19,7 +19,7 @@ use crate::platform::embedded_graphics_lib::PonderjarTarget as DisplayType;
#[cfg(feature="smart-leds")] #[cfg(feature="smart-leds")]
#[cfg(feature="rmt")] #[cfg(feature="rmt")]
use ws2812_esp32_rmt_driver::lib_smart_leds::Ws2812Esp32Rmt as DisplayType; use crate::platform::smart_leds_lib::rmt::FastWs2812Esp32Rmt as DisplayType;
#[cfg(feature="smart-leds")] #[cfg(feature="smart-leds")]
#[cfg(feature="spi")] #[cfg(feature="spi")]

View File

@ -1,6 +1,7 @@
use smart_leds_trait::SmartLedsWrite; use smart_leds_trait::SmartLedsWrite;
use crate::lib8::Rgb8Blend; use crate::lib8::Rgb8Blend;
use crate::lib8::interpolate::Fract8Ops;
use crate::render::{Framed, Surface, Display, Surfaces}; use crate::render::{Framed, Surface, Display, Surfaces};
use crate::buffers::SurfacePool; use crate::buffers::SurfacePool;
use crate::power::{brightness_for_mw, AsMilliwatts}; use crate::power::{brightness_for_mw, AsMilliwatts};
@ -8,19 +9,18 @@ use crate::geometry::*;
use crate::mappings::*; use crate::mappings::*;
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use smart_leds::brightness;
use std::io; use std::io;
use rgb::Rgb; use rgb::Rgb;
use std::ops::IndexMut; use std::ops::IndexMut;
pub trait HardwarePixel: Rgb8Blend + Clone + Copy + AsMilliwatts + Default + From<Rgb<u8>> {} pub trait HardwarePixel: Send + Sync + Rgb8Blend + Copy + AsMilliwatts + Default + From<Rgb<u8>> + Fract8Ops {}
impl<T> HardwarePixel for T where T: Rgb8Blend + Clone + Copy + AsMilliwatts + Default + From<Rgb<u8>> {} impl<T> HardwarePixel for T where T: Send + Sync + Rgb8Blend + Copy + AsMilliwatts + Default + From<Rgb<u8>> + Fract8Ops {}
pub trait Pixbuf<T: HardwarePixel>: AsMilliwatts + IndexMut<usize, Output=T> { pub trait Pixbuf<T: HardwarePixel>: AsMilliwatts + IndexMut<usize, Output=T> {
fn new() -> Self; fn new() -> Self;
fn blank(&mut self); fn blank(&mut self);
fn iter_with_brightness(&self, brightness: u8) -> impl Iterator<Item = T> + Send;
} }
impl<T: HardwarePixel, const PIXEL_NUM: usize> Pixbuf<T> for [T; PIXEL_NUM] { impl<T: HardwarePixel, const PIXEL_NUM: usize> Pixbuf<T> for [T; PIXEL_NUM] {
@ -31,22 +31,22 @@ impl<T: HardwarePixel, const PIXEL_NUM: usize> Pixbuf<T> for [T; PIXEL_NUM] {
fn blank(&mut self) { fn blank(&mut self) {
self.fill(T::default()) self.fill(T::default())
} }
fn iter_with_brightness(&self, brightness: u8) -> impl Iterator<Item=T> + Send {
self.iter().map(move |x| { x.scale8(brightness)})
}
} }
pub struct SmartLedDisplay<T: SmartLedsWrite, S: Surface, const PIXEL_NUM: usize> where struct SmartLedDisplay<T: FastWrite, S: Surface, P: Pixbuf<T::Color>> {
T::Color: HardwarePixel,
[T::Color; PIXEL_NUM]: Pixbuf<T::Color> {
surfaces : Option<SurfacePool<S>>, surfaces : Option<SurfacePool<S>>,
pixmap: StrideMapping, pixmap: StrideMapping,
target: T, target: T,
pixbuf: [T::Color; PIXEL_NUM], pixbuf: P,
max_mw: u32, max_mw: u32,
frame: usize frame: usize
} }
impl<T: SmartLedsWrite, S: Surface, const PIXEL_NUM: usize> Debug for SmartLedDisplay<T, S, PIXEL_NUM> where impl<T: FastWrite, S: Surface, P: Pixbuf<T::Color>> Debug for SmartLedDisplay<T, S, P> {
T::Color: HardwarePixel,
[T::Color; PIXEL_NUM]: Pixbuf<T::Color> {
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> { fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
f.debug_struct("SmartLedDisplay") f.debug_struct("SmartLedDisplay")
.field("total_mw", &self.pixbuf.as_milliwatts()) .field("total_mw", &self.pixbuf.as_milliwatts())
@ -55,36 +55,32 @@ T::Color: HardwarePixel,
} }
} }
impl<T: SmartLedsWrite, S: Surface, const PIXEL_NUM: usize> SmartLedDisplay<T, S, PIXEL_NUM> where impl<T: FastWrite, S: Surface, P: Pixbuf<T::Color>> SmartLedDisplay<T, S, P> {
T::Color: HardwarePixel, fn new(target: T, max_mw: u32, pixmap: StrideMapping, pixbuf: P) -> Self {
[T::Color; PIXEL_NUM]: Pixbuf<T::Color> {
pub fn new(target: T, max_mw: u32) -> Self {
SmartLedDisplay { SmartLedDisplay {
pixbuf: <[T::Color; PIXEL_NUM]>::new(), pixbuf,
surfaces: Some(SurfacePool::new()), surfaces: Some(SurfacePool::new()),
target, target,
max_mw, max_mw,
pixmap: StrideMapping::new(), pixmap,
frame: 0 frame: 0
} }
} }
} }
impl<T, S, const PIXEL_NUM: usize> AsMilliwatts for SmartLedDisplay<T, S, PIXEL_NUM> where impl<T, S, P> AsMilliwatts for SmartLedDisplay<T, S, P> where
T: SmartLedsWrite, T: FastWrite,
S: Surface, S: Surface,
T::Color: HardwarePixel, P: Pixbuf<T::Color> {
[T::Color; PIXEL_NUM]: Pixbuf<T::Color> {
fn as_milliwatts(&self) -> u32 { fn as_milliwatts(&self) -> u32 {
self.pixbuf.as_milliwatts() self.pixbuf.as_milliwatts()
} }
} }
impl<T, S, const PIXEL_NUM: usize> Surfaces<S> for SmartLedDisplay<T, S, PIXEL_NUM> where impl<T, S, P> Surfaces<S> for SmartLedDisplay<T, S, P> where
T: SmartLedsWrite, T: FastWrite,
S: Surface, S: Surface,
T::Color: HardwarePixel, P: Pixbuf<T::Color> {
[T::Color; PIXEL_NUM]: Pixbuf<T::Color> {
fn new_surface(&mut self, area: &Rectangle<u8, Virtual>) -> Result<S, io::Error> { fn new_surface(&mut self, area: &Rectangle<u8, Virtual>) -> Result<S, io::Error> {
if let Some(ref mut s) = self.surfaces { if let Some(ref mut s) = self.surfaces {
s.new_surface(area) s.new_surface(area)
@ -94,38 +90,43 @@ T::Color: HardwarePixel,
} }
} }
impl<T, S, const PIXEL_NUM: usize> Display<S> for SmartLedDisplay<T, S, PIXEL_NUM> where impl<T, S, P> Display<S> for SmartLedDisplay<T, S, P> where
T: SmartLedsWrite, T: FastWrite,
S: Surface, S: Surface,
T::Color: HardwarePixel, P: Pixbuf<T::Color> {
[T::Color; PIXEL_NUM]: Pixbuf<T::Color>,
Rgb<u8>: From<T::Color>
{
fn render_frame(&mut self) { fn render_frame(&mut self) {
let surfaces = self.surfaces.take().unwrap(); let surfaces = self.surfaces.take().unwrap();
for surface in surfaces.iter() { for surface in surfaces.iter() {
let rect = surface.rect().clone(); let rect = surface.rect().clone();
let mut sel = self.pixmap.select(&rect); let mut sel = self.pixmap.select(&rect);
surface.with_shader(|shader| { let opacity = surface.opacity();
while let Some((virt_coords, phys_coords)) = sel.next() { if opacity > 0 {
let idx = phys_coords.x as usize; surface.with_shader(|shader| {
if idx >= PIXEL_NUM { while let Some((virt_coords, phys_coords)) = sel.next() {
continue; let idx = self.pixmap.to_idx(&phys_coords);
self.pixbuf[idx] = self.pixbuf[idx].blend8(shader.draw(&virt_coords, self.frame).into(), opacity);
} }
self.pixbuf[idx] = self.pixbuf[idx].saturating_add(shader.draw(&virt_coords, self.frame)); })
} }
})
} }
self.surfaces = Some(surfaces); self.surfaces = Some(surfaces);
} }
} }
impl<T, S, const PIXEL_NUM: usize> Framed for SmartLedDisplay<T, S, PIXEL_NUM> where trait FastWrite {
T: SmartLedsWrite, type Target: SmartLedsWrite;
type Color: HardwarePixel;
type Error;
fn fast_write<T, I>(&mut self, iterator: T) -> Result<(), Self::Error> where
T: IntoIterator<Item = I>,
I: Into<Self::Color>,
<T as IntoIterator>::IntoIter: Send;
}
impl<T, S, P> Framed for SmartLedDisplay<T, S, P> where
T: FastWrite,
S: Surface, S: Surface,
T::Color: HardwarePixel, P: Pixbuf<T::Color> {
[T::Color; PIXEL_NUM]: Pixbuf<T::Color>,
Rgb<u8>: From<T::Color> {
fn start_frame(&mut self) { fn start_frame(&mut self) {
self.pixbuf.blank(); self.pixbuf.blank();
@ -133,8 +134,8 @@ Rgb<u8>: From<T::Color> {
fn end_frame(&mut self) { fn end_frame(&mut self) {
let b = brightness_for_mw(self.pixbuf.as_milliwatts(), 255, self.max_mw); let b = brightness_for_mw(self.pixbuf.as_milliwatts(), 255, self.max_mw);
if let Err(_) = self.target.write(brightness(self.pixbuf.iter().cloned().map(|x| { x.into() }), b)) { if let Err(_) = self.target.fast_write(self.pixbuf.iter_with_brightness(b)) {
panic!("Could not write frame"); panic!("Could not write frame!");
} }
self.frame += 1; self.frame += 1;
} }
@ -143,43 +144,90 @@ Rgb<u8>: From<T::Color> {
#[cfg(feature="rmt")] #[cfg(feature="rmt")]
pub mod rmt { pub mod rmt {
use esp_idf_svc::hal::prelude::Peripherals; use esp_idf_svc::hal::prelude::Peripherals;
use ws2812_esp32_rmt_driver::lib_smart_leds::LedPixelEsp32Rmt; use ws2812_esp32_rmt_driver::driver::color::{LedPixelColor, LedPixelColorGrb24};
use ws2812_esp32_rmt_driver::driver::color::LedPixelColor; use smart_leds::SmartLedsWrite;
use crate::render::{Display, Surface};
use crate::platform::smart_leds_lib::{Pixbuf, SmartLedDisplay, HardwarePixel};
use crate::platform::DisplayInit;
use rgb::Rgb; use rgb::Rgb;
use ws2812_esp32_rmt_driver::LedPixelEsp32Rmt;
impl<CSmart, CDev> DisplayInit for LedPixelEsp32Rmt<'_, CSmart, CDev> where use crate::mappings::StrideMapping;
CSmart: HardwarePixel, use crate::render::{Display, Surface};
[CSmart; 310]: Pixbuf<CSmart>, use crate::platform::DisplayInit;
CDev: LedPixelColor + From<CSmart>,
Rgb<u8>: From<CSmart> use super::{Pixbuf, FastWrite, SmartLedDisplay};
{
use crate::lib8::Rgb8Blend;
use crate::lib8::interpolate::{Fract8, Fract8Ops};
use crate::power::AsMilliwatts;
pub type FastWs2812Esp32Rmt<'a> = LedPixelEsp32Rmt<'a, Rgb<u8>, LedPixelColorGrb24>;
impl Fract8Ops for LedPixelColorGrb24 {
fn blend8(self, other: Self, scale: Fract8) -> Self {
self
}
fn scale8(self, scale: Fract8) -> Self {
self
}
}
impl AsMilliwatts for LedPixelColorGrb24 {
fn as_milliwatts(&self) -> u32 {
Rgb::new(self.r(), self.g(), self.b()).as_milliwatts()
}
}
impl Rgb8Blend for LedPixelColorGrb24 {
fn saturating_add<T: Into<Self>>(self, b: T) -> Self where Self: Sized {
let s = b.into();
LedPixelColorGrb24::new_with_rgb(s.r(), s.g(), s.b())
}
}
impl FastWrite for FastWs2812Esp32Rmt<'_> {
type Color = <Self as SmartLedsWrite>::Color;
type Error = <Self as SmartLedsWrite>::Error;
type Target = Self;
fn fast_write<T, I>(&mut self, iterator: T) -> Result<(), Self::Error> where
T: IntoIterator<Item = I>,
I: Into<Self::Color>,
<T as IntoIterator>::IntoIter: Send {
self.write_nocopy(iterator)
}
}
impl DisplayInit for FastWs2812Esp32Rmt<'_> {
fn new_display<S: Surface>() -> impl Display<S> { fn new_display<S: Surface>() -> impl Display<S> {
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take().unwrap();
let led_pin = peripherals.pins.gpio14; let led_pin = peripherals.pins.gpio14;
//let led_pin = peripherals.pins.gpio5;
let channel = peripherals.rmt.channel0; let channel = peripherals.rmt.channel0;
const POWER_VOLTS : u32 = 5; const POWER_VOLTS : u32 = 5;
const POWER_MA : u32 = 500; const POWER_MA : u32 = 500;
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA; const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
//let pixbuf: [Rgb<u8>; 310] = Pixbuf::new();
let pixbuf: [<Self as FastWrite>::Color; 310] = Pixbuf::new();
let pixmap = StrideMapping::new_jar();
assert!(pixmap.pixel_count <= pixbuf.len(), "map needs {} pixels, I only have PIXEL_NUM={}", pixmap.pixel_count, pixbuf.len());
let target = Self::new(channel, led_pin).unwrap(); let target = Self::new(channel, led_pin).unwrap();
return SmartLedDisplay::<Self, S, 310>::new(target, MAX_POWER_MW); return SmartLedDisplay::new(target, MAX_POWER_MW, pixmap, pixbuf);
} }
} }
} }
#[cfg(feature="spi")] #[cfg(feature="spi")]
pub mod spi { pub mod spi {
use ws2812_spi::Ws2812; use smart_leds::SmartLedsWrite;
use ws2812_spi::prerendered::Ws2812;
use crate::render::{Display, Surface}; use crate::render::{Display, Surface};
use crate::task::Task;
use crate::platform::smart_leds_lib::SmartLedDisplay; use crate::platform::smart_leds_lib::SmartLedDisplay;
use crate::DisplayInit; use crate::DisplayInit;
use super::FastWrite;
use esp_idf_svc::hal::{ use esp_idf_svc::hal::{
prelude::*, prelude::*,
@ -192,27 +240,49 @@ pub mod spi {
} }
}; };
impl<'a> FastWrite for Ws2812<'_, SpiBusDriver<'a, SpiDriver<'a>>> {
type Color = <Self as SmartLedsWrite>::Color;
type Error = <Self as SmartLedsWrite>::Error;
type Target = Self;
fn fast_write<T, I>(&mut self, iterator: T) -> Result<(), Self::Error> where
T: IntoIterator<Item = I>,
I: Into<Self::Color>,
<T as IntoIterator>::IntoIter: Send {
let resp = self.write(iterator);
if let Err(e) = resp {
panic!("Could not write SPI frame! {:?}", e)
} else {
resp
}
}
}
static mut STATIC_BUFFER: [u8; 400 * 12] = [0; 400 * 12];
pub struct SPIDisplay {} pub struct SPIDisplay {}
impl DisplayInit for SPIDisplay { impl DisplayInit for SPIDisplay {
fn new_display<S: Surface>() -> impl Display<S> + Task { fn new_display<S: Surface>() -> impl Display<S> {
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take().unwrap();
let driver = SpiDriver::new_without_sclk( let driver = SpiDriver::new_without_sclk(
peripherals.spi2, peripherals.spi2,
peripherals.pins.gpio14, peripherals.pins.gpio5,
Option::<AnyIOPin>::None, Option::<AnyIOPin>::None,
&DriverConfig::new().dma(Dma::Auto(512)) &DriverConfig::new().dma(Dma::Auto(4092))
).unwrap(); ).unwrap();
let cfg = Config::new().baudrate(3_200.kHz().into()); let cfg = Config::new().baudrate(3.MHz().into());
let spi = SpiBusDriver::new(driver, &cfg).unwrap(); let spi = SpiBusDriver::new(driver, &cfg).unwrap();
const POWER_VOLTS : u32 = 5; const POWER_VOLTS : u32 = 5;
const POWER_MA : u32 = 500; const POWER_MA : u32 = 500;
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA; const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
let target = Ws2812::new(spi); unsafe {
return SmartLedDisplay::new(target, MAX_POWER_MW) let target = Ws2812::new(spi, &mut STATIC_BUFFER);
return SmartLedDisplay::<Ws2812<SpiBusDriver<'_, SpiDriver<'_>>>, S, 310>::new(target, MAX_POWER_MW);
}
} }
} }
} }