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-10-29 01:27:20 +01:00
|
|
|
use crate::render::{Surface, SurfacePool, Display, Surfaces};
|
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-02 15:22:37 +01:00
|
|
|
use std::fmt::{Debug, Formatter};
|
2024-10-29 01:27:20 +01:00
|
|
|
|
|
|
|
use smart_leds::brightness;
|
|
|
|
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
use rgb::Rgb;
|
|
|
|
|
2024-11-16 12:09:38 +01:00
|
|
|
pub struct SmartLedDisplay<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> {
|
2024-10-29 01:27:20 +01:00
|
|
|
surfaces : SurfacePool<S>,
|
|
|
|
target: T,
|
2024-11-16 12:09:38 +01:00
|
|
|
pixbuf: [T::Color; PIXEL_NUM],
|
2024-10-29 01:27:20 +01:00
|
|
|
max_mw: u32
|
|
|
|
}
|
|
|
|
|
2024-11-16 12:09:38 +01:00
|
|
|
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> Debug for SmartLedDisplay<T, S, PIXEL_NUM> {
|
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-16 12:09:38 +01:00
|
|
|
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> SmartLedDisplay<T, S, PIXEL_NUM> {
|
2024-10-29 01:27:20 +01:00
|
|
|
pub fn new(target: T, max_mw: u32) -> Self {
|
|
|
|
SmartLedDisplay {
|
|
|
|
surfaces: SurfacePool::new(),
|
2024-11-16 12:09:38 +01:00
|
|
|
pixbuf: [Rgb::new(0, 0, 0); PIXEL_NUM],
|
2024-10-29 01:27:20 +01:00
|
|
|
target: target,
|
|
|
|
max_mw: max_mw,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-16 12:09:38 +01:00
|
|
|
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> AsMilliwatts for SmartLedDisplay<T, S, PIXEL_NUM> {
|
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-16 12:09:38 +01:00
|
|
|
impl<T, S, const PIXEL_NUM: usize> Surfaces<S> for SmartLedDisplay<T, S, PIXEL_NUM>
|
2024-10-29 01:27:20 +01:00
|
|
|
where
|
|
|
|
T: SmartLedsWrite<Color = Rgb<u8>>,
|
|
|
|
S: Surface {
|
|
|
|
fn new_surface(&mut self) -> Result<S, io::Error> {
|
|
|
|
self.surfaces.new_surface()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-16 12:09:38 +01:00
|
|
|
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface, const PIXEL_NUM: usize> Display<S> for SmartLedDisplay<T, S, PIXEL_NUM> {
|
|
|
|
|
2024-10-29 01:27:20 +01:00
|
|
|
fn start_frame(&mut self) {
|
2024-11-16 12:09:38 +01:00
|
|
|
self.pixbuf.fill(Rgb::new(0, 0, 0));
|
2024-10-29 01:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn end_frame(&mut self) {
|
2024-11-16 12:06:00 +01:00
|
|
|
let b = brightness_for_mw(self.pixbuf.as_milliwatts(), 255, self.max_mw);
|
2024-10-30 19:52:02 +01:00
|
|
|
let _ = self.target.write(brightness(self.pixbuf.iter().cloned(), b));
|
2024-10-29 01:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn render_frame(&mut self) {
|
2024-10-29 11:51:25 +01:00
|
|
|
for x in 0..self.pixbuf.len() {
|
2024-10-30 19:52:02 +01:00
|
|
|
let virt_coords = VirtualCoordinates::new(x as u8, 0);
|
2024-10-30 20:48:00 +01:00
|
|
|
let mut pixel = Rgb::new(0, 0, 0);
|
2024-10-29 01:27:20 +01:00
|
|
|
for surface in self.surfaces.iter() {
|
|
|
|
surface.with_shader(|shader| {
|
2024-11-16 11:31:45 +01:00
|
|
|
pixel = pixel.saturating_add(shader.draw(&virt_coords));
|
2024-10-29 01:27:20 +01:00
|
|
|
})
|
|
|
|
}
|
2024-10-30 20:48:00 +01:00
|
|
|
self.pixbuf[x] = Rgb::new(pixel.r, pixel.g, pixel.b);
|
2024-10-29 01:27:20 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2024-10-30 19:52:02 +01:00
|
|
|
|
|
|
|
#[cfg(feature="rmt")]
|
|
|
|
pub mod rmt {
|
|
|
|
use esp_idf_svc::hal::prelude::Peripherals;
|
|
|
|
use ws2812_esp32_rmt_driver::lib_smart_leds::Ws2812Esp32Rmt;
|
|
|
|
|
|
|
|
use crate::render::{Display, Surface};
|
2024-10-30 20:54:49 +01:00
|
|
|
use crate::platform::smart_leds_lib::SmartLedDisplay;
|
2024-10-30 19:52:02 +01:00
|
|
|
|
|
|
|
use crate::platform::DisplayInit;
|
|
|
|
impl DisplayInit for Ws2812Esp32Rmt<'_> {
|
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;
|
|
|
|
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();
|
2024-11-16 12:09:38 +01:00
|
|
|
return SmartLedDisplay::<Ws2812Esp32Rmt, S, 300>::new(target, MAX_POWER_MW);
|
2024-10-30 19:52:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature="spi")]
|
|
|
|
pub mod spi {
|
|
|
|
use ws2812_spi::Ws2812;
|
|
|
|
|
|
|
|
use crate::render::{Display, Surface};
|
|
|
|
use crate::task::Task;
|
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-10-30 19:52:02 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|