From 5acc84e4236f70e39d56192db3bd67b5a881d219 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sun, 8 Sep 2024 12:53:35 -0400 Subject: [PATCH] Enable the brake light --- bike-lights/bike/src/main.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/bike-lights/bike/src/main.rs b/bike-lights/bike/src/main.rs index 8bd6779..f8050b2 100644 --- a/bike-lights/bike/src/main.rs +++ b/bike-lights/bike/src/main.rs @@ -17,7 +17,7 @@ use rp_pico::{ entry, hal::{ clocks::init_clocks_and_plls, - gpio::{FunctionSio, Pin, PinId, PullUp, SioInput}, + gpio::{FunctionSio, Pin, PinId, PullDown, PullUp, SioInput, SioOutput}, pac::{CorePeripherals, Peripherals}, spi::{Enabled, Spi, SpiDevice, ValidSpiPinout}, watchdog::Watchdog, @@ -65,12 +65,16 @@ struct BikeUI< RightId: PinId, PreviousId: PinId, NextId: PinId, + BrakeId: PinId, > { spi: RefCell>, left_blinker_button: DebouncedButton, right_blinker_button: DebouncedButton, previous_animation_button: DebouncedButton, next_animation_button: DebouncedButton, + brake_sensor: Pin, PullUp>, + + brake_enabled: bool, } impl< @@ -80,7 +84,8 @@ impl< RightId: PinId, PreviousId: PinId, NextId: PinId, - > BikeUI + BrakeId: PinId, + > BikeUI { fn new( spi: Spi, @@ -88,6 +93,7 @@ impl< right_blinker_button: Pin, PullUp>, previous_animation_button: Pin, PullUp>, next_animation_button: Pin, PullUp>, + brake_sensor: Pin, PullUp>, ) -> Self { Self { spi: RefCell::new(spi), @@ -95,6 +101,9 @@ impl< right_blinker_button: DebouncedButton::new(right_blinker_button), previous_animation_button: DebouncedButton::new(previous_animation_button), next_animation_button: DebouncedButton::new(next_animation_button), + brake_sensor, + + brake_enabled: false, } } } @@ -106,10 +115,17 @@ impl< RightId: PinId, PreviousId: PinId, NextId: PinId, - > UI for BikeUI + BrakeId: PinId, + > UI for BikeUI { fn check_event(&mut self, current_time: Instant) -> Option { - if self.left_blinker_button.is_low(current_time) { + if self.brake_sensor.is_high().unwrap_or(true) && !self.brake_enabled { + self.brake_enabled = true; + Some(Event::Brake) + } else if self.brake_sensor.is_low().unwrap_or(false) && self.brake_enabled { + self.brake_enabled = false; + Some(Event::BrakeRelease) + } else if self.left_blinker_button.is_low(current_time) { self.left_blinker_button.set_debounce(current_time); Some(Event::LeftBlinker) } else if self.right_blinker_button.is_low(current_time) { @@ -197,6 +213,9 @@ fn main() -> ! { let right_blinker_button = pins.gpio16.into_pull_up_input(); let previous_animation_button = pins.gpio27.into_pull_up_input(); let next_animation_button = pins.gpio26.into_pull_up_input(); + let brake_sensor = pins.gpio18.into_pull_up_input(); + + let mut led_pin = pins.led.into_push_pull_output(); let ui = BikeUI::new( spi, @@ -204,11 +223,11 @@ fn main() -> ! { right_blinker_button, previous_animation_button, next_animation_button, + brake_sensor, ); let mut app = App::new(Box::new(ui)); - let mut led_pin = pins.led.into_push_pull_output(); led_pin.set_high(); let mut time = Instant::default();