src: rewrite display to have generic surface implementations
This commit is contained in:
parent
ed034046e8
commit
e475d66842
@ -3,23 +3,40 @@ use embedded_graphics::{
|
|||||||
pixelcolor::Rgb888,
|
pixelcolor::Rgb888,
|
||||||
primitives::Rectangle
|
primitives::Rectangle
|
||||||
};
|
};
|
||||||
|
use embedded_graphics::pixelcolor::RgbColor;
|
||||||
use ws2812_esp32_rmt_driver::lib_embedded_graphics::{Ws2812DrawTarget, LedPixelShape};
|
use ws2812_esp32_rmt_driver::lib_embedded_graphics::{Ws2812DrawTarget, LedPixelShape};
|
||||||
|
|
||||||
use std::rc::Rc;
|
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::sync::{Arc, Mutex};
|
|
||||||
use running_average::RealTimeRunningAverage;
|
use running_average::RealTimeRunningAverage;
|
||||||
|
use std::io;
|
||||||
|
|
||||||
use crate::power;
|
use crate::power;
|
||||||
|
use crate::power::AsMilliwatts;
|
||||||
use crate::lib8::*;
|
use crate::lib8::*;
|
||||||
use crate::render::*;
|
use crate::render::*;
|
||||||
use crate::time::Periodically;
|
use crate::time::Periodically;
|
||||||
|
use crate::task::Task;
|
||||||
use crate::geometry::*;
|
use crate::geometry::*;
|
||||||
|
|
||||||
pub struct EmbeddedDisplay<T>
|
impl<T: RgbColor> AsMilliwatts for T {
|
||||||
|
fn as_milliwatts(&self) -> u32 {
|
||||||
|
const RED_MW : u32 = 16 * 5; //< 16mA @ 5v = 80mW
|
||||||
|
const GREEN_MW : u32 = 11 * 5; //< 11mA @ 5v = 55mW
|
||||||
|
const BLUE_MW : u32 = 15 * 5; //< 15mA @ 5v = 75mW
|
||||||
|
const DARK_MW : u32 = 1 * 5; //< 1mA @ 5v = 5mW
|
||||||
|
|
||||||
|
let red = (self.r() as u32 * RED_MW).wrapping_shr(8);
|
||||||
|
let green = (self.g() as u32 * GREEN_MW).wrapping_shr(8);
|
||||||
|
let blue = (self.b() as u32 * BLUE_MW).wrapping_shr(8);
|
||||||
|
|
||||||
|
return red + green + blue + DARK_MW;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct EmbeddedDisplay<T, S>
|
||||||
where
|
where
|
||||||
T: DrawTarget {
|
T: DrawTarget,
|
||||||
surfaces : RefCell<Vec<Surface>>,
|
S: Surface + Default + Clone {
|
||||||
|
surfaces : SurfacePool<S>,
|
||||||
target: T,
|
target: T,
|
||||||
total_mw: u32,
|
total_mw: u32,
|
||||||
max_mw: u32,
|
max_mw: u32,
|
||||||
@ -28,12 +45,27 @@ T: DrawTarget {
|
|||||||
fps_display: Periodically
|
fps_display: Periodically
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> EmbeddedDisplay<T>
|
impl<T: LedPixelShape, S: Surface + Default + Clone> Task for EmbeddedDisplay<Ws2812DrawTarget<'_, T>, S> {
|
||||||
|
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>
|
||||||
where
|
where
|
||||||
T: DrawTarget {
|
T: DrawTarget,
|
||||||
|
S: Surface + Default + Clone {
|
||||||
pub fn new(target: T, max_mw: u32) -> Self {
|
pub fn new(target: T, max_mw: u32) -> Self {
|
||||||
EmbeddedDisplay {
|
EmbeddedDisplay {
|
||||||
surfaces: RefCell::new(Vec::new()),
|
surfaces: SurfacePool::new(),
|
||||||
target: target,
|
target: target,
|
||||||
max_mw: max_mw,
|
max_mw: max_mw,
|
||||||
total_mw: 0,
|
total_mw: 0,
|
||||||
@ -44,17 +76,17 @@ T: DrawTarget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Surfaces for EmbeddedDisplay<T>
|
impl<T, S> Surfaces<S> for EmbeddedDisplay<T, S>
|
||||||
where
|
where
|
||||||
T: DrawTarget {
|
T: DrawTarget,
|
||||||
fn new_surface(&mut self) -> Surface {
|
S: Surface + Default + Clone {
|
||||||
let surface = Surface::new();
|
fn new_surface(&mut self) -> Result<S, io::Error> {
|
||||||
self.surfaces.borrow_mut().push(surface.clone());
|
self.surfaces.new_surface()
|
||||||
return surface;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: LedPixelShape> Display for EmbeddedDisplay<Ws2812DrawTarget<'_, T>> {
|
impl<T: LedPixelShape, S: Surface + Default + Clone> Display<S> for EmbeddedDisplay<Ws2812DrawTarget<'_, T>, S> {
|
||||||
|
|
||||||
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);
|
self.frame = self.frame.wrapping_add(1);
|
||||||
@ -76,19 +108,17 @@ impl<T: LedPixelShape> Display for EmbeddedDisplay<Ws2812DrawTarget<'_, T>> {
|
|||||||
let yStride: u8 = 255 / (size.height as u8);
|
let yStride: u8 = 255 / (size.height as u8);
|
||||||
let area = Rectangle::new(Point::new(0, 0), size);
|
let area = Rectangle::new(Point::new(0, 0), size);
|
||||||
|
|
||||||
let surfaces = self.surfaces.borrow();
|
|
||||||
|
|
||||||
self.target.draw_iter(
|
self.target.draw_iter(
|
||||||
area.points()
|
area.points()
|
||||||
.map(|pos| {
|
.map(|pos| {
|
||||||
let virtCoords = VirtualCoordinates::new(pos.x as u8 * xStride, pos.y as u8 * yStride);
|
let virtCoords = VirtualCoordinates::new(pos.x as u8 * xStride, pos.y as u8 * yStride);
|
||||||
let mut pixel = RGB8::new(0, 0, 0);
|
let mut pixel = RGB8::new(0, 0, 0);
|
||||||
for surface in surfaces.iter() {
|
for surface in self.surfaces.iter() {
|
||||||
surface.with_shader(|shader| {
|
surface.with_shader(|shader| {
|
||||||
pixel = shader.draw(virtCoords.clone());
|
pixel = shader.draw(virtCoords.clone());
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
self.total_mw += power::color_to_mw(&pixel);
|
self.total_mw += pixel.as_milliwatts();
|
||||||
return Pixel(pos, Rgb888::new(pixel.red, pixel.green, pixel.blue));
|
return Pixel(pos, Rgb888::new(pixel.red, pixel.green, pixel.blue));
|
||||||
})
|
})
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
26
src/main.rs
26
src/main.rs
@ -1,13 +1,12 @@
|
|||||||
#![feature(trait_upcasting)]
|
#![feature(trait_upcasting)]
|
||||||
#![allow(arithmetic_overflow)]
|
#![allow(arithmetic_overflow)]
|
||||||
use esp_idf_svc::hal::prelude::Peripherals;
|
use esp_idf_svc::hal::prelude::Peripherals;
|
||||||
use ws2812_esp32_rmt_driver::lib_embedded_graphics::{LedPixelStrip, LedPixelShape, LedPixelMatrix, Ws2812DrawTarget};
|
use ws2812_esp32_rmt_driver::lib_embedded_graphics::{LedPixelShape, Ws2812DrawTarget};
|
||||||
use embedded_graphics::{
|
use embedded_graphics::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
};
|
};
|
||||||
use palette::Hsv;
|
use palette::Hsv;
|
||||||
use palette::convert::IntoColorUnclamped;
|
use palette::convert::IntoColorUnclamped;
|
||||||
use std::thread;
|
|
||||||
|
|
||||||
mod power;
|
mod power;
|
||||||
mod lib8;
|
mod lib8;
|
||||||
@ -20,10 +19,11 @@ mod embedded_graphics_lib;
|
|||||||
use crate::time::Periodically;
|
use crate::time::Periodically;
|
||||||
use crate::geometry::{Coordinates, VirtualCoordinates};
|
use crate::geometry::{Coordinates, VirtualCoordinates};
|
||||||
use crate::embedded_graphics_lib::EmbeddedDisplay;
|
use crate::embedded_graphics_lib::EmbeddedDisplay;
|
||||||
|
use crate::render::{Surfaces, Surface, SimpleSurface};
|
||||||
|
|
||||||
struct IdleTask {
|
struct IdleTask<T: Surface> {
|
||||||
frame: u8,
|
frame: u8,
|
||||||
surface: render::Surface,
|
surface: T,
|
||||||
updater: Periodically
|
updater: Periodically
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,17 +37,17 @@ impl render::Shader for IdleShader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IdleTask {
|
impl<T: Surface> IdleTask<T> {
|
||||||
fn new(render: &mut dyn render::Display) -> Self {
|
fn new(surface: T) -> Self {
|
||||||
IdleTask {
|
IdleTask {
|
||||||
frame: 0,
|
frame: 0,
|
||||||
surface: render.new_surface(),
|
surface: surface,
|
||||||
updater: Periodically::new_every_n_ms(16)
|
updater: Periodically::new_every_n_ms(16)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl task::Task for IdleTask {
|
impl<T: Surface> task::Task for IdleTask<T> {
|
||||||
fn name(&self) -> &'static str { "Idle" }
|
fn name(&self) -> &'static str { "Idle" }
|
||||||
|
|
||||||
fn tick(&mut self) {
|
fn tick(&mut self) {
|
||||||
@ -82,6 +82,8 @@ impl LedPixelShape for PonderjarMatrix {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type PonderjarTarget<'a> = Ws2812DrawTarget<'a, PonderjarMatrix>;
|
||||||
|
|
||||||
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
|
||||||
@ -96,19 +98,17 @@ fn main() {
|
|||||||
let led_pin = peripherals.pins.gpio14;
|
let led_pin = peripherals.pins.gpio14;
|
||||||
let channel = peripherals.rmt.channel0;
|
let channel = peripherals.rmt.channel0;
|
||||||
|
|
||||||
const NUM_PIXELS : usize = 255;
|
|
||||||
const POWER_VOLTS : u32 = 5;
|
const POWER_VOLTS : u32 = 5;
|
||||||
const POWER_MA : u32 = 500;
|
const POWER_MA : u32 = 500;
|
||||||
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
|
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
|
||||||
|
|
||||||
log::info!("Setting up display");
|
log::info!("Setting up display");
|
||||||
let mut target = Ws2812DrawTarget::<PonderjarMatrix>::new(channel, led_pin).unwrap();
|
let target = PonderjarTarget::new(channel, led_pin).unwrap();
|
||||||
target.set_brightness(0);
|
let mut display = EmbeddedDisplay::<PonderjarTarget, SimpleSurface>::new(target, MAX_POWER_MW);
|
||||||
let mut display = EmbeddedDisplay::new(target, MAX_POWER_MW);
|
|
||||||
|
|
||||||
log::info!("Creating runner");
|
log::info!("Creating runner");
|
||||||
let mut runner = task::Scheduler::new(vec![
|
let mut runner = task::Scheduler::new(vec![
|
||||||
Box::new(IdleTask::new(&mut display)),
|
Box::new(IdleTask::new(display.new_surface().unwrap())),
|
||||||
Box::new(display),
|
Box::new(display),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
15
src/power.rs
15
src/power.rs
@ -1,16 +1,5 @@
|
|||||||
use embedded_graphics::pixelcolor::RgbColor;
|
pub trait AsMilliwatts {
|
||||||
|
fn as_milliwatts(&self) -> u32;
|
||||||
pub fn color_to_mw<T: RgbColor>(color : &T) -> u32 {
|
|
||||||
const RED_MW : u32 = 16 * 5; //< 16mA @ 5v = 80mW
|
|
||||||
const GREEN_MW : u32 = 11 * 5; //< 11mA @ 5v = 55mW
|
|
||||||
const BLUE_MW : u32 = 15 * 5; //< 15mA @ 5v = 75mW
|
|
||||||
const DARK_MW : u32 = 1 * 5; //< 1mA @ 5v = 5mW
|
|
||||||
|
|
||||||
let red = (color.r() as u32 * RED_MW).wrapping_shr(8);
|
|
||||||
let green = (color.g() as u32 * GREEN_MW).wrapping_shr(8);
|
|
||||||
let blue = (color.b() as u32 * BLUE_MW).wrapping_shr(8);
|
|
||||||
|
|
||||||
return red + green + blue + DARK_MW;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn brightness_for_mw(total_mw : u32, target : u8, max_power: u32) -> u8 {
|
pub fn brightness_for_mw(total_mw : u32, target : u8, max_power: u32) -> u8 {
|
||||||
|
113
src/render.rs
113
src/render.rs
@ -1,55 +1,47 @@
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use palette::blend::{BlendWith, Equations, Parameter, PreAlpha};
|
use std::io;
|
||||||
|
|
||||||
use crate::task;
|
|
||||||
use crate::lib8::RGB8;
|
use crate::lib8::RGB8;
|
||||||
use crate::power;
|
|
||||||
use crate::time::Periodically;
|
|
||||||
use crate::geometry::*;
|
use crate::geometry::*;
|
||||||
|
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
pub trait Shader: Send {
|
pub trait Shader: Send {
|
||||||
fn draw(&self, surface_coords: VirtualCoordinates) -> RGB8;
|
fn draw(&self, surface_coords: VirtualCoordinates) -> RGB8;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Surfaces {
|
pub trait Surface {
|
||||||
fn new_surface(&mut self) -> Surface;
|
fn with_shader<F: FnMut(&dyn Shader)>(&self, f: F);
|
||||||
|
fn set_shader(&mut self, shader: Box<dyn Shader>);
|
||||||
|
fn clear_shader(&mut self);
|
||||||
|
fn set_opacity(&mut self, opacity: u8);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait Display: Surfaces {
|
pub trait Surfaces<T: Surface> {
|
||||||
|
fn new_surface(&mut self) -> Result<T, io::Error>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Display<T: Surface>: Surfaces<T> {
|
||||||
fn start_frame(&mut self) {}
|
fn start_frame(&mut self) {}
|
||||||
fn end_frame(&mut self) {}
|
fn end_frame(&mut self) {}
|
||||||
|
fn render_frame(&mut self);
|
||||||
fn render_frame(&mut self) {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> task::Task for T
|
pub struct ShaderBinding {
|
||||||
where
|
|
||||||
T: Display {
|
|
||||||
fn name(&self) -> &'static str { "Renderer" }
|
|
||||||
|
|
||||||
fn tick(&mut self) {
|
|
||||||
self.start_frame();
|
|
||||||
self.render_frame();
|
|
||||||
self.end_frame();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ShaderBinding {
|
|
||||||
shader: Option<Box<dyn Shader>>,
|
shader: Option<Box<dyn Shader>>,
|
||||||
opacity: u8,
|
opacity: u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Surface {
|
pub struct BoundSurface<T> {
|
||||||
pub binding: Arc<Mutex<ShaderBinding>>
|
pub binding: T
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Surface {
|
pub type SharedSurface = BoundSurface<Arc<Mutex<ShaderBinding>>>;
|
||||||
pub fn new() -> Self {
|
pub type SimpleSurface = BoundSurface<Rc<RefCell<ShaderBinding>>>;
|
||||||
|
|
||||||
|
impl Default for BoundSurface<Arc<Mutex<ShaderBinding>>> {
|
||||||
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
binding: Arc::new(Mutex::new(ShaderBinding {
|
binding: Arc::new(Mutex::new(ShaderBinding {
|
||||||
shader: None,
|
shader: None,
|
||||||
@ -57,22 +49,79 @@ impl Surface {
|
|||||||
})),
|
})),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn with_shader<F: FnOnce(&dyn Shader)>(&self, f: F) {
|
impl Default for BoundSurface<Rc<RefCell<ShaderBinding>>>{
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
binding: Rc::new(RefCell::new(ShaderBinding {
|
||||||
|
shader: None,
|
||||||
|
opacity: 255,
|
||||||
|
})),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Surface for BoundSurface<Rc<RefCell<ShaderBinding>>> {
|
||||||
|
fn with_shader<F: FnMut(&dyn Shader)>(&self, mut f: F) {
|
||||||
|
if let Some(ref shader) = self.binding.borrow().shader {
|
||||||
|
f(shader.as_ref());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_shader(&mut self, shader: Box<dyn Shader>) {
|
||||||
|
self.binding.borrow_mut().shader = Some(shader);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clear_shader(&mut self) {
|
||||||
|
self.binding.borrow_mut().shader = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_opacity(&mut self, opacity: u8) {
|
||||||
|
self.binding.borrow_mut().opacity = opacity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Surface for BoundSurface<Arc<Mutex<ShaderBinding>>> {
|
||||||
|
fn with_shader<F: FnMut(&dyn Shader)>(&self, mut f: F) {
|
||||||
if let Some(ref shader) = self.binding.lock().unwrap().shader {
|
if let Some(ref shader) = self.binding.lock().unwrap().shader {
|
||||||
f(shader.as_ref());
|
f(shader.as_ref());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_shader(&mut self, shader: Box<dyn Shader>) {
|
fn set_shader(&mut self, shader: Box<dyn Shader>) {
|
||||||
self.binding.lock().unwrap().shader = Some(shader);
|
self.binding.lock().unwrap().shader = Some(shader);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear_shader(&mut self) {
|
fn clear_shader(&mut self) {
|
||||||
self.binding.lock().unwrap().shader = None;
|
self.binding.lock().unwrap().shader = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_opacity(&mut self, opacity: u8) {
|
fn set_opacity(&mut self, opacity: u8) {
|
||||||
self.binding.lock().unwrap().opacity = opacity;
|
self.binding.lock().unwrap().opacity = opacity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct SurfacePool<S: Surface + Default> {
|
||||||
|
surfaces: Vec<S>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S: Surface + Default> SurfacePool<S> {
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
surfaces: Vec::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn iter(&self) -> std::slice::Iter<S> {
|
||||||
|
self.surfaces.iter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<S: Surface + Default + Clone> Surfaces<S> for SurfacePool<S> {
|
||||||
|
fn new_surface(&mut self) -> Result<S, io::Error> {
|
||||||
|
let surface = S::default();
|
||||||
|
self.surfaces.push(surface.clone());
|
||||||
|
return Ok(surface);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user