2024-10-29 01:27:20 +01:00
|
|
|
use smart_leds_trait::SmartLedsWrite;
|
|
|
|
|
2024-11-29 19:00:07 +01:00
|
|
|
use crate::buffers::Pixbuf;
|
2024-11-29 18:13:22 +01:00
|
|
|
use crate::render::{HardwarePixel, Output, PixelView, Sample};
|
2024-11-29 19:00:07 +01:00
|
|
|
use crate::power::brightness_for_mw;
|
2024-10-29 01:27:20 +01:00
|
|
|
use crate::geometry::*;
|
2024-11-16 12:13:49 +01:00
|
|
|
use crate::mappings::*;
|
2024-10-29 01:27:20 +01:00
|
|
|
|
2024-11-29 18:13:22 +01:00
|
|
|
use std::fmt::Debug;
|
2024-11-18 17:21:58 +01:00
|
|
|
|
2024-11-29 10:57:33 +01:00
|
|
|
struct StrideSampler<'a, P: Pixbuf> {
|
|
|
|
pixbuf: &'a mut P,
|
|
|
|
selection: StrideView<'a>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, P: Pixbuf> PixelView for StrideSampler<'a, P> {
|
|
|
|
type Pixel = P::Pixel;
|
|
|
|
fn next(&mut self) -> Option<(Coordinates<Virtual>, &mut Self::Pixel)> {
|
|
|
|
if let Some((virt, coords)) = self.selection.next() {
|
|
|
|
let idx = self.selection.map.strides[coords.x as usize].pixel_idx_for_offset(coords.y);
|
|
|
|
Some((virt, &mut self.pixbuf[idx]))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-29 18:13:22 +01:00
|
|
|
impl<P: Pixbuf, T: FastWrite> Debug for StrideOutput<P, T> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.debug_struct("StrideOutput").finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct StrideOutput<P: Pixbuf, T: FastWrite> {
|
2024-11-29 10:57:33 +01:00
|
|
|
pixbuf: P,
|
2024-11-29 18:13:22 +01:00
|
|
|
stride_map: StrideMapping,
|
|
|
|
target: T,
|
|
|
|
max_mw: u32
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<P: Pixbuf, T: FastWrite> StrideOutput<P, T> {
|
|
|
|
fn new(pixbuf: P, stride_map: StrideMapping, target: T, max_mw: u32) -> Self {
|
|
|
|
assert!(stride_map.pixel_count <= pixbuf.pixel_count(), "map needs {} pixels, I only have PIXEL_NUM={}", stride_map.pixel_count, pixbuf.pixel_count());
|
|
|
|
StrideOutput {
|
|
|
|
pixbuf,
|
|
|
|
stride_map,
|
|
|
|
target,
|
|
|
|
max_mw
|
|
|
|
}
|
|
|
|
}
|
2024-11-29 10:57:33 +01:00
|
|
|
}
|
|
|
|
|
2024-11-29 18:13:22 +01:00
|
|
|
impl<P: Pixbuf, T: FastWrite> Sample for StrideOutput<P, T> {
|
2024-11-29 10:57:33 +01:00
|
|
|
type Pixel = P::Pixel;
|
|
|
|
fn sample(&mut self, rect: &Rectangle<Virtual>) -> impl PixelView<Pixel = Self::Pixel> {
|
|
|
|
StrideSampler {
|
|
|
|
pixbuf: &mut self.pixbuf,
|
|
|
|
selection: self.stride_map.select(rect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-29 18:13:22 +01:00
|
|
|
impl<P: Pixbuf<Pixel=T::Color>, T: FastWrite> Output for StrideOutput<P, T> {
|
2024-11-18 17:21:58 +01:00
|
|
|
fn blank(&mut self) {
|
2024-11-29 18:13:22 +01:00
|
|
|
self.pixbuf.blank();
|
2024-11-18 17:21:58 +01:00
|
|
|
}
|
2024-11-24 23:53:43 +01:00
|
|
|
|
2024-11-29 18:13:22 +01:00
|
|
|
fn commit(&mut self) {
|
|
|
|
let b = brightness_for_mw(self.pixbuf.as_milliwatts(), 255, self.max_mw);
|
|
|
|
if self.target.fast_write(self.pixbuf.iter_with_brightness(b)).is_err() {
|
|
|
|
panic!("Could not write frame!");
|
|
|
|
};
|
2024-10-29 01:27:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-29 18:13:22 +01:00
|
|
|
pub trait FastWrite {
|
2024-11-24 23:53:43 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-10-30 19:52:02 +01:00
|
|
|
#[cfg(feature="rmt")]
|
|
|
|
pub mod rmt {
|
2024-11-29 18:13:22 +01:00
|
|
|
use esp_idf_svc::{hal::prelude::Peripherals, sys::esp_efuse_mac_get_default};
|
2024-11-29 00:14:06 +01:00
|
|
|
use ws2812_esp32_rmt_driver::driver::color::LedPixelColorGrb24;
|
2024-11-24 23:53:43 +01:00
|
|
|
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-11-29 18:13:22 +01:00
|
|
|
use crate::platform::smart_leds_lib::StrideOutput;
|
|
|
|
use crate::render::Surface;
|
2024-10-30 19:52:02 +01:00
|
|
|
use crate::platform::DisplayInit;
|
2024-11-18 17:21:58 +01:00
|
|
|
|
2024-11-29 18:13:22 +01:00
|
|
|
use super::{Pixbuf, FastWrite};
|
2024-11-24 23:53:43 +01:00
|
|
|
|
|
|
|
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<'_> {
|
2024-11-29 18:13:22 +01:00
|
|
|
type Output = StrideOutput<[Rgb<u8>; 310], Self>;
|
2024-10-30 19:52:02 +01:00
|
|
|
|
2024-11-29 18:13:22 +01:00
|
|
|
fn new_display<S: Surface>() -> Self::Output {
|
2024-10-30 19:52:02 +01:00
|
|
|
const POWER_VOLTS : u32 = 5;
|
|
|
|
const POWER_MA : u32 = 500;
|
|
|
|
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
|
2024-11-29 18:13:22 +01:00
|
|
|
let peripherals = Peripherals::take().unwrap();
|
|
|
|
let channel = peripherals.rmt.channel0;
|
|
|
|
let pins = peripherals.pins;
|
2024-10-30 19:52:02 +01:00
|
|
|
|
2024-11-29 18:13:22 +01:00
|
|
|
let mut chip_id: [u8; 8] = [0; 8];
|
|
|
|
unsafe {
|
|
|
|
esp_efuse_mac_get_default(&mut chip_id as *mut u8);
|
|
|
|
}
|
2024-11-24 23:53:43 +01:00
|
|
|
|
2024-11-29 18:13:22 +01:00
|
|
|
let (pixmap, target) = match chip_id {
|
|
|
|
[72, 202, 67, 89, 145, 204, 0, 0] => {
|
|
|
|
(StrideMapping::new_panel(), Self::new(channel, pins.gpio5).unwrap())
|
|
|
|
},
|
|
|
|
[140, 170, 181, 131, 95, 116, 0, 0] => {
|
|
|
|
(StrideMapping::new_jar(), Self::new(channel, pins.gpio14).unwrap())
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
(StrideMapping::new(), Self::new(channel, pins.gpio5).unwrap())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
StrideOutput::new(
|
|
|
|
Pixbuf::new(),
|
|
|
|
pixmap,
|
|
|
|
target,
|
|
|
|
MAX_POWER_MW
|
|
|
|
)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|