cargo: also hide thread api behind thread cfg

This commit is contained in:
Victoria Fischer 2024-10-30 19:59:19 +01:00
parent f803d8fe93
commit b7995423d7
3 changed files with 35 additions and 27 deletions

View File

@ -32,6 +32,8 @@ nightly = ["esp-idf-svc/nightly"]
experimental = ["esp-idf-svc/experimental"] experimental = ["esp-idf-svc/experimental"]
embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"] embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"]
threads = []
[dependencies] [dependencies]
log = { version = "0.4", default-features = false } log = { version = "0.4", default-features = false }
esp-idf-svc = { version = "0.49", default-features = false } esp-idf-svc = { version = "0.49", default-features = false }

View File

@ -29,9 +29,15 @@ use crate::smart_leds_lib::spi::SPIDisplay;
use crate::time::Periodically; use crate::time::Periodically;
use crate::geometry::{Coordinates, VirtualCoordinates}; use crate::geometry::{Coordinates, VirtualCoordinates};
use crate::render::{Shader, Surfaces, Surface, SimpleSurface}; use crate::render::{Shader, Surfaces, Surface};
use crate::task::Task; 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<T: Surface> { struct IdleTask<T: Surface> {
frame: u8, frame: u8,
@ -84,14 +90,19 @@ fn main() {
log::info!("Setting up display"); log::info!("Setting up display");
#[cfg(feature="threads")]
type SurfaceType = SharedSurface;
#[cfg(not(feature="threads"))]
type SurfaceType = SimpleSurface;
#[cfg(feature="spi")] #[cfg(feature="spi")]
let mut display = SPIDisplay::new_display::<SimpleSurface>(); let mut display = SPIDisplay::new_display::<SurfaceType>();
#[cfg(feature="embedded-graphics")] #[cfg(feature="embedded-graphics")]
let mut display = PonderjarTarget::new_display::<SimpleSurface>(); let mut display = PonderjarTarget::new_display::<SurfaceType>();
#[cfg(feature="rmt")] #[cfg(feature="rmt")]
let mut display = Ws2812Esp32Rmt::new_display::<SimpleSurface>(); 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![

View File

@ -1,8 +1,10 @@
use std::rc::Rc; use std::rc::Rc;
use std::cell::RefCell; use std::cell::RefCell;
use std::sync::{Arc, Mutex};
use std::io; use std::io;
#[cfg(feature="threads")]
use std::sync::{Arc, Mutex};
use crate::lib8::RGB8; use crate::lib8::RGB8;
use crate::geometry::*; use crate::geometry::*;
@ -14,7 +16,6 @@ pub trait Surface: Default + Clone {
fn with_shader<F: FnMut(&dyn Shader)>(&self, f: F); fn with_shader<F: FnMut(&dyn Shader)>(&self, f: F);
fn set_shader(&mut self, shader: Box<dyn Shader>); fn set_shader(&mut self, shader: Box<dyn Shader>);
fn clear_shader(&mut self); fn clear_shader(&mut self);
fn set_opacity(&mut self, opacity: u8);
} }
pub trait Surfaces<T: Surface> { pub trait Surfaces<T: Surface> {
@ -29,7 +30,6 @@ pub trait Display<T: Surface>: Surfaces<T> {
pub struct ShaderBinding { pub struct ShaderBinding {
shader: Option<Box<dyn Shader>>, shader: Option<Box<dyn Shader>>,
opacity: u8,
} }
#[derive(Clone)] #[derive(Clone)]
@ -37,26 +37,13 @@ pub struct BoundSurface<T> {
pub binding: T pub binding: T
} }
pub type SharedSurface = BoundSurface<Arc<Mutex<ShaderBinding>>>;
pub type SimpleSurface = BoundSurface<Rc<RefCell<ShaderBinding>>>; pub type SimpleSurface = BoundSurface<Rc<RefCell<ShaderBinding>>>;
impl Default for BoundSurface<Arc<Mutex<ShaderBinding>>> {
fn default() -> Self {
Self {
binding: Arc::new(Mutex::new(ShaderBinding {
shader: None,
opacity: 255,
})),
}
}
}
impl Default for BoundSurface<Rc<RefCell<ShaderBinding>>>{ impl Default for BoundSurface<Rc<RefCell<ShaderBinding>>>{
fn default() -> Self { fn default() -> Self {
Self { Self {
binding: Rc::new(RefCell::new(ShaderBinding { binding: Rc::new(RefCell::new(ShaderBinding {
shader: None, shader: None,
opacity: 255,
})), })),
} }
} }
@ -76,12 +63,24 @@ impl Surface for BoundSurface<Rc<RefCell<ShaderBinding>>> {
fn clear_shader(&mut self) { fn clear_shader(&mut self) {
self.binding.borrow_mut().shader = None; self.binding.borrow_mut().shader = None;
} }
}
fn set_opacity(&mut self, opacity: u8) { #[cfg(feature="threads")]
self.binding.borrow_mut().opacity = opacity; pub type SharedSurface = BoundSurface<Arc<Mutex<ShaderBinding>>>;
#[cfg(feature="threads")]
impl Default for BoundSurface<Arc<Mutex<ShaderBinding>>> {
fn default() -> Self {
Self {
binding: Arc::new(Mutex::new(ShaderBinding {
shader: None,
})),
}
} }
} }
#[cfg(feature="threads")]
impl Surface for BoundSurface<Arc<Mutex<ShaderBinding>>> { impl Surface for BoundSurface<Arc<Mutex<ShaderBinding>>> {
fn with_shader<F: FnMut(&dyn Shader)>(&self, mut f: F) { 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 {
@ -96,10 +95,6 @@ impl Surface for BoundSurface<Arc<Mutex<ShaderBinding>>> {
fn clear_shader(&mut self) { fn clear_shader(&mut self) {
self.binding.lock().unwrap().shader = None; self.binding.lock().unwrap().shader = None;
} }
fn set_opacity(&mut self, opacity: u8) {
self.binding.lock().unwrap().opacity = opacity;
}
} }
pub struct SurfacePool<S: Surface + Default> { pub struct SurfacePool<S: Surface + Default> {