task: move Scheduler into a trait, define static-sized scheduler implementation, expose as a system task runner in platform

This commit is contained in:
2024-12-02 19:32:18 +01:00
parent 132d7c0a33
commit f789f6ded9
4 changed files with 41 additions and 31 deletions

View File

@ -18,6 +18,7 @@ use rgb::Rgb;
use super::Board;
use crate::task::FixedSizeScheduler;
use crate::task::Task;
use crate::buffers::{Pixbuf, SurfacePool};
use crate::mappings::StrideMapping;
@ -34,12 +35,14 @@ use crate::buffers::SimpleSurface as SurfaceType;
pub struct Esp32Board<'a> {
output: Option<<Self as Board>::Output>,
surfaces: Option<SurfacePool<SurfaceType>>,
tasks: Option<Vec<Box<dyn Task>>>
sys_loop: EspSystemEventLoop,
modem: Option<Modem>,
}
impl<'a> Board for Esp32Board<'a> {
type Output = StrideOutput<[Rgb<u8>; 310], FastWs2812Esp32Rmt<'a>>;
type Surfaces = SurfacePool<SurfaceType>;
type Scheduler = FixedSizeScheduler<2>;
fn take() -> Self {
// It is necessary to call this function once. Otherwise some patches to the runtime
@ -51,7 +54,6 @@ impl<'a> Board for Esp32Board<'a> {
let peripherals = Peripherals::take().unwrap();
let sys_loop = EspSystemEventLoop::take().unwrap();
let nvs = EspDefaultNvsPartition::take().unwrap();
let channel = peripherals.rmt.channel0;
let pins = peripherals.pins;
@ -74,10 +76,6 @@ impl<'a> Board for Esp32Board<'a> {
}
};
let tasks: Vec<Box<dyn Task>> = vec![
Box::new(WifiTask::new(peripherals.modem, sys_loop.clone(), &nvs)),
];
const POWER_VOLTS : u32 = 5;
const POWER_MA : u32 = 500;
const MAX_POWER_MW : u32 = POWER_VOLTS * POWER_MA;
@ -92,7 +90,8 @@ impl<'a> Board for Esp32Board<'a> {
Esp32Board {
surfaces: Some(SurfacePool::new()),
output: Some(output),
tasks: Some(tasks),
modem: Some(peripherals.modem),
sys_loop: sys_loop.clone(),
}
}
@ -104,8 +103,11 @@ impl<'a> Board for Esp32Board<'a> {
self.surfaces.take().unwrap()
}
fn system_tasks(&mut self) -> Vec<Box<dyn Task>> {
self.tasks.take().unwrap()
fn system_tasks(&mut self) -> Self::Scheduler {
let nvs = EspDefaultNvsPartition::take().unwrap();
FixedSizeScheduler::new([
Box::new(WifiTask::new(self.modem.take().unwrap(), self.sys_loop.clone(), &nvs)),
])
}
}

View File

@ -9,14 +9,15 @@ pub mod esp32;
pub type DefaultBoard = esp32::Esp32Board;
use crate::render::{Output, Surfaces};
use crate::task::Task;
use crate::task::Scheduler;
pub trait Board {
type Output: Output;
type Surfaces: Surfaces;
type Scheduler: Scheduler;
fn take() -> Self;
fn output(&mut self) -> Self::Output;
fn surfaces(&mut self) -> Self::Surfaces;
fn system_tasks(&mut self) -> Vec<Box<dyn Task>>;
fn system_tasks(&mut self) -> Self::Scheduler;
}