renderbug/src/platform/smart_leds_lib.rs

219 lines
6.6 KiB
Rust
Raw Normal View History

use smart_leds_trait::SmartLedsWrite;
use crate::lib8::Rgb8Blend;
use crate::render::{Framed, Surface, Display, Surfaces};
use crate::buffers::SurfacePool;
use crate::power::{brightness_for_mw, AsMilliwatts};
use crate::geometry::*;
2024-11-16 12:13:49 +01:00
use crate::mappings::*;
use std::fmt::{Debug, Formatter};
use smart_leds::brightness;
use std::io;
use rgb::Rgb;
use std::ops::IndexMut;
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> {
2024-11-16 12:13:49 +01:00
surfaces : Option<SurfacePool<S>>,
pixmap: StrideMapping,
target: T,
pixbuf: [T::Color; PIXEL_NUM],
max_mw: u32,
frame: usize
}
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())
.field("surfaces", &self.surfaces)
.finish()
}
}
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: <[T::Color; PIXEL_NUM]>::new(),
2024-11-16 12:13:49 +01:00
surfaces: Some(SurfacePool::new()),
2024-11-16 12:41:10 +01:00
target,
max_mw,
pixmap: StrideMapping::new(),
frame: 0
}
}
}
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,
S: Surface,
T::Color: HardwarePixel,
[T::Color; PIXEL_NUM]: Pixbuf<T::Color> {
2024-11-16 12:13:49 +01:00
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)
} else {
panic!("Could not grab surface list")
}
}
}
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 render_frame(&mut self) {
2024-11-16 12:13:49 +01:00
let surfaces = self.surfaces.take().unwrap();
for surface in surfaces.iter() {
let rect = surface.rect().clone();
let mut sel = self.pixmap.select(&rect);
surface.with_shader(|shader| {
while let Some((virt_coords, phys_coords)) = sel.next() {
let idx = phys_coords.x as usize;
if idx >= PIXEL_NUM {
continue;
}
self.pixbuf[idx] = self.pixbuf[idx].saturating_add(shader.draw(&virt_coords, self.frame));
2024-11-16 12:13:49 +01:00
}
})
}
self.surfaces = Some(surfaces);
}
}
impl<T, S, const PIXEL_NUM: usize> Framed 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.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().map(|x| { x.into() }), b)) {
panic!("Could not write frame");
}
self.frame += 1;
}
}
#[cfg(feature="rmt")]
pub mod rmt {
use esp_idf_svc::hal::prelude::Peripherals;
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::{Pixbuf, SmartLedDisplay, HardwarePixel};
use crate::platform::DisplayInit;
use rgb::Rgb;
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;
let channel = peripherals.rmt.channel0;
const POWER_VOLTS : u32 = 5;
const POWER_MA : u32 = 500;
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
let target = Self::new(channel, led_pin).unwrap();
return SmartLedDisplay::<Self, S, 300>::new(target, MAX_POWER_MW);
}
}
}
#[cfg(feature="spi")]
pub mod spi {
use ws2812_spi::Ws2812;
use crate::render::{Display, Surface};
use crate::task::Task;
use crate::platform::smart_leds_lib::SmartLedDisplay;
use crate::DisplayInit;
use esp_idf_svc::hal::{
prelude::*,
gpio::AnyIOPin,
spi::{
config::{Config, DriverConfig},
Dma,
SpiBusDriver,
SpiDriver,
}
};
pub struct SPIDisplay {}
impl DisplayInit for SPIDisplay {
fn new_display<S: Surface>() -> impl Display<S> + Task {
let peripherals = Peripherals::take().unwrap();
let driver = SpiDriver::new_without_sclk(
peripherals.spi2,
peripherals.pins.gpio14,
Option::<AnyIOPin>::None,
&DriverConfig::new().dma(Dma::Auto(512))
).unwrap();
let cfg = Config::new().baudrate(3_200.kHz().into());
let spi = SpiBusDriver::new(driver, &cfg).unwrap();
const POWER_VOLTS : u32 = 5;
const POWER_MA : u32 = 500;
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
let target = Ws2812::new(spi);
return SmartLedDisplay::new(target, MAX_POWER_MW)
}
}
}