2024-10-29 01:27:20 +01:00
|
|
|
use smart_leds_trait::SmartLedsWrite;
|
|
|
|
use running_average::RealTimeRunningAverage;
|
|
|
|
|
|
|
|
use crate::render::{Surface, SurfacePool, Display, Surfaces};
|
|
|
|
use crate::task::Task;
|
|
|
|
use crate::power;
|
|
|
|
use crate::time::Periodically;
|
|
|
|
use crate::lib8::RGB8;
|
|
|
|
use crate::geometry::*;
|
|
|
|
use crate::power::AsMilliwatts;
|
|
|
|
|
|
|
|
use smart_leds::brightness;
|
|
|
|
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
use rgb::Rgb;
|
|
|
|
|
|
|
|
pub struct SmartLedDisplay<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> {
|
|
|
|
surfaces : SurfacePool<S>,
|
|
|
|
target: T,
|
|
|
|
fps: RealTimeRunningAverage<u32>,
|
|
|
|
frame: u32,
|
|
|
|
fps_display: Periodically,
|
|
|
|
pixbuf: [T::Color; 255],
|
|
|
|
total_mw: u32,
|
|
|
|
max_mw: u32
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> SmartLedDisplay<T, S> {
|
|
|
|
pub fn new(target: T, max_mw: u32) -> Self {
|
|
|
|
SmartLedDisplay {
|
|
|
|
surfaces: SurfacePool::new(),
|
|
|
|
target: target,
|
|
|
|
max_mw: max_mw,
|
|
|
|
total_mw: 0,
|
|
|
|
fps: RealTimeRunningAverage::default(),
|
|
|
|
frame: 0,
|
|
|
|
fps_display: Periodically::new_every_n_seconds(5),
|
|
|
|
pixbuf: [Rgb::new(0, 0, 0); 255]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> Task for SmartLedDisplay<T, S> {
|
|
|
|
fn name(&self) -> &'static str { "Renderer" }
|
|
|
|
|
|
|
|
fn tick(&mut self) {
|
|
|
|
self.start_frame();
|
|
|
|
self.render_frame();
|
|
|
|
self.end_frame();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, S> Surfaces<S> for SmartLedDisplay<T, S>
|
|
|
|
where
|
|
|
|
T: SmartLedsWrite<Color = Rgb<u8>>,
|
|
|
|
S: Surface {
|
|
|
|
fn new_surface(&mut self) -> Result<S, io::Error> {
|
|
|
|
self.surfaces.new_surface()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: SmartLedsWrite<Color = Rgb<u8>>, S: Surface> Display<S> for SmartLedDisplay<T, S> {
|
|
|
|
fn start_frame(&mut self) {
|
|
|
|
self.frame = self.frame.wrapping_add(1);
|
|
|
|
self.total_mw = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end_frame(&mut self) {
|
|
|
|
self.fps.insert(1);
|
|
|
|
let b = power::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);
|
|
|
|
});
|
|
|
|
self.target.write(brightness(self.pixbuf.iter().cloned(), b));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn render_frame(&mut self) {
|
2024-10-29 11:51:25 +01:00
|
|
|
for x in 0..self.pixbuf.len() {
|
2024-10-29 01:27:20 +01:00
|
|
|
let virtCoords = VirtualCoordinates::new(x as u8, 0);
|
|
|
|
let mut pixel = RGB8::new(0, 0, 0);
|
|
|
|
for surface in self.surfaces.iter() {
|
|
|
|
surface.with_shader(|shader| {
|
|
|
|
pixel = shader.draw(virtCoords.clone());
|
|
|
|
})
|
|
|
|
}
|
|
|
|
self.total_mw += pixel.as_milliwatts();
|
|
|
|
self.pixbuf[x] = Rgb::new(pixel.red, pixel.green, pixel.blue);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|