2024-10-29 01:27:20 +01:00
|
|
|
use smart_leds_trait::SmartLedsWrite;
|
|
|
|
|
2024-10-30 21:55:38 +01:00
|
|
|
use crate::lib8::Rgb8Blend;
|
2024-11-24 23:53:43 +01:00
|
|
|
use crate::lib8::interpolate::Fract8Ops;
|
2024-11-18 23:48:20 +01:00
|
|
|
use crate::render::{Framed, Surface, Display, Surfaces};
|
|
|
|
use crate::buffers::SurfacePool;
|
2024-10-30 19:52:02 +01:00
|
|
|
use crate::power::{brightness_for_mw, AsMilliwatts};
|
2024-10-29 01:27:20 +01:00
|
|
|
use crate::geometry::*;
|
2024-11-16 12:13:49 +01:00
|
|
|
use crate::mappings::*;
|
2024-11-02 15:22:37 +01:00
|
|
|
use std::fmt::{Debug, Formatter};
|
2024-10-29 01:27:20 +01:00
|
|
|
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
use rgb::Rgb;
|
2024-11-18 17:21:58 +01:00
|
|
|
use std::ops::IndexMut;
|
2024-10-29 01:27:20 +01:00
|
|
|
|
2024-11-24 23:53:43 +01:00
|
|
|
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 {}
|
2024-11-18 17:21:58 +01:00
|
|
|
|
|
|
|
pub trait Pixbuf<T: HardwarePixel>: AsMilliwatts + IndexMut<usize, Output=T> {
|
|
|
|
fn new() -> Self;
|
|
|
|
fn blank(&mut self);
|
2024-11-24 23:53:43 +01:00
|
|
|
fn iter_with_brightness(&self, brightness: u8) -> impl Iterator<Item = T> + Send;
|
2024-11-18 17:21:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
2024-11-24 23:53:43 +01:00
|
|
|
|
|
|
|
fn iter_with_brightness(&self, brightness: u8) -> impl Iterator<Item=T> + Send {
|
|
|
|
self.iter().map(move |x| { x.scale8(brightness)})
|
|
|
|
}
|
2024-11-18 17:21:58 +01:00
|
|
|
}
|
|
|
|
|
2024-11-24 23:53:43 +01:00
|
|
|
struct SmartLedDisplay<T: FastWrite, S: Surface, P: Pixbuf<T::Color>> {
|
2024-11-16 12:13:49 +01:00
|
|
|
surfaces : Option<SurfacePool<S>>,
|
|
|
|
pixmap: StrideMapping,
|
2024-10-29 01:27:20 +01:00
|
|
|
target: T,
|
2024-11-24 23:53:43 +01:00
|
|
|
pixbuf: P,
|
2024-11-23 14:50:28 +01:00
|
|
|
max_mw: u32,
|
|
|
|
frame: usize
|
2024-10-29 01:27:20 +01:00
|
|
|
}
|
|
|
|
|
2024-11-24 23:53:43 +01:00
|
|
|
impl<T: FastWrite, S: Surface, P: Pixbuf<T::Color>> Debug for SmartLedDisplay<T, S, P> {
|
2024-11-02 15:22:37 +01:00
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
|
|
|
|
f.debug_struct("SmartLedDisplay")
|
2024-11-16 12:06:00 +01:00
|
|
|
.field("total_mw", &self.pixbuf.as_milliwatts())
|
2024-11-02 15:22:37 +01:00
|
|
|
.field("surfaces", &self.surfaces)
|
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-24 23:53:43 +01:00
|
|
|
impl<T: FastWrite, S: Surface, P: Pixbuf<T::Color>> SmartLedDisplay<T, S, P> {
|
|
|
|
fn new(target: T, max_mw: u32, pixmap: StrideMapping, pixbuf: P) -> Self {
|
2024-10-29 01:27:20 +01:00
|
|
|
SmartLedDisplay {
|
2024-11-24 23:53:43 +01:00
|
|
|
pixbuf,
|
2024-11-16 12:13:49 +01:00
|
|
|
surfaces: Some(SurfacePool::new()),
|
2024-11-16 12:41:10 +01:00
|
|
|
target,
|
|
|
|
max_mw,
|
2024-11-24 23:53:43 +01:00
|
|
|
pixmap,
|
2024-11-23 14:50:28 +01:00
|
|
|
frame: 0
|
2024-10-29 01:27:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-24 23:53:43 +01:00
|
|
|
impl<T, S, P> AsMilliwatts for SmartLedDisplay<T, S, P> where
|
|
|
|
T: FastWrite,
|
2024-11-18 17:21:58 +01:00
|
|
|
S: Surface,
|
2024-11-24 23:53:43 +01:00
|
|
|
P: Pixbuf<T::Color> {
|
2024-11-02 15:22:37 +01:00
|
|
|
fn as_milliwatts(&self) -> u32 {
|
2024-11-16 12:06:00 +01:00
|
|
|
self.pixbuf.as_milliwatts()
|
2024-10-29 01:27:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-24 23:53:43 +01:00
|
|
|
impl<T, S, P> Surfaces<S> for SmartLedDisplay<T, S, P> where
|
|
|
|
T: FastWrite,
|
2024-11-18 17:21:58 +01:00
|
|
|
S: Surface,
|
2024-11-24 23:53:43 +01:00
|
|
|
P: 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")
|
|
|
|
}
|
2024-10-29 01:27:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-24 23:53:43 +01:00
|
|
|
impl<T, S, P> Display<S> for SmartLedDisplay<T, S, P> where
|
|
|
|
T: FastWrite,
|
2024-11-18 17:21:58 +01:00
|
|
|
S: Surface,
|
2024-11-24 23:53:43 +01:00
|
|
|
P: Pixbuf<T::Color> {
|
2024-10-29 01:27:20 +01:00
|
|
|
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);
|
2024-11-24 23:53:43 +01:00
|
|
|
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-24 23:53:43 +01:00
|
|
|
})
|
|
|
|
}
|
2024-11-16 12:13:49 +01:00
|
|
|
}
|
|
|
|
self.surfaces = Some(surfaces);
|
2024-10-29 01:27:20 +01:00
|
|
|
}
|
|
|
|
}
|
2024-10-30 19:52:02 +01:00
|
|
|
|
2024-11-24 23:53:43 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, S, P> Framed for SmartLedDisplay<T, S, P> where
|
|
|
|
T: FastWrite,
|
2024-11-18 23:48:20 +01:00
|
|
|
S: Surface,
|
2024-11-24 23:53:43 +01:00
|
|
|
P: Pixbuf<T::Color> {
|
2024-11-18 23:48:20 +01:00
|
|
|
|
|
|
|
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);
|
2024-11-24 23:53:43 +01:00
|
|
|
if let Err(_) = self.target.fast_write(self.pixbuf.iter_with_brightness(b)) {
|
|
|
|
panic!("Could not write frame!");
|
2024-11-18 23:48:20 +01:00
|
|
|
}
|
2024-11-23 14:50:28 +01:00
|
|
|
self.frame += 1;
|
2024-11-18 23:48:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-30 19:52:02 +01:00
|
|
|
#[cfg(feature="rmt")]
|
|
|
|
pub mod rmt {
|
|
|
|
use esp_idf_svc::hal::prelude::Peripherals;
|
2024-11-24 23:53:43 +01:00
|
|
|
use ws2812_esp32_rmt_driver::driver::color::{LedPixelColor, LedPixelColorGrb24};
|
|
|
|
use smart_leds::SmartLedsWrite;
|
|
|
|
use rgb::Rgb;
|
|
|
|
use ws2812_esp32_rmt_driver::LedPixelEsp32Rmt;
|
2024-10-30 19:52:02 +01:00
|
|
|
|
2024-11-24 23:53:43 +01:00
|
|
|
use crate::mappings::StrideMapping;
|
2024-10-30 19:52:02 +01:00
|
|
|
use crate::render::{Display, Surface};
|
|
|
|
use crate::platform::DisplayInit;
|
2024-11-18 17:21:58 +01:00
|
|
|
|
2024-11-24 23:53:43 +01:00
|
|
|
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 {
|
2024-11-28 19:38:44 +01:00
|
|
|
fn blend8(self, _other: Self, _scale: Fract8) -> Self {
|
2024-11-24 23:53:43 +01:00
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2024-11-28 19:38:44 +01:00
|
|
|
fn scale8(self, _scale: Fract8) -> Self {
|
2024-11-24 23:53:43 +01:00
|
|
|
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<'_> {
|
2024-11-02 15:22:37 +01:00
|
|
|
fn new_display<S: Surface>() -> impl Display<S> {
|
2024-10-30 19:52:02 +01:00
|
|
|
let peripherals = Peripherals::take().unwrap();
|
|
|
|
let led_pin = peripherals.pins.gpio14;
|
2024-11-24 23:53:43 +01:00
|
|
|
//let led_pin = peripherals.pins.gpio5;
|
2024-10-30 19:52:02 +01:00
|
|
|
let channel = peripherals.rmt.channel0;
|
|
|
|
|
|
|
|
const POWER_VOLTS : u32 = 5;
|
|
|
|
const POWER_MA : u32 = 500;
|
|
|
|
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
|
|
|
|
|
2024-11-24 23:53:43 +01:00
|
|
|
//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());
|
|
|
|
|
2024-10-30 19:52:02 +01:00
|
|
|
let target = Self::new(channel, led_pin).unwrap();
|
2024-11-24 23:53:43 +01:00
|
|
|
return SmartLedDisplay::new(target, MAX_POWER_MW, pixmap, pixbuf);
|
2024-10-30 19:52:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature="spi")]
|
|
|
|
pub mod spi {
|
2024-11-24 23:53:43 +01:00
|
|
|
use smart_leds::SmartLedsWrite;
|
|
|
|
use ws2812_spi::prerendered::Ws2812;
|
2024-10-30 19:52:02 +01:00
|
|
|
|
|
|
|
use crate::render::{Display, Surface};
|
2024-10-30 20:54:49 +01:00
|
|
|
use crate::platform::smart_leds_lib::SmartLedDisplay;
|
2024-10-30 21:56:03 +01:00
|
|
|
use crate::DisplayInit;
|
2024-11-24 23:53:43 +01:00
|
|
|
use super::FastWrite;
|
2024-10-30 19:52:02 +01:00
|
|
|
|
|
|
|
use esp_idf_svc::hal::{
|
|
|
|
prelude::*,
|
|
|
|
gpio::AnyIOPin,
|
|
|
|
spi::{
|
|
|
|
config::{Config, DriverConfig},
|
|
|
|
Dma,
|
|
|
|
SpiBusDriver,
|
|
|
|
SpiDriver,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-11-24 23:53:43 +01:00
|
|
|
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];
|
|
|
|
|
2024-10-30 19:52:02 +01:00
|
|
|
pub struct SPIDisplay {}
|
|
|
|
impl DisplayInit for SPIDisplay {
|
2024-11-24 23:53:43 +01:00
|
|
|
fn new_display<S: Surface>() -> impl Display<S> {
|
2024-10-30 19:52:02 +01:00
|
|
|
let peripherals = Peripherals::take().unwrap();
|
|
|
|
|
|
|
|
let driver = SpiDriver::new_without_sclk(
|
|
|
|
peripherals.spi2,
|
2024-11-24 23:53:43 +01:00
|
|
|
peripherals.pins.gpio5,
|
2024-10-30 19:52:02 +01:00
|
|
|
Option::<AnyIOPin>::None,
|
2024-11-24 23:53:43 +01:00
|
|
|
&DriverConfig::new().dma(Dma::Auto(4092))
|
2024-10-30 19:52:02 +01:00
|
|
|
).unwrap();
|
|
|
|
|
2024-11-24 23:53:43 +01:00
|
|
|
let cfg = Config::new().baudrate(3.MHz().into());
|
2024-10-30 19:52:02 +01:00
|
|
|
|
|
|
|
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;
|
2024-11-24 23:53:43 +01:00
|
|
|
unsafe {
|
|
|
|
let target = Ws2812::new(spi, &mut STATIC_BUFFER);
|
|
|
|
return SmartLedDisplay::<Ws2812<SpiBusDriver<'_, SpiDriver<'_>>>, S, 310>::new(target, MAX_POWER_MW);
|
|
|
|
}
|
2024-10-30 19:52:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|