From b7995423d7c8cd19307ec3ba779d6532be746740 Mon Sep 17 00:00:00 2001 From: Victoria Fischer Date: Wed, 30 Oct 2024 19:59:19 +0100 Subject: [PATCH] cargo: also hide thread api behind thread cfg --- Cargo.toml | 2 ++ src/main.rs | 21 ++++++++++++++++----- src/render.rs | 39 +++++++++++++++++---------------------- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 873e4c2..66d81f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,8 @@ nightly = ["esp-idf-svc/nightly"] experimental = ["esp-idf-svc/experimental"] embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] +threads = [] + [dependencies] log = { version = "0.4", default-features = false } esp-idf-svc = { version = "0.49", default-features = false } diff --git a/src/main.rs b/src/main.rs index 7f22c8f..411b8f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,9 +29,15 @@ use crate::smart_leds_lib::spi::SPIDisplay; use crate::time::Periodically; use crate::geometry::{Coordinates, VirtualCoordinates}; -use crate::render::{Shader, Surfaces, Surface, SimpleSurface}; +use crate::render::{Shader, Surfaces, Surface}; use crate::task::Task; -use crate::platform::{DisplayInit}; +use crate::platform::DisplayInit; + +#[cfg(feature="threads")] +use crate::render::SharedSurface; + +#[cfg(not(feature="threads"))] +use crate::render::SimpleSurface; struct IdleTask { frame: u8, @@ -84,14 +90,19 @@ fn main() { log::info!("Setting up display"); + #[cfg(feature="threads")] + type SurfaceType = SharedSurface; + #[cfg(not(feature="threads"))] + type SurfaceType = SimpleSurface; + #[cfg(feature="spi")] - let mut display = SPIDisplay::new_display::(); + let mut display = SPIDisplay::new_display::(); #[cfg(feature="embedded-graphics")] - let mut display = PonderjarTarget::new_display::(); + let mut display = PonderjarTarget::new_display::(); #[cfg(feature="rmt")] - let mut display = Ws2812Esp32Rmt::new_display::(); + let mut display = Ws2812Esp32Rmt::new_display::(); log::info!("Creating runner"); let mut runner = task::Scheduler::new(vec![ diff --git a/src/render.rs b/src/render.rs index 6b08682..3ffd3c1 100644 --- a/src/render.rs +++ b/src/render.rs @@ -1,8 +1,10 @@ use std::rc::Rc; use std::cell::RefCell; -use std::sync::{Arc, Mutex}; use std::io; +#[cfg(feature="threads")] +use std::sync::{Arc, Mutex}; + use crate::lib8::RGB8; use crate::geometry::*; @@ -14,7 +16,6 @@ pub trait Surface: Default + Clone { fn with_shader(&self, f: F); fn set_shader(&mut self, shader: Box); fn clear_shader(&mut self); - fn set_opacity(&mut self, opacity: u8); } pub trait Surfaces { @@ -29,7 +30,6 @@ pub trait Display: Surfaces { pub struct ShaderBinding { shader: Option>, - opacity: u8, } #[derive(Clone)] @@ -37,26 +37,13 @@ pub struct BoundSurface { pub binding: T } -pub type SharedSurface = BoundSurface>>; pub type SimpleSurface = BoundSurface>>; -impl Default for BoundSurface>> { - fn default() -> Self { - Self { - binding: Arc::new(Mutex::new(ShaderBinding { - shader: None, - opacity: 255, - })), - } - } -} - impl Default for BoundSurface>>{ fn default() -> Self { Self { binding: Rc::new(RefCell::new(ShaderBinding { shader: None, - opacity: 255, })), } } @@ -76,12 +63,24 @@ impl Surface for BoundSurface>> { fn clear_shader(&mut self) { self.binding.borrow_mut().shader = None; } +} - fn set_opacity(&mut self, opacity: u8) { - self.binding.borrow_mut().opacity = opacity; +#[cfg(feature="threads")] +pub type SharedSurface = BoundSurface>>; + + +#[cfg(feature="threads")] +impl Default for BoundSurface>> { + fn default() -> Self { + Self { + binding: Arc::new(Mutex::new(ShaderBinding { + shader: None, + })), + } } } +#[cfg(feature="threads")] impl Surface for BoundSurface>> { fn with_shader(&self, mut f: F) { if let Some(ref shader) = self.binding.lock().unwrap().shader { @@ -96,10 +95,6 @@ impl Surface for BoundSurface>> { fn clear_shader(&mut self) { self.binding.lock().unwrap().shader = None; } - - fn set_opacity(&mut self, opacity: u8) { - self.binding.lock().unwrap().opacity = opacity; - } } pub struct SurfacePool {