2024-10-27 15:14:28 +01:00
|
|
|
use embedded_graphics::{
|
|
|
|
prelude::*,
|
|
|
|
pixelcolor::Rgb888,
|
|
|
|
primitives::Rectangle
|
|
|
|
};
|
2024-10-28 22:22:03 +01:00
|
|
|
use embedded_graphics::pixelcolor::RgbColor;
|
2024-10-27 15:14:28 +01:00
|
|
|
use ws2812_esp32_rmt_driver::lib_embedded_graphics::{Ws2812DrawTarget, LedPixelShape};
|
|
|
|
|
|
|
|
use running_average::RealTimeRunningAverage;
|
2024-10-28 22:22:03 +01:00
|
|
|
use std::io;
|
2024-10-27 15:14:28 +01:00
|
|
|
|
|
|
|
use crate::power;
|
2024-10-28 22:22:03 +01:00
|
|
|
use crate::power::AsMilliwatts;
|
2024-10-27 15:14:28 +01:00
|
|
|
use crate::lib8::*;
|
|
|
|
use crate::render::*;
|
|
|
|
use crate::time::Periodically;
|
2024-10-28 22:22:03 +01:00
|
|
|
use crate::task::Task;
|
2024-10-27 15:14:28 +01:00
|
|
|
use crate::geometry::*;
|
2024-10-30 19:52:02 +01:00
|
|
|
use crate::platform::DisplayInit;
|
2024-10-28 22:22:03 +01:00
|
|
|
|
|
|
|
pub struct EmbeddedDisplay<T, S>
|
2024-10-27 15:14:28 +01:00
|
|
|
where
|
2024-10-28 22:22:03 +01:00
|
|
|
T: DrawTarget,
|
2024-10-28 23:30:26 +01:00
|
|
|
S: Surface {
|
2024-10-28 22:22:03 +01:00
|
|
|
surfaces : SurfacePool<S>,
|
2024-10-27 15:14:28 +01:00
|
|
|
target: T,
|
|
|
|
total_mw: u32,
|
|
|
|
max_mw: u32,
|
|
|
|
fps: RealTimeRunningAverage<u32>,
|
|
|
|
frame: u32,
|
|
|
|
fps_display: Periodically
|
|
|
|
}
|
|
|
|
|
2024-10-28 23:30:26 +01:00
|
|
|
impl<T: LedPixelShape, S: Surface> Task for EmbeddedDisplay<Ws2812DrawTarget<'_, T>, S> {
|
2024-10-28 22:22:03 +01:00
|
|
|
fn start(&mut self) {
|
|
|
|
self.target.set_brightness(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn name(&self) -> &'static str { "Renderer" }
|
|
|
|
|
|
|
|
fn tick(&mut self) {
|
|
|
|
self.start_frame();
|
|
|
|
self.render_frame();
|
|
|
|
self.end_frame();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, S> EmbeddedDisplay<T, S>
|
2024-10-27 15:14:28 +01:00
|
|
|
where
|
2024-10-28 22:22:03 +01:00
|
|
|
T: DrawTarget,
|
2024-10-28 23:30:26 +01:00
|
|
|
S: Surface {
|
2024-10-27 15:14:28 +01:00
|
|
|
pub fn new(target: T, max_mw: u32) -> Self {
|
|
|
|
EmbeddedDisplay {
|
2024-10-28 22:22:03 +01:00
|
|
|
surfaces: SurfacePool::new(),
|
2024-10-27 15:14:28 +01:00
|
|
|
target: target,
|
|
|
|
max_mw: max_mw,
|
|
|
|
total_mw: 0,
|
|
|
|
fps: RealTimeRunningAverage::default(),
|
|
|
|
frame: 0,
|
|
|
|
fps_display: Periodically::new_every_n_seconds(5)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-28 22:22:03 +01:00
|
|
|
impl<T, S> Surfaces<S> for EmbeddedDisplay<T, S>
|
2024-10-27 15:14:28 +01:00
|
|
|
where
|
2024-10-28 22:22:03 +01:00
|
|
|
T: DrawTarget,
|
2024-10-28 23:30:26 +01:00
|
|
|
S: Surface {
|
2024-10-28 22:22:03 +01:00
|
|
|
fn new_surface(&mut self) -> Result<S, io::Error> {
|
|
|
|
self.surfaces.new_surface()
|
2024-10-27 15:14:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-28 23:30:26 +01:00
|
|
|
impl<T: LedPixelShape, S: Surface> Display<S> for EmbeddedDisplay<Ws2812DrawTarget<'_, T>, S> {
|
2024-10-28 22:22:03 +01:00
|
|
|
|
2024-10-27 15:14:28 +01:00
|
|
|
fn start_frame(&mut self) {
|
|
|
|
self.total_mw = 0;
|
|
|
|
self.frame = self.frame.wrapping_add(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn end_frame(&mut self) {
|
|
|
|
let brightness = power::brightness_for_mw(self.total_mw, 255, self.max_mw);
|
|
|
|
self.target.set_brightness(brightness);
|
|
|
|
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) {
|
|
|
|
let size = T::size();
|
|
|
|
let xStride: u8 = 255 / (size.width as u8);
|
|
|
|
let yStride: u8 = 255 / (size.height as u8);
|
|
|
|
let area = Rectangle::new(Point::new(0, 0), size);
|
|
|
|
|
|
|
|
self.target.draw_iter(
|
|
|
|
area.points()
|
|
|
|
.map(|pos| {
|
|
|
|
let virtCoords = VirtualCoordinates::new(pos.x as u8 * xStride, pos.y as u8 * yStride);
|
|
|
|
let mut pixel = RGB8::new(0, 0, 0);
|
2024-10-28 22:22:03 +01:00
|
|
|
for surface in self.surfaces.iter() {
|
2024-10-27 15:14:28 +01:00
|
|
|
surface.with_shader(|shader| {
|
|
|
|
pixel = shader.draw(virtCoords.clone());
|
|
|
|
})
|
|
|
|
}
|
2024-10-28 22:22:03 +01:00
|
|
|
self.total_mw += pixel.as_milliwatts();
|
2024-10-27 15:14:28 +01:00
|
|
|
return Pixel(pos, Rgb888::new(pixel.red, pixel.green, pixel.blue));
|
|
|
|
})
|
|
|
|
).unwrap();
|
|
|
|
}
|
|
|
|
}
|
2024-10-30 19:52:02 +01:00
|
|
|
|
|
|
|
impl<Shape: LedPixelShape> DisplayInit for Ws2812DrawTarget<'_, Shape> {
|
|
|
|
fn new_display<S: Surface>() -> impl Display<S> + Task {
|
|
|
|
let peripherals = Peripherals::take().unwrap();
|
|
|
|
let led_pin = peripherals.pins.gpio14;
|
|
|
|
let channel = peripherals.rmt.channel0;
|
|
|
|
|
|
|
|
const POWER_VOLTS : u32 = 5;
|
|
|
|
const POWER_MA : u32 = 500;
|
|
|
|
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
|
|
|
|
|
|
|
|
let target = Self::new(channel, led_pin).unwrap();
|
|
|
|
return EmbeddedDisplay::<Self, S>::new(target, MAX_POWER_MW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct PonderjarMatrix {}
|
|
|
|
impl LedPixelShape for PonderjarMatrix {
|
|
|
|
fn size() -> Size {
|
|
|
|
Size::new(17, 17)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pixel_index(point: Point) -> Option<usize> {
|
|
|
|
if (0..Self::size().width as i32).contains(&point.x) && (0..Self::size().height as i32).contains(&point.y) {
|
|
|
|
if point.y % 2 == 0 {
|
|
|
|
Some((point.y as u32 * Self::size().width as u32 + point.x as u32).try_into().unwrap())
|
|
|
|
} else {
|
|
|
|
Some((point.y as u32 * Self::size().width as u32 - point.x as u32).try_into().unwrap())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type PonderjarTarget<'a> = Ws2812DrawTarget<'a, PonderjarMatrix>;
|