renderbug/src/platform/smart_leds_lib.rs

249 lines
7.9 KiB
Rust
Raw Normal View History

use smart_leds_trait::SmartLedsWrite;
use crate::lib8::Rgb8Blend;
use crate::lib8::interpolate::Fract8Ops;
use crate::render::{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 std::io;
use rgb::Rgb;
use std::ops::IndexMut;
pub trait HardwarePixel: Send + Sync + Rgb8Blend + Copy + AsMilliwatts + Default + From<Rgb<u8>> + Fract8Ops {}
impl<T> HardwarePixel for T where T: Send + Sync + Rgb8Blend + Copy + AsMilliwatts + Default + From<Rgb<u8>> + Fract8Ops {}
pub trait Pixbuf: AsMilliwatts + IndexMut<usize, Output=Self::Pixel> {
type Pixel: HardwarePixel;
fn new() -> Self;
fn blank(&mut self);
fn iter_with_brightness(&self, brightness: u8) -> impl Iterator<Item = Self::Pixel> + Send;
}
impl<T: HardwarePixel, const PIXEL_NUM: usize> Pixbuf for [T; PIXEL_NUM] {
type Pixel = T;
fn new() -> Self {
[T::default(); PIXEL_NUM]
}
fn blank(&mut self) {
self.fill(T::default())
}
fn iter_with_brightness(&self, brightness: u8) -> impl Iterator<Item=T> + Send {
self.iter().map(move |x| { x.scale8(brightness)})
}
}
struct SmartLedDisplay<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> {
2024-11-16 12:13:49 +01:00
surfaces : Option<SurfacePool<S>>,
pixmap: StrideMapping,
target: T,
pixbuf: P,
max_mw: u32,
frame: usize
}
impl<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> Debug for SmartLedDisplay<T, S, P> {
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: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> SmartLedDisplay<T, S, P> {
fn new(target: T, max_mw: u32, pixmap: StrideMapping, pixbuf: P) -> Self {
SmartLedDisplay {
pixbuf,
2024-11-16 12:13:49 +01:00
surfaces: Some(SurfacePool::new()),
2024-11-16 12:41:10 +01:00
target,
max_mw,
pixmap,
frame: 0
}
}
}
impl<T, S, P> Surfaces<S> for SmartLedDisplay<T, S, P> where
T: FastWrite,
S: Surface,
P: Pixbuf<Pixel = T::Color> {
fn new_surface(&mut self, area: &Rectangle<Virtual>) -> Result<S, io::Error> {
2024-11-16 12:13:49 +01:00
if let Some(ref mut s) = self.surfaces {
s.new_surface(area)
} else {
panic!("Could not grab surface list")
}
}
}
impl<T, S, P> Display<S> for SmartLedDisplay<T, S, P> where
T: FastWrite,
S: Surface,
P: Pixbuf<Pixel = T::Color> {
fn start_frame(&mut self) {
self.pixbuf.blank();
}
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);
let opacity = surface.opacity();
if opacity > 0 {
surface.with_shader(|shader| {
while let Some((virt_coords, phys_coords)) = sel.next() {
let idx = self.pixmap.to_idx(&phys_coords);
self.pixbuf[idx] = self.pixbuf[idx].blend8(shader.draw(&virt_coords, self.frame).into(), opacity);
2024-11-16 12:13:49 +01:00
}
})
}
2024-11-16 12:13:49 +01:00
}
self.surfaces = Some(surfaces);
}
fn end_frame(&mut self) {
let b = brightness_for_mw(self.pixbuf.as_milliwatts(), 255, self.max_mw);
if let Err(_) = self.target.fast_write(self.pixbuf.iter_with_brightness(b)) {
panic!("Could not write frame!");
}
self.frame += 1;
}
}
trait FastWrite {
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;
}
#[cfg(feature="rmt")]
pub mod rmt {
use esp_idf_svc::hal::prelude::Peripherals;
use ws2812_esp32_rmt_driver::driver::color::LedPixelColorGrb24;
use smart_leds::SmartLedsWrite;
use rgb::Rgb;
use ws2812_esp32_rmt_driver::LedPixelEsp32Rmt;
use crate::mappings::StrideMapping;
use crate::render::{Display, Surface};
use crate::platform::DisplayInit;
use super::{Pixbuf, FastWrite, SmartLedDisplay};
pub type FastWs2812Esp32Rmt<'a> = LedPixelEsp32Rmt<'a, Rgb<u8>, LedPixelColorGrb24>;
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> {
let peripherals = Peripherals::take().unwrap();
let led_pin = peripherals.pins.gpio14;
//let led_pin = peripherals.pins.gpio5;
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 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();
return SmartLedDisplay::new(target, MAX_POWER_MW, pixmap, pixbuf);
}
}
}
#[cfg(feature="spi")]
pub mod spi {
use smart_leds::SmartLedsWrite;
use ws2812_spi::prerendered::Ws2812;
use crate::render::{Display, Surface};
use crate::platform::smart_leds_lib::SmartLedDisplay;
use crate::DisplayInit;
use super::FastWrite;
use esp_idf_svc::hal::{
prelude::*,
gpio::AnyIOPin,
spi::{
config::{Config, DriverConfig},
Dma,
SpiBusDriver,
SpiDriver,
}
};
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 {}
impl DisplayInit for SPIDisplay {
fn new_display<S: Surface>() -> impl Display<S> {
let peripherals = Peripherals::take().unwrap();
let driver = SpiDriver::new_without_sclk(
peripherals.spi2,
peripherals.pins.gpio5,
Option::<AnyIOPin>::None,
&DriverConfig::new().dma(Dma::Auto(4092))
).unwrap();
let cfg = Config::new().baudrate(3.MHz().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;
unsafe {
let target = Ws2812::new(spi, &mut STATIC_BUFFER);
return SmartLedDisplay::<Ws2812<SpiBusDriver<'_, SpiDriver<'_>>>, S, 310>::new(target, MAX_POWER_MW);
}
}
}
}