Enable the brake sensor #248

Merged
savanni merged 2 commits from bike-lights__enable_brakes into main 2024-09-16 03:58:54 +00:00
1 changed files with 26 additions and 7 deletions

View File

@ -17,7 +17,7 @@ use rp_pico::{
entry, entry,
hal::{ hal::{
clocks::init_clocks_and_plls, clocks::init_clocks_and_plls,
gpio::{FunctionSio, Pin, PinId, PullUp, SioInput}, gpio::{FunctionSio, Pin, PinId, PullDown, PullUp, SioInput, SioOutput},
pac::{CorePeripherals, Peripherals}, pac::{CorePeripherals, Peripherals},
spi::{Enabled, Spi, SpiDevice, ValidSpiPinout}, spi::{Enabled, Spi, SpiDevice, ValidSpiPinout},
watchdog::Watchdog, watchdog::Watchdog,
@ -65,12 +65,16 @@ struct BikeUI<
RightId: PinId, RightId: PinId,
PreviousId: PinId, PreviousId: PinId,
NextId: PinId, NextId: PinId,
BrakeId: PinId,
> { > {
spi: RefCell<Spi<Enabled, D, P, 8>>, spi: RefCell<Spi<Enabled, D, P, 8>>,
left_blinker_button: DebouncedButton<LeftId>, left_blinker_button: DebouncedButton<LeftId>,
right_blinker_button: DebouncedButton<RightId>, right_blinker_button: DebouncedButton<RightId>,
previous_animation_button: DebouncedButton<PreviousId>, previous_animation_button: DebouncedButton<PreviousId>,
next_animation_button: DebouncedButton<NextId>, next_animation_button: DebouncedButton<NextId>,
brake_sensor: Pin<BrakeId, FunctionSio<SioInput>, PullUp>,
brake_enabled: bool,
} }
impl< impl<
@ -80,7 +84,8 @@ impl<
RightId: PinId, RightId: PinId,
PreviousId: PinId, PreviousId: PinId,
NextId: PinId, NextId: PinId,
> BikeUI<D, P, LeftId, RightId, PreviousId, NextId> BrakeId: PinId,
> BikeUI<D, P, LeftId, RightId, PreviousId, NextId, BrakeId>
{ {
fn new( fn new(
spi: Spi<Enabled, D, P, 8>, spi: Spi<Enabled, D, P, 8>,
@ -88,6 +93,7 @@ impl<
right_blinker_button: Pin<RightId, FunctionSio<SioInput>, PullUp>, right_blinker_button: Pin<RightId, FunctionSio<SioInput>, PullUp>,
previous_animation_button: Pin<PreviousId, FunctionSio<SioInput>, PullUp>, previous_animation_button: Pin<PreviousId, FunctionSio<SioInput>, PullUp>,
next_animation_button: Pin<NextId, FunctionSio<SioInput>, PullUp>, next_animation_button: Pin<NextId, FunctionSio<SioInput>, PullUp>,
brake_sensor: Pin<BrakeId, FunctionSio<SioInput>, PullUp>,
) -> Self { ) -> Self {
Self { Self {
spi: RefCell::new(spi), spi: RefCell::new(spi),
@ -95,6 +101,9 @@ impl<
right_blinker_button: DebouncedButton::new(right_blinker_button), right_blinker_button: DebouncedButton::new(right_blinker_button),
previous_animation_button: DebouncedButton::new(previous_animation_button), previous_animation_button: DebouncedButton::new(previous_animation_button),
next_animation_button: DebouncedButton::new(next_animation_button), next_animation_button: DebouncedButton::new(next_animation_button),
brake_sensor,
brake_enabled: false,
} }
} }
} }
@ -106,10 +115,17 @@ impl<
RightId: PinId, RightId: PinId,
PreviousId: PinId, PreviousId: PinId,
NextId: PinId, NextId: PinId,
> UI for BikeUI<D, P, LeftId, RightId, PreviousId, NextId> BrakeId: PinId,
> UI for BikeUI<D, P, LeftId, RightId, PreviousId, NextId, BrakeId>
{ {
fn check_event(&mut self, current_time: Instant) -> Option<Event> { fn check_event(&mut self, current_time: Instant) -> Option<Event> {
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); self.left_blinker_button.set_debounce(current_time);
Some(Event::LeftBlinker) Some(Event::LeftBlinker)
} else if self.right_blinker_button.is_low(current_time) { } else if self.right_blinker_button.is_low(current_time) {
@ -193,10 +209,13 @@ fn main() -> ! {
embedded_hal::spi::MODE_1, embedded_hal::spi::MODE_1,
); );
let left_blinker_button = pins.gpio17.into_pull_up_input(); let left_blinker_button = pins.gpio16.into_pull_up_input();
let right_blinker_button = pins.gpio16.into_pull_up_input(); let right_blinker_button = pins.gpio17.into_pull_up_input();
let previous_animation_button = pins.gpio27.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 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( let ui = BikeUI::new(
spi, spi,
@ -204,11 +223,11 @@ fn main() -> ! {
right_blinker_button, right_blinker_button,
previous_animation_button, previous_animation_button,
next_animation_button, next_animation_button,
brake_sensor,
); );
let mut app = App::new(Box::new(ui)); let mut app = App::new(Box::new(ui));
let mut led_pin = pins.led.into_push_pull_output();
led_pin.set_high(); led_pin.set_high();
let mut time = Instant::default(); let mut time = Instant::default();