monorepo/bike-lights/core/src/lib.rs

73 lines
1.5 KiB
Rust
Raw Normal View History

2023-11-27 01:57:28 +00:00
use std::time::Duration;
mod patterns;
pub use patterns::*;
mod types;
pub use types::{DashboardPattern, Pattern, RGB};
pub trait UI {
fn update_lights(&self, dashboard_lights: DashboardPattern, lights: Pattern);
}
pub trait Animation {
fn tick(&self, frame: Duration) -> (DashboardPattern, Pattern);
}
pub struct DefaultAnimation {}
impl Animation for DefaultAnimation {
fn tick(&self, _: Duration) -> (DashboardPattern, Pattern) {
(PRIDE_DASHBOARD, PRIDE)
}
}
#[derive(Clone)]
pub enum Event {
Brake,
BrakeRelease,
LeftBlinker,
LeftBlinkerRelease,
NextPattern,
PreviousPattern,
RightBlinker,
RightBlinkerRelease,
}
#[derive(Clone)]
pub enum State {
Pattern(u8),
Brake,
LeftBlinker,
RightBlinker,
BrakeLeftBlinker,
BrakeRightBlinker,
}
pub struct App {
ui: Box<dyn UI>,
state: State,
current_animation: Box<dyn Animation>,
dashboard_lights: DashboardPattern,
lights: Pattern,
}
2023-11-27 01:57:28 +00:00
impl App {
pub fn new(ui: Box<dyn UI>) -> Self {
Self {
ui,
state: State::Pattern(0),
current_animation: Box::new(DefaultAnimation {}),
dashboard_lights: OFF_DASHBOARD,
lights: OFF,
}
}
2023-11-27 01:57:28 +00:00
pub fn tick(&mut self, frame: Duration) {
let (dashboard, lights) = self.current_animation.tick(frame);
self.dashboard_lights = dashboard.clone();
self.lights = lights.clone();
self.ui.update_lights(dashboard, lights);
}
}