events: implement a first attempt at an eventing system

This commit is contained in:
Torrie Fischer
2024-12-13 00:56:50 +01:00
parent 9a749c40a1
commit d7f312ffe4
8 changed files with 236 additions and 30 deletions

View File

@@ -1,7 +1,9 @@
use core::fmt;
use crate::events::{Event, EventBus};
pub trait Task: Send {
fn tick(&mut self) {}
fn tick(&mut self, event: &Event, bus: &mut EventBus) {}
fn start(&mut self) {}
fn stop(&mut self) {}
fn name(&self) -> &'static str {
@@ -55,7 +57,7 @@ impl ScheduledTask {
}
}
fn tick(&mut self) {
fn tick(&mut self, event: &Event, bus: &mut EventBus) {
self.state = match self.state {
ScheduledState::Start => {
log::info!("Starting task {}", self.task.name());
@@ -63,7 +65,7 @@ impl ScheduledTask {
ScheduledState::Running
},
ScheduledState::Running => {
self.task.tick();
self.task.tick(event, bus);
ScheduledState::Running
},
ScheduledState::Stop => {
@@ -96,10 +98,10 @@ impl<const TASK_COUNT: usize> FixedSizeScheduler<TASK_COUNT> {
}
impl<const TASK_COUNT: usize> Scheduler for FixedSizeScheduler<TASK_COUNT> {
fn tick(&mut self) {
fn tick(&mut self, event: &Event, bus: &mut EventBus) {
for slot in &mut self.tasks {
match slot {
Some(task) => task.tick(),
Some(task) => task.tick(event, bus),
_ => ()
}
}
@@ -107,5 +109,5 @@ impl<const TASK_COUNT: usize> Scheduler for FixedSizeScheduler<TASK_COUNT> {
}
pub trait Scheduler {
fn tick(&mut self);
fn tick(&mut self, event: &Event, bus: &mut EventBus);
}