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

@@ -23,6 +23,9 @@ use super::Board;
use crate::buffers::BufferedSurfacePool;
use crate::buffers::Pixbuf;
use crate::events::Event;
use crate::events::EventBus;
use crate::lib8::interpolate::lerp8by8;
use crate::mappings::StrideMapping;
use crate::task::FixedSizeScheduler;
use crate::task::Task;
@@ -55,6 +58,10 @@ pub mod i2s {
}
impl<'d> Output for I2SOutput<'d> {
fn on_event(&mut self, event: &crate::events::Event) {
}
fn blank(&mut self) {
self.pixbuf.blank();
}
@@ -307,7 +314,7 @@ impl Task for WifiTask {
self.connect();
}
fn tick(&mut self ) {
fn tick(&mut self, event: &Event, bus: &mut EventBus) {
if self.connection_check.tick() {
let cur_state = *self.state.lock().unwrap();
@@ -322,10 +329,12 @@ impl Task for WifiTask {
log::info!("online: {:?}", cur_state);
self.last_state = cur_state;
}
let now: DateTime<Utc> = std::time::SystemTime::now().into();
log::info!("Current time: {} status={:?}", now.format("%d/%m/%Y %T"), self.ntp.get_sync_status());
match cur_state {
WifiState::Connected => bus.push(Event::new_input_event(crate::events::InputEvent::NetworkOnline)),
_ => bus.push(Event::new_input_event(crate::events::InputEvent::NetworkOffline))
}
}
}
}

View File

@@ -1,6 +1,7 @@
use smart_leds_trait::SmartLedsWrite;
use crate::buffers::Pixbuf;
use crate::events::Variant;
use crate::render::{HardwarePixel, Output, PixelView, Sample};
use crate::power::brightness_for_mw;
use crate::geometry::*;
@@ -18,7 +19,8 @@ pub struct StrideOutput<P: Pixbuf, T: FastWrite> {
pixbuf: P,
stride_map: StrideMapping,
target: T,
max_mw: u32
max_mw: u32,
brightness: u8
}
impl<P: Pixbuf, T: FastWrite> StrideOutput<P, T> {
@@ -28,7 +30,8 @@ impl<P: Pixbuf, T: FastWrite> StrideOutput<P, T> {
pixbuf,
stride_map,
target,
max_mw
max_mw,
brightness: 255
}
}
}
@@ -46,11 +49,18 @@ impl<P: Pixbuf<Pixel=T::Color>, T: FastWrite> Output for StrideOutput<P, T> {
}
fn commit(&mut self) {
let b = brightness_for_mw(self.pixbuf.as_milliwatts(), 255, self.max_mw);
let b = brightness_for_mw(self.pixbuf.as_milliwatts(), self.brightness, self.max_mw);
if self.target.fast_write(self.pixbuf.iter_with_brightness(b)).is_err() {
panic!("Could not write frame!");
};
}
fn on_event(&mut self, event: &crate::events::Event) {
match event {
crate::events::Event::PropertyChange("output.brightness", new_brightness) => self.brightness = new_brightness.clone().into(),
_ => ()
}
}
}
pub trait FastWrite: Send {