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>, S: Surface> { surfaces : SurfacePool, target: T, fps: RealTimeRunningAverage, frame: u32, fps_display: Periodically, pixbuf: [T::Color; 255], total_mw: u32, max_mw: u32 } impl>, S: Surface> SmartLedDisplay { 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>, S: Surface> Task for SmartLedDisplay { fn name(&self) -> &'static str { "Renderer" } fn tick(&mut self) { self.start_frame(); self.render_frame(); self.end_frame(); } } impl Surfaces for SmartLedDisplay where T: SmartLedsWrite>, S: Surface { fn new_surface(&mut self) -> Result { self.surfaces.new_surface() } } impl>, S: Surface> Display for SmartLedDisplay { 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) { for x in (0..self.pixbuf.len()) { 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); }; } }