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

76 lines
1.7 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 check_event(&self) -> Option<Event>;
2023-11-27 01:57:28 +00:00
fn update_lights(&self, dashboard_lights: DashboardPattern, lights: Pattern);
}
pub trait Animation {
fn tick(&self, time: std::time::Instant) -> (DashboardPattern, Pattern);
2023-11-27 01:57:28 +00:00
}
pub struct DefaultAnimation {}
impl Animation for DefaultAnimation {
fn tick(&self, time: std::time::Instant) -> (DashboardPattern, Pattern) {
2023-11-27 01:57:28 +00:00
(PRIDE_DASHBOARD, PRIDE)
}
}
#[derive(Clone, Debug)]
2023-11-27 01:57:28 +00:00
pub enum Event {
Brake,
BrakeRelease,
LeftBlinker,
NextPattern,
PreviousPattern,
RightBlinker,
}
#[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,
}
}
pub fn tick(&mut self, time: std::time::Instant) {
match self.ui.check_event() {
Some(event) => println!("event received: {:?}", event),
None => (),
};
let (dashboard, lights) = self.current_animation.tick(time);
2023-11-27 01:57:28 +00:00
self.dashboard_lights = dashboard.clone();
self.lights = lights.clone();
self.ui.update_lights(dashboard, lights);
}
}