render: effectively rename Display to Output, push remaining common code into Renderer task

This commit is contained in:
2024-11-29 18:13:22 +01:00
parent 5ca062adbd
commit bd2f2edebb
6 changed files with 131 additions and 129 deletions

View File

@ -4,8 +4,9 @@ pub mod embedded_graphics_lib;
#[cfg(feature="smart-leds")]
pub mod smart_leds_lib;
use crate::render::{Surface, Display};
use crate::render::{Surface, Output};
pub trait DisplayInit {
fn new_display<S: Surface>() -> impl Display<S>;
type Output: Output;
fn new_display<S: Surface>() -> Self::Output;
}

View File

@ -1,14 +1,11 @@
use smart_leds_trait::SmartLedsWrite;
use crate::lib8::interpolate::Fract8Ops;
use crate::render::{Display, HardwarePixel, PixelView, Sample, Surface, Surfaces};
use crate::buffers::SurfacePool;
use crate::render::{HardwarePixel, Output, PixelView, Sample};
use crate::power::{brightness_for_mw, AsMilliwatts};
use crate::geometry::*;
use crate::mappings::*;
use std::fmt::{Debug, Formatter};
use std::io;
use std::fmt::Debug;
use std::ops::IndexMut;
pub trait Pixbuf: AsMilliwatts + IndexMut<usize, Output=Self::Pixel> {
@ -16,6 +13,7 @@ pub trait Pixbuf: AsMilliwatts + IndexMut<usize, Output=Self::Pixel> {
fn new() -> Self;
fn blank(&mut self);
fn iter_with_brightness(&self, brightness: u8) -> impl Iterator<Item = Self::Pixel> + Send;
fn pixel_count(&self) -> usize;
}
struct StrideSampler<'a, P: Pixbuf> {
@ -35,12 +33,32 @@ impl<'a, P: Pixbuf> PixelView for StrideSampler<'a, P> {
}
}
struct StrideOutput<P: Pixbuf> {
pixbuf: P,
stride_map: StrideMapping
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()
}
}
impl<P: Pixbuf> Sample for StrideOutput<P> {
pub struct StrideOutput<P: Pixbuf, T: FastWrite> {
pixbuf: P,
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
}
}
}
impl<P: Pixbuf, T: FastWrite> Sample for StrideOutput<P, T> {
type Pixel = P::Pixel;
fn sample(&mut self, rect: &Rectangle<Virtual>) -> impl PixelView<Pixel = Self::Pixel> {
StrideSampler {
@ -50,12 +68,29 @@ impl<P: Pixbuf> Sample for StrideOutput<P> {
}
}
impl<P: Pixbuf<Pixel=T::Color>, T: FastWrite> Output for StrideOutput<P, T> {
fn blank(&mut self) {
self.pixbuf.blank();
}
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!");
};
}
}
impl<T: HardwarePixel, const PIXEL_NUM: usize> Pixbuf for [T; PIXEL_NUM] {
type Pixel = T;
fn new() -> Self {
[T::default(); PIXEL_NUM]
}
fn pixel_count(&self) -> usize {
self.len()
}
fn blank(&mut self) {
self.fill(T::default())
}
@ -64,84 +99,7 @@ impl<T: HardwarePixel, const PIXEL_NUM: usize> Pixbuf for [T; PIXEL_NUM] {
self.iter().map(move |x| { x.scale8(brightness)})
}
}
struct SmartLedDisplay<T: FastWrite, S: Surface, P: Pixbuf<Pixel = T::Color>> {
surfaces : Option<SurfacePool<S>>,
output: StrideOutput<P>,
target: T,
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.output.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 {
output: StrideOutput { pixbuf: pixbuf, stride_map: pixmap },
surfaces: Some(SurfacePool::new()),
target,
max_mw,
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> {
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.output.pixbuf.blank();
}
fn render_frame(&mut self) {
let surfaces = self.surfaces.take().unwrap();
for surface in surfaces.iter() {
let rect = surface.rect();
let opacity = surface.opacity();
if opacity > 0 {
let mut sample = self.output.sample(&rect);
surface.with_shader(|shader| {
while let Some((virt_coords, pixel)) = sample.next() {
*pixel = pixel.blend8(shader.draw(&virt_coords, self.frame).into(), opacity);
}
})
}
}
self.surfaces = Some(surfaces);
}
fn end_frame(&mut self) {
let b = brightness_for_mw(self.output.pixbuf.as_milliwatts(), 255, self.max_mw);
if self.target.fast_write(self.output.pixbuf.iter_with_brightness(b)).is_err() {
panic!("Could not write frame!");
};
self.frame += 1;
}
}
trait FastWrite {
pub trait FastWrite {
type Target: SmartLedsWrite;
type Color: HardwarePixel;
type Error;
@ -153,17 +111,18 @@ trait FastWrite {
#[cfg(feature="rmt")]
pub mod rmt {
use esp_idf_svc::hal::prelude::Peripherals;
use esp_idf_svc::{hal::prelude::Peripherals, sys::esp_efuse_mac_get_default};
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::smart_leds_lib::StrideOutput;
use crate::render::Surface;
use crate::platform::DisplayInit;
use super::{Pixbuf, FastWrite, SmartLedDisplay};
use super::{Pixbuf, FastWrite};
pub type FastWs2812Esp32Rmt<'a> = LedPixelEsp32Rmt<'a, Rgb<u8>, LedPixelColorGrb24>;
@ -180,24 +139,39 @@ pub mod rmt {
}
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;
type Output = StrideOutput<[Rgb<u8>; 310], Self>;
fn new_display<S: Surface>() -> Self::Output {
const POWER_VOLTS : u32 = 5;
const POWER_MA : u32 = 500;
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
let peripherals = Peripherals::take().unwrap();
let channel = peripherals.rmt.channel0;
let pins = peripherals.pins;
//let pixbuf: [Rgb<u8>; 310] = Pixbuf::new();
let pixbuf: [<Self as FastWrite>::Color; 310] = Pixbuf::new();
let pixmap = StrideMapping::new_jar();
let mut chip_id: [u8; 8] = [0; 8];
unsafe {
esp_efuse_mac_get_default(&mut chip_id as *mut u8);
}
assert!(pixmap.pixel_count <= pixbuf.len(), "map needs {} pixels, I only have PIXEL_NUM={}", pixmap.pixel_count, pixbuf.len());
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())
}
};
let target = Self::new(channel, led_pin).unwrap();
return SmartLedDisplay::new(target, MAX_POWER_MW, pixmap, pixbuf);
StrideOutput::new(
Pixbuf::new(),
pixmap,
target,
MAX_POWER_MW
)
}
}
}