main: rewrite display creation code to use type aliases and fewer cfg branches
This commit is contained in:
parent
0a28f9f5c6
commit
8662eb0db7
84
src/main.rs
84
src/main.rs
@ -1,7 +1,3 @@
|
|||||||
use palette::Hsv;
|
|
||||||
|
|
||||||
use rgb::RGB8;
|
|
||||||
|
|
||||||
mod power;
|
mod power;
|
||||||
mod lib8;
|
mod lib8;
|
||||||
mod render;
|
mod render;
|
||||||
@ -11,72 +7,26 @@ mod geometry;
|
|||||||
mod platform;
|
mod platform;
|
||||||
mod animations;
|
mod animations;
|
||||||
|
|
||||||
|
|
||||||
use crate::time::Periodically;
|
|
||||||
use crate::geometry::{Coordinates, VirtualCoordinates};
|
|
||||||
use crate::render::{Shader, Surfaces, Surface};
|
|
||||||
use crate::task::Task;
|
|
||||||
use crate::platform::DisplayInit;
|
use crate::platform::DisplayInit;
|
||||||
use crate::lib8::IntoRgb8;
|
use crate::render::Surfaces;
|
||||||
|
|
||||||
#[cfg(feature="rmt")]
|
|
||||||
#[cfg(feature="embedded-graphics")]
|
#[cfg(feature="embedded-graphics")]
|
||||||
use ws2812_esp32_rmt_driver::lib_embedded_graphics::PonderjarTarget;
|
|
||||||
|
|
||||||
#[cfg(feature="rmt")]
|
#[cfg(feature="rmt")]
|
||||||
#[cfg(feature="smart-leds")]
|
use crate::platform::embedded_graphics_lib::PonderjarTarget as DisplayType;
|
||||||
use ws2812_esp32_rmt_driver::lib_smart_leds::Ws2812Esp32Rmt;
|
|
||||||
|
|
||||||
#[cfg(feature="spi")]
|
|
||||||
#[cfg(feature="smart-leds")]
|
#[cfg(feature="smart-leds")]
|
||||||
use crate::platform::smart_leds_lib::spi::SPIDisplay;
|
#[cfg(feature="rmt")]
|
||||||
|
use ws2812_esp32_rmt_driver::lib_smart_leds::Ws2812Esp32Rmt as DisplayType;
|
||||||
|
|
||||||
|
#[cfg(feature="smart-leds")]
|
||||||
|
#[cfg(feature="spi")]
|
||||||
|
use crate::platform::smart_leds_lib::spi::SPIDisplay as DisplayType;
|
||||||
|
|
||||||
#[cfg(feature="threads")]
|
#[cfg(feature="threads")]
|
||||||
use crate::render::SharedSurface;
|
use crate::render::SharedSurface as SurfaceType;
|
||||||
|
|
||||||
#[cfg(not(feature="threads"))]
|
#[cfg(not(feature="threads"))]
|
||||||
use crate::render::SimpleSurface;
|
use crate::render::SimpleSurface as SurfaceType;
|
||||||
|
|
||||||
struct IdleTask<T: Surface> {
|
|
||||||
frame: u8,
|
|
||||||
surface: T,
|
|
||||||
updater: Periodically
|
|
||||||
}
|
|
||||||
|
|
||||||
struct IdleShader {
|
|
||||||
frame: u8
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Shader for IdleShader {
|
|
||||||
fn draw(&self, coords: VirtualCoordinates) -> RGB8 {
|
|
||||||
Hsv::new_srgb(self.frame.wrapping_add(coords.x()).wrapping_add(coords.y()), 255, 255).into_rgb8()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Surface> IdleTask<T> {
|
|
||||||
fn new(surface: T) -> Self {
|
|
||||||
IdleTask {
|
|
||||||
frame: 0,
|
|
||||||
surface: surface,
|
|
||||||
updater: Periodically::new_every_n_ms(16)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Surface> Task for IdleTask<T> {
|
|
||||||
fn name(&self) -> &'static str { "Idle" }
|
|
||||||
|
|
||||||
fn tick(&mut self) {
|
|
||||||
self.updater.run(|| {
|
|
||||||
self.frame = self.frame.wrapping_add(1);
|
|
||||||
self.surface.set_shader(Box::new(IdleShader { frame: self.frame }));
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
fn stop(&mut self) {
|
|
||||||
self.surface.clear_shader();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
||||||
@ -88,19 +38,7 @@ fn main() {
|
|||||||
|
|
||||||
log::info!("Setting up display");
|
log::info!("Setting up display");
|
||||||
|
|
||||||
#[cfg(feature="threads")]
|
let mut display = DisplayType::new_display::<SurfaceType>();
|
||||||
type SurfaceType = SharedSurface;
|
|
||||||
#[cfg(not(feature="threads"))]
|
|
||||||
type SurfaceType = SimpleSurface;
|
|
||||||
|
|
||||||
#[cfg(feature="spi")]
|
|
||||||
let mut display = SPIDisplay::new_display::<SurfaceType>();
|
|
||||||
|
|
||||||
#[cfg(feature="embedded-graphics")]
|
|
||||||
let mut display = PonderjarTarget::new_display::<SurfaceType>();
|
|
||||||
|
|
||||||
#[cfg(feature="rmt")]
|
|
||||||
let mut display = Ws2812Esp32Rmt::new_display::<SurfaceType>();
|
|
||||||
|
|
||||||
log::info!("Creating runner");
|
log::info!("Creating runner");
|
||||||
let mut runner = task::Scheduler::new(vec![
|
let mut runner = task::Scheduler::new(vec![
|
||||||
|
@ -123,6 +123,7 @@ pub mod spi {
|
|||||||
use crate::render::{Display, Surface};
|
use crate::render::{Display, Surface};
|
||||||
use crate::task::Task;
|
use crate::task::Task;
|
||||||
use crate::platform::smart_leds_lib::SmartLedDisplay;
|
use crate::platform::smart_leds_lib::SmartLedDisplay;
|
||||||
|
use crate::DisplayInit;
|
||||||
|
|
||||||
use esp_idf_svc::hal::{
|
use esp_idf_svc::hal::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user