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 {
|
2023-11-27 02:19:09 +00:00
|
|
|
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 {
|
2023-11-27 02:19:09 +00:00
|
|
|
fn tick(&self, time: std::time::Instant) -> (DashboardPattern, Pattern);
|
2023-11-27 01:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DefaultAnimation {}
|
|
|
|
|
|
|
|
impl Animation for DefaultAnimation {
|
2023-11-27 02:19:09 +00:00
|
|
|
fn tick(&self, time: std::time::Instant) -> (DashboardPattern, Pattern) {
|
2023-11-27 01:57:28 +00:00
|
|
|
(PRIDE_DASHBOARD, PRIDE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-27 02:19:09 +00:00
|
|
|
#[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-26 18:03:19 +00:00
|
|
|
}
|
|
|
|
|
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-26 18:03:19 +00:00
|
|
|
|
2023-11-27 02:19:09 +00:00
|
|
|
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);
|
2023-11-26 18:03:19 +00:00
|
|
|
}
|
|
|
|
}
|