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; fn update_lights(&self, dashboard_lights: DashboardPattern, lights: Pattern); } pub trait Animation { fn tick(&self, time: std::time::Instant) -> (DashboardPattern, Pattern); } pub struct DefaultAnimation {} impl Animation for DefaultAnimation { fn tick(&self, time: std::time::Instant) -> (DashboardPattern, Pattern) { (PRIDE_DASHBOARD, PRIDE) } } #[derive(Clone, Debug)] 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, state: State, current_animation: Box, dashboard_lights: DashboardPattern, lights: Pattern, } impl App { pub fn new(ui: Box) -> 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); self.dashboard_lights = dashboard.clone(); self.lights = lights.clone(); self.ui.update_lights(dashboard, lights); } }