render: implement a generic Renderer task that can run any display

This commit is contained in:
Victoria Fischer 2024-11-02 15:22:37 +01:00
parent 873954d596
commit dbfc79046e
5 changed files with 80 additions and 53 deletions

View File

@ -28,6 +28,8 @@ use crate::render::SharedSurface as SurfaceType;
#[cfg(not(feature="threads"))] #[cfg(not(feature="threads"))]
use crate::render::SimpleSurface as SurfaceType; use crate::render::SimpleSurface as SurfaceType;
use crate::render::Renderer;
fn main() { fn main() {
// It is necessary to call this function once. Otherwise some patches to the runtime // It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
@ -40,13 +42,17 @@ fn main() {
let mut display = DisplayType::new_display::<SurfaceType>(); let mut display = DisplayType::new_display::<SurfaceType>();
log::info!("Created new display type {}", core::any::type_name_of_val(&display));
log::info!("Creating runner"); log::info!("Creating runner");
let mut runner = task::Scheduler::new(vec![ let mut runner = task::Scheduler::new(vec![
Box::new(animations::IdleTask::new(display.new_surface().unwrap())), Box::new(animations::IdleTask::new(display.new_surface().unwrap())),
Box::new(animations::TestPattern::new(display.new_surface().unwrap())), Box::new(animations::TestPattern::new(display.new_surface().unwrap())),
Box::new(display), Box::new(Renderer::new(display)),
]); ]);
log::info!("Runner ready: {:?}", runner);
log::info!("Ready to rock and roll"); log::info!("Ready to rock and roll");
loop { loop {
runner.tick(); runner.tick();

View File

@ -8,15 +8,13 @@ use ws2812_esp32_rmt_driver::lib_embedded_graphics::{Ws2812DrawTarget, LedPixelS
use esp_idf_svc::hal::prelude::Peripherals; use esp_idf_svc::hal::prelude::Peripherals;
use running_average::RealTimeRunningAverage;
use std::io; use std::io;
use std::fmt::{Formatter, Debug};
use crate::power; use crate::power;
use crate::power::AsMilliwatts; use crate::power::AsMilliwatts;
use crate::lib8::*; use crate::lib8::*;
use crate::render::*; use crate::render::*;
use crate::time::Periodically;
use crate::task::Task;
use crate::geometry::*; use crate::geometry::*;
use crate::platform::DisplayInit; use crate::platform::DisplayInit;
@ -28,42 +26,44 @@ S: Surface {
target: T, target: T,
total_mw: u32, total_mw: u32,
max_mw: u32, max_mw: u32,
fps: RealTimeRunningAverage<u32>,
frame: u32,
fps_display: Periodically
} }
impl<T: LedPixelShape, S: Surface> Task for EmbeddedDisplay<Ws2812DrawTarget<'_, T>, S> {
fn start(&mut self) {
self.target.set_brightness(0);
}
fn name(&self) -> &'static str { "Renderer" } impl<T: DrawTarget, S: Surface> Debug for EmbeddedDisplay<T, S> {
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
fn tick(&mut self) { f.debug_struct("EmbeddedDisplay")
self.start_frame(); .field("total_mw", &self.total_mw)
self.render_frame(); .field("surfaces", &self.surfaces)
self.end_frame(); .finish()
} }
} }
trait TargetInit<T: DrawTarget> {
fn setup_target(mut target: T) -> T { target }
}
impl<T, S> EmbeddedDisplay<T, S> impl<T, S> EmbeddedDisplay<T, S>
where where
Self: TargetInit<T>,
T: DrawTarget, T: DrawTarget,
S: Surface { S: Surface {
pub fn new(target: T, max_mw: u32) -> Self { pub fn new(target: T, max_mw: u32) -> Self {
EmbeddedDisplay { EmbeddedDisplay {
surfaces: SurfacePool::new(), surfaces: SurfacePool::new(),
target: target, target: Self::setup_target(target),
max_mw: max_mw, max_mw: max_mw,
total_mw: 0, total_mw: 0,
fps: RealTimeRunningAverage::default(),
frame: 0,
fps_display: Periodically::new_every_n_seconds(5)
} }
} }
} }
impl<T: LedPixelShape, S: Surface> TargetInit<Ws2812DrawTarget<'_, T>> for EmbeddedDisplay<Ws2812DrawTarget<'_, T>, S> {
fn setup_target(mut target: Ws2812DrawTarget<'_, T>) -> Ws2812DrawTarget<'_, T> {
target.set_brightness(0);
target
}
}
impl<T, S> Surfaces<S> for EmbeddedDisplay<T, S> impl<T, S> Surfaces<S> for EmbeddedDisplay<T, S>
where where
T: DrawTarget, T: DrawTarget,
@ -77,17 +77,12 @@ impl<T: LedPixelShape, S: Surface> Display<S> for EmbeddedDisplay<Ws2812DrawTarg
fn start_frame(&mut self) { fn start_frame(&mut self) {
self.total_mw = 0; self.total_mw = 0;
self.frame = self.frame.wrapping_add(1);
} }
fn end_frame(&mut self) { fn end_frame(&mut self) {
let brightness = power::brightness_for_mw(self.total_mw, 255, self.max_mw); let brightness = power::brightness_for_mw(self.total_mw, 255, self.max_mw);
self.target.set_brightness(brightness); self.target.set_brightness(brightness);
self.target.flush().unwrap(); self.target.flush().unwrap();
self.fps.insert(1);
self.fps_display.run(|| {
log::info!("FPS: {} frame={} brightness={} mw={}", self.fps.measurement(), self.frame, brightness, self.total_mw);
});
} }
fn render_frame(&mut self) { fn render_frame(&mut self) {
@ -114,7 +109,7 @@ impl<T: LedPixelShape, S: Surface> Display<S> for EmbeddedDisplay<Ws2812DrawTarg
} }
impl<Shape: LedPixelShape> DisplayInit for Ws2812DrawTarget<'_, Shape> { impl<Shape: LedPixelShape> DisplayInit for Ws2812DrawTarget<'_, Shape> {
fn new_display<S: Surface>() -> impl Display<S> + Task { fn new_display<S: Surface>() -> impl Display<S> {
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take().unwrap();
let led_pin = peripherals.pins.gpio14; let led_pin = peripherals.pins.gpio14;
let channel = peripherals.rmt.channel0; let channel = peripherals.rmt.channel0;

View File

@ -5,8 +5,7 @@ pub mod embedded_graphics_lib;
pub mod smart_leds_lib; pub mod smart_leds_lib;
use crate::render::{Surface, Display}; use crate::render::{Surface, Display};
use crate::task::Task;
pub trait DisplayInit { pub trait DisplayInit {
fn new_display<S: Surface>() -> impl Display<S> + Task; fn new_display<S: Surface>() -> impl Display<S>;
} }

View File

@ -1,12 +1,10 @@
use smart_leds_trait::SmartLedsWrite; use smart_leds_trait::SmartLedsWrite;
use running_average::RealTimeRunningAverage;
use crate::lib8::Rgb8Blend; use crate::lib8::Rgb8Blend;
use crate::render::{Surface, SurfacePool, Display, Surfaces}; use crate::render::{Surface, SurfacePool, Display, Surfaces};
use crate::task::Task;
use crate::power::{brightness_for_mw, AsMilliwatts}; use crate::power::{brightness_for_mw, AsMilliwatts};
use crate::time::Periodically;
use crate::geometry::*; use crate::geometry::*;
use std::fmt::{Debug, Formatter};
use smart_leds::brightness; use smart_leds::brightness;
@ -17,14 +15,20 @@ use rgb::Rgb;
pub struct SmartLedDisplay<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> { pub struct SmartLedDisplay<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> {
surfaces : SurfacePool<S>, surfaces : SurfacePool<S>,
target: T, target: T,
fps: RealTimeRunningAverage<u32>,
frame: u32,
fps_display: Periodically,
pixbuf: [T::Color; 255], pixbuf: [T::Color; 255],
total_mw: u32, total_mw: u32,
max_mw: u32 max_mw: u32
} }
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> Debug for SmartLedDisplay<T, S> {
fn fmt(&self, f: &mut Formatter) -> Result<(), std::fmt::Error> {
f.debug_struct("SmartLedDisplay")
.field("total_mw", &self.total_mw)
.field("surfaces", &self.surfaces)
.finish()
}
}
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> SmartLedDisplay<T, S> { impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> SmartLedDisplay<T, S> {
pub fn new(target: T, max_mw: u32) -> Self { pub fn new(target: T, max_mw: u32) -> Self {
SmartLedDisplay { SmartLedDisplay {
@ -32,21 +36,14 @@ impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> SmartLedDisplay<T, S> {
target: target, target: target,
max_mw: max_mw, max_mw: max_mw,
total_mw: 0, total_mw: 0,
fps: RealTimeRunningAverage::default(),
frame: 0,
fps_display: Periodically::new_every_n_seconds(5),
pixbuf: [Rgb::new(0, 0, 0); 255] pixbuf: [Rgb::new(0, 0, 0); 255]
} }
} }
} }
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> Task for SmartLedDisplay<T, S> { impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> AsMilliwatts for SmartLedDisplay<T, S> {
fn name(&self) -> &'static str { "Renderer" } fn as_milliwatts(&self) -> u32 {
self.total_mw
fn tick(&mut self) {
self.start_frame();
self.render_frame();
self.end_frame();
} }
} }
@ -61,16 +58,11 @@ S: Surface {
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> Display<S> for SmartLedDisplay<T, S> { impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> Display<S> for SmartLedDisplay<T, S> {
fn start_frame(&mut self) { fn start_frame(&mut self) {
self.frame = self.frame.wrapping_add(1);
self.total_mw = 0; self.total_mw = 0;
} }
fn end_frame(&mut self) { fn end_frame(&mut self) {
self.fps.insert(1);
let b = brightness_for_mw(self.total_mw, 255, self.max_mw); let b = brightness_for_mw(self.total_mw, 255, self.max_mw);
self.fps_display.run(|| {
log::info!("FPS: {} frame={} brightness={} mw={}", self.fps.measurement(), self.frame, b, self.total_mw);
});
let _ = self.target.write(brightness(self.pixbuf.iter().cloned(), b)); let _ = self.target.write(brightness(self.pixbuf.iter().cloned(), b));
} }
@ -95,12 +87,11 @@ pub mod rmt {
use ws2812_esp32_rmt_driver::lib_smart_leds::Ws2812Esp32Rmt; use ws2812_esp32_rmt_driver::lib_smart_leds::Ws2812Esp32Rmt;
use crate::render::{Display, Surface}; use crate::render::{Display, Surface};
use crate::task::Task;
use crate::platform::smart_leds_lib::SmartLedDisplay; use crate::platform::smart_leds_lib::SmartLedDisplay;
use crate::platform::DisplayInit; use crate::platform::DisplayInit;
impl DisplayInit for Ws2812Esp32Rmt<'_> { impl DisplayInit for Ws2812Esp32Rmt<'_> {
fn new_display<S: Surface>() -> impl Display<S> + Task { fn new_display<S: Surface>() -> impl Display<S> {
let peripherals = Peripherals::take().unwrap(); let peripherals = Peripherals::take().unwrap();
let led_pin = peripherals.pins.gpio14; let led_pin = peripherals.pins.gpio14;
let channel = peripherals.rmt.channel0; let channel = peripherals.rmt.channel0;
@ -113,7 +104,6 @@ pub mod rmt {
return SmartLedDisplay::new(target, MAX_POWER_MW); return SmartLedDisplay::new(target, MAX_POWER_MW);
} }
} }
} }
#[cfg(feature="spi")] #[cfg(feature="spi")]

View File

@ -7,6 +7,10 @@ use rgb::RGB8;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use crate::geometry::*; use crate::geometry::*;
use crate::task::Task;
use crate::time::Periodically;
use running_average::RealTimeRunningAverage;
use std::marker::PhantomData;
use std::fmt::Debug; use std::fmt::Debug;
pub trait Shader: Send + Debug { pub trait Shader: Send + Debug {
@ -29,6 +33,39 @@ pub trait Display<T: Surface>: Surfaces<T> {
fn render_frame(&mut self); fn render_frame(&mut self);
} }
#[derive(Debug)]
pub struct Renderer<T: Display<S>, S: Surface> {
display: T,
fps: RealTimeRunningAverage<u32>,
fps_display: Periodically,
_sfc: PhantomData<S>
}
impl<T: Display<S>, S: Surface> Renderer<T, S> {
pub fn new(display: T) -> Self {
Self {
display,
fps: RealTimeRunningAverage::default(),
fps_display: Periodically::new_every_n_seconds(5),
_sfc: PhantomData::default()
}
}
}
impl<T: Display<S>, S: Surface> Task for Renderer<T, S> {
fn name(&self) -> &'static str { "Renderer" }
fn tick(&mut self) {
self.display.start_frame();
self.display.render_frame();
self.display.end_frame();
self.fps.insert(1);
self.fps_display.run(|| {
log::info!("FPS: {} {:?}", self.fps.measurement(), self.display);
});
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct ShaderBinding { pub struct ShaderBinding {
shader: Option<Box<dyn Shader>>, shader: Option<Box<dyn Shader>>,