An applicaiton and simulator for a bike lighting system #245

Merged
savanni merged 16 commits from bike-lights into main 2024-08-01 01:32:29 +00:00
3 changed files with 714 additions and 447 deletions
Showing only changes of commit 22f0f9061c - Show all commits

View File

@ -132,7 +132,12 @@ pub enum FadeDirection {
FadeOut,
}
pub struct LeftBlinker {
pub enum BlinkerDirection {
Left,
Right,
}
pub struct Blinker {
fade_in: Fade,
fade_out: Fade,
direction: FadeDirection,
@ -141,31 +146,53 @@ pub struct LeftBlinker {
frames: u8,
}
impl LeftBlinker {
impl Blinker {
fn new(
starting_dashboard: DashboardPattern,
starting_body: Pattern,
direction: BlinkerDirection,
time: std::time::Instant,
) -> Self {
let mut ending_dashboard = starting_dashboard.clone();
match direction {
BlinkerDirection::Left => {
ending_dashboard[0].r = LEFT_DASHBOARD[0].r;
ending_dashboard[0].g = LEFT_DASHBOARD[0].g;
ending_dashboard[0].b = LEFT_DASHBOARD[0].b;
}
BlinkerDirection::Right => {
ending_dashboard[2].r = RIGHT_DASHBOARD[2].r;
ending_dashboard[2].g = RIGHT_DASHBOARD[2].g;
ending_dashboard[2].b = RIGHT_DASHBOARD[2].b;
}
}
let mut ending_body = starting_body.clone();
match direction {
BlinkerDirection::Left => {
for i in 0..30 {
ending_body[i].r = LEFT[i].r;
ending_body[i].g = LEFT[i].g;
ending_body[i].b = LEFT[i].b;
}
}
BlinkerDirection::Right => {
for i in 30..60 {
ending_body[i].r = RIGHT[i].r;
ending_body[i].g = RIGHT[i].g;
ending_body[i].b = RIGHT[i].b;
}
}
}
LeftBlinker {
Blinker {
fade_in: Fade::new(
starting_dashboard.clone(),
starting_body.clone(),
ending_dashboard.clone(),
ending_body.clone(),
LEFT_FRAMES,
BLINKER_FRAMES,
time,
),
fade_out: Fade::new(
@ -173,17 +200,17 @@ impl LeftBlinker {
ending_body.clone(),
starting_dashboard.clone(),
starting_body.clone(),
LEFT_FRAMES,
BLINKER_FRAMES,
time,
),
direction: FadeDirection::FadeIn,
start_time: time,
frames: LEFT_FRAMES,
frames: BLINKER_FRAMES,
}
}
}
impl Animation for LeftBlinker {
impl Animation for Blinker {
fn tick(&mut self, time: std::time::Instant) -> (DashboardPattern, Pattern) {
let frames: u8 = ((time - self.start_time).as_millis() as f64 / FPS as f64) as u8;
if frames > self.frames {
@ -249,7 +276,7 @@ impl App {
}
}
fn update_state(&mut self, time: std::time::Instant) {
fn update_animation(&mut self, time: std::time::Instant) {
match self.state {
State::Pattern(0) => {
self.current_animation = Box::new(Fade::new(
@ -283,21 +310,27 @@ impl App {
));
}
State::LeftBlinker => {
self.current_animation = Box::new(LeftBlinker::new(
self.current_animation = Box::new(Blinker::new(
self.dashboard_lights.clone(),
self.lights.clone(),
BlinkerDirection::Left,
time,
));
}
State::RightBlinker => {
self.current_animation = Box::new(Blinker::new(
self.dashboard_lights.clone(),
self.lights.clone(),
BlinkerDirection::Right,
time,
));
}
State::RightBlinker => (),
State::BrakeLeftBlinker => (),
State::BrakeRightBlinker => (),
}
}
pub fn tick(&mut self, time: std::time::Instant) {
match self.ui.check_event() {
Some(event) => {
fn update_state(&mut self, event: Event) {
match event {
Event::Brake => {
if self.state == State::Brake {
@ -306,7 +339,7 @@ impl App {
self.state = State::Brake;
}
}
Event::BrakeRelease => self.state = State::Pattern(0),
Event::BrakeRelease => self.state = self.home_state.clone(),
Event::LeftBlinker => match self.state {
State::Brake => self.state = State::BrakeLeftBlinker,
State::BrakeLeftBlinker => self.state = State::Brake,
@ -332,9 +365,20 @@ impl App {
}
_ => (),
},
Event::RightBlinker => {}
Event::RightBlinker => match self.state {
State::Brake => self.state = State::BrakeRightBlinker,
State::BrakeRightBlinker => self.state = State::Brake,
State::RightBlinker => self.state = self.home_state.clone(),
_ => self.state = State::RightBlinker,
},
}
self.update_state(time);
}
pub fn tick(&mut self, time: std::time::Instant) {
match self.ui.check_event() {
Some(event) => {
self.update_state(event);
self.update_animation(time);
}
None => {}
};

File diff suppressed because it is too large Load Diff

View File

@ -170,7 +170,9 @@ impl BikeLights {
let s: Self = Object::builder().build();
s.set_width_request(WIDTH);
s.set_height_request(200);
s.set_height_request(640);
let center = WIDTH as f64 / 2.;
s.set_draw_func({
let s = s.clone();
@ -182,7 +184,7 @@ impl BikeLights {
lights[i].g as f64 / 256.,
lights[i].b as f64 / 256.,
);
context.rectangle(5. + 20. * i as f64, 5., 15., 15.);
context.rectangle(center - 45., 5. + 20. * i as f64, 15., 15.);
let _ = context.fill();
}
for i in 0..30 {
@ -191,7 +193,7 @@ impl BikeLights {
lights[i + 30].g as f64 / 256.,
lights[i + 30].b as f64 / 256.,
);
context.rectangle(5. + 20. * i as f64, 30., 15., 15.);
context.rectangle(center + 15., 5. + 20. * (30. - (i + 1) as f64), 15., 15.);
let _ = context.fill();
}
}