events: rewrite the eventing system to reduce mutex usage to just the measurement bus

This commit is contained in:
2025-12-24 09:11:16 +01:00
parent 36f232f43c
commit 046406291a
11 changed files with 185 additions and 284 deletions

View File

@@ -1,5 +1,5 @@
use embassy_sync::pubsub::DynSubscriber;
use embassy_time::{Duration, Timer};
use embassy_time::Duration;
use figments::prelude::*;
use figments_render::output::Brightness;
use rgb::Rgba;
@@ -7,7 +7,7 @@ use core::fmt::Debug;
use futures::join;
use log::*;
use crate::{animation::{AnimDisplay, AnimatedSurface, Animation}, events::{Notification, Prediction}, graphics::{display::{DisplayControls, SegmentSpace, Uniforms}, shaders::*}, tasks::ui::UiSurfacePool};
use crate::{animation::{AnimDisplay, AnimatedSurface, Animation}, events::{Personality, Prediction}, graphics::{display::{DisplayControls, SegmentSpace, Uniforms}, shaders::*}, tasks::ui::UiSurfacePool};
#[derive(Debug)]
pub struct SafetyUi<S: Surface> {
@@ -92,43 +92,35 @@ impl<S: Debug + Surface<Uniforms = Uniforms, CoordinateSpace = SegmentSpace, Pix
info!("Fade out overlay");
TURN_OFF.apply(&mut self.overlay).await;
self.overlay.set_visible(false);
Timer::after_secs(3).await;
warn!("Turning off safety lights");
join!(
TURN_OFF.apply(&mut self.headlight),
TURN_OFF.apply(&mut self.brakelight)
);
info!("Wakeup complete!");
}
pub async fn on_event(&mut self, event: Notification) {
match event {
Notification::SceneChange(_) => (), // We already log this inside apply_scene()
evt => info!("SafetyUI event: {evt:?}")
}
match event {
// Toggling head and brake lights
// FIXME: These should be a Off/Low/High enum, so the stopping brake looks different from the dayrunning brake.
Notification::SetBrakelight(is_on) => {
if is_on {
TURN_ON.apply(&mut self.brakelight).await;
} else {
TURN_OFF.apply(&mut self.brakelight).await;
}
pub async fn on_event(&mut self, event: Prediction) {
if let Prediction::SetPersonality(personality) = event { match personality {
Personality::Active => {
// FIXME: These should be a Off/Low/High enum, so the stopping brake looks different from the dayrunning brake.
warn!("Active personality: Turning on safety lights");
join!(
TURN_ON.apply(&mut self.brakelight),
TURN_ON.apply(&mut self.headlight)
);
},
Notification::SetHeadlight(is_on) => {
if is_on {
TURN_ON.apply(&mut self.headlight).await;
} else {
TURN_OFF.apply(&mut self.headlight).await;
}
Personality::Parked => {
warn!("Idle personality: Turning off safety lights");
join!(
TURN_OFF.apply(&mut self.brakelight),
TURN_OFF.apply(&mut self.headlight)
);
},
Notification::Sleep => self.sleep().await,
Notification::WakeUp => self.wake().await,
_ => ()
}
Personality::Sleeping => {
warn!("Sleeping personality: Safety UI is going to sleep");
self.sleep().await;
},
Personality::Waking => {
warn!("Waking personality: Waking up safety UI");
self.wake().await;
},
} }
}
}
@@ -136,7 +128,7 @@ const TURN_ON: Animation = Animation::new().duration(Duration::from_secs(1)).fro
const TURN_OFF: Animation = Animation::new().duration(Duration::from_secs(1)).from(255).to(0);
#[embassy_executor::task]
pub async fn safety_ui_main(mut events: DynSubscriber<'static, Notification>, mut ui: SafetyUi<<UiSurfacePool as Surfaces<SegmentSpace>>::Surface>) {
pub async fn safety_ui_main(mut events: DynSubscriber<'static, Prediction>, mut ui: SafetyUi<<UiSurfacePool as Surfaces<SegmentSpace>>::Surface>) {
// Wait for the renderer to start running
//ui.display.render_is_running.wait().await;
trace!("spooling until render starts ui={ui:?}");