From 4d8a2970c477fce920384e904dfdff827c35d5da Mon Sep 17 00:00:00 2001 From: Dave Date: Fri, 30 Aug 2024 01:05:27 -0400 Subject: [PATCH] Changed message type from u8 to button --- gem-remotes-esp32/src/ble_server.rs | 34 +++++++---- gem-remotes-esp32/src/commands.rs | 18 +++--- gem-remotes-esp32/src/motor_controller.rs | 64 +++++++++----------- gem-remotes-esp32/src/test_console.rs | 73 ++++++++++++++++++----- 4 files changed, 115 insertions(+), 74 deletions(-) diff --git a/gem-remotes-esp32/src/ble_server.rs b/gem-remotes-esp32/src/ble_server.rs index fdd4cad..04db446 100644 --- a/gem-remotes-esp32/src/ble_server.rs +++ b/gem-remotes-esp32/src/ble_server.rs @@ -95,7 +95,7 @@ impl BleServer { ); button_up.lock().set_value(&[0]) .on_write(closure!(clone sender, |args: &mut OnWriteArgs| { - on_bluetooth_cmd(&sender, args, Commands::BluetoothUp {data: 0}) + on_bluetooth_cmd(&sender, args, Commands::BluetoothUp {data: Button::Released}) })); // --- Button Down Bluetooth GATT -------------------------------------------------------- let button_down = lift_service.lock().create_characteristic( @@ -104,7 +104,7 @@ impl BleServer { ); button_down.lock().set_value(&[0]) .on_write(closure!(clone sender, |args: &mut OnWriteArgs| { - on_bluetooth_cmd(&sender, args, Commands::BluetoothDown {data: 0}) + on_bluetooth_cmd(&sender, args, Commands::BluetoothDown {data: Button::Released}) })); // --- Button Stop Bluetooth GATT -------------------------------------------------------- let button_stop = lift_service.lock().create_characteristic( @@ -113,7 +113,7 @@ impl BleServer { ); button_stop.lock().set_value(&[1]) .on_write(closure!(clone sender, |args: &mut OnWriteArgs| { - on_bluetooth_cmd(&sender, args, Commands::BluetoothStop {data: 0}) + on_bluetooth_cmd(&sender, args, Commands::BluetoothStop {data: Button::Released}) })); // --- Device Name Bluetooth GATT -------------------------------------------------------- let device_name = lift_service.lock().create_characteristic( @@ -210,19 +210,13 @@ fn on_bluetooth_cmd(sender: &SendQ, args: &mut OnWriteArgs, cmd: Commands) { // receiving incorrect data isn't fatal, but being unable to send events is. let attempt = match cmd { Commands::BluetoothUp { data: _ } => { - if v.len() > 0 { - sender.send_blocking(Commands::BluetoothUp {data: v[0]} ) - } else {error!("Received zero-byte bluetooth characteristic update {:?}", cmd); Ok(())} + sender.send_blocking(Commands::BluetoothUp {data: ble_to_button(v)} ) } Commands::BluetoothDown { data: _ } => { - if v.len() > 0 { - sender.send_blocking(Commands::BluetoothDown {data: v[0]} ) - } else {error!("Received zero-byte bluetooth characteristic update {:?}", cmd); Ok(())} + sender.send_blocking(Commands::BluetoothDown {data: ble_to_button(v)} ) } Commands::BluetoothStop { data: _ } => { - if v.len() > 0 { - sender.send_blocking(Commands::BluetoothStop {data: v[0]} ) - } else {error!("Received zero-byte bluetooth characteristic update {:?}", cmd); Ok(())} + sender.send_blocking(Commands::BluetoothStop {data: ble_to_button(v)} ) } Commands::BluetoothName { data: _ } => { if v.len() > 0 { @@ -243,6 +237,22 @@ fn on_bluetooth_cmd(sender: &SendQ, args: &mut OnWriteArgs, cmd: Commands) { } } +fn ble_to_button(val: &[u8]) -> Button { + if val.len() > 0 { + match val[0] { + BLE_BUTTON_PRESS => {Button::Pressed} + BLE_BUTTON_RELEASE => {Button::Released} + _ => { + error!("Received invalid bluetooth data {:?}", val); + Button::Released + } + } + } else { + error!("Received zero-length bluetooth data when expecting a button press"); + Button::Released + } +} + fn set_device_security(dev: &mut BLEDevice) { dev.security() // Enable all security protections (including bond, so that bond info is saved) diff --git a/gem-remotes-esp32/src/commands.rs b/gem-remotes-esp32/src/commands.rs index 8862708..5fc6527 100644 --- a/gem-remotes-esp32/src/commands.rs +++ b/gem-remotes-esp32/src/commands.rs @@ -12,19 +12,19 @@ pub enum Commands { // TODO: move these to buttons driver, for eventual move to ESP only? // PIC button commands are considered "remote" due to the delay in sending notification - PicRecvUp {data: u8}, - PicRecvDown {data: u8}, - PicRecvStop {data: u8}, - PicRecvLimitUp {data: u8}, // 0 for not hit, 1 for hit - PicRecvLimitDown {data: u8}, // 0 for not hit, 1 for hit - PicRecvAutoMode {data: u8}, // 0 for disallowed, 1 for allowed + PicRecvUp {data: Button}, + PicRecvDown {data: Button}, + PicRecvStop {data: Button}, + PicRecvLimitUp {data: Button}, // 0 for not hit, 1 for hit + PicRecvLimitDown {data: Button}, // 0 for not hit, 1 for hit + PicRecvAutoMode {data: Button}, // 0 for disallowed, 1 for allowed // TODO: real hardware buttons - consider re-sending occasionally when pressed, so that transitions like holding up -> stopping -> holding down -> stopped -> (should go down but gets no new notice) work. // Inputs from bluetooth - BluetoothUp {data: u8}, //TODO change these to real button states and change them on input. - BluetoothDown {data: u8}, - BluetoothStop {data: u8}, // There is no state where releasing the stop button induces a change. + BluetoothUp {data: Button}, //TODO change these to real button states and change them on input. + BluetoothDown {data: Button}, + BluetoothStop {data: Button}, // There is no state where releasing the stop button induces a change. BluetoothName {data: Arc}, //TODO: Allow auto mode to be set via bluetooth as well diff --git a/gem-remotes-esp32/src/motor_controller.rs b/gem-remotes-esp32/src/motor_controller.rs index d1dbe9e..e81bcd7 100644 --- a/gem-remotes-esp32/src/motor_controller.rs +++ b/gem-remotes-esp32/src/motor_controller.rs @@ -60,15 +60,15 @@ impl Controller { /// a callback channel that receives those messages. pub fn prepare_controller(dp: &mut Dispatch) -> RecvQ { let cmds = vec![ - Commands::PicRecvUp{data: 0}, - Commands::PicRecvDown{data: 0}, - Commands::PicRecvStop{data: 0}, - Commands::BluetoothUp{data: 0}, - Commands::BluetoothDown{data: 0}, - Commands::BluetoothStop{data: 0}, - Commands::PicRecvLimitUp{data: 0}, - Commands::PicRecvLimitDown{data: 0}, - Commands::PicRecvAutoMode{data: 0}, + Commands::PicRecvUp{data: Button::Released}, + Commands::PicRecvDown{data: Button::Released}, + Commands::PicRecvStop{data: Button::Released}, + Commands::BluetoothUp{data: Button::Released}, + Commands::BluetoothDown{data: Button::Released}, + Commands::BluetoothStop{data: Button::Released}, + Commands::PicRecvLimitUp{data: Button::Released}, + Commands::PicRecvLimitDown{data: Button::Released}, + Commands::PicRecvAutoMode{data: Button::Released}, Commands::StopTimerExpired, Commands::ButtonTimerExpired, ]; @@ -132,25 +132,17 @@ impl Controller { Ok(()) } - fn change_state_if_released(&self, data: &u8, new_state: ControllerStates) -> ControllerStates { + fn change_state_if_released(&self, data: &Button, new_state: ControllerStates) -> ControllerStates { match data { - 0 => {new_state} - 1 => {self.state.clone()} - _ => { - warn!("Data that was not a 0 or 1 received {}", data); //TODO: internal error - self.state.clone() - } + Button::Released => {new_state} + Button::Pressed => {self.state.clone()} } } - fn change_state_if_pressed(&self, data: &u8, new_state: ControllerStates) -> ControllerStates { + fn change_state_if_pressed(&self, data: &Button, new_state: ControllerStates) -> ControllerStates { match data { - 0 => {self.state.clone()} - 1 => {new_state} - _ => { - warn!("Data that was not a 0 or 1 received {}", data); - self.state.clone() - } + Button::Released => {self.state.clone()} + Button::Pressed => {new_state} } } @@ -356,25 +348,24 @@ impl Controller { } } - fn set_auto(&mut self, data: &u8) { + fn set_auto(&mut self, data: &Button) { match data { - 0 => { + Button::Released => { self.auto_mode = AutoMode::Disallowed; } - 1 => { + Button::Pressed => { if self.limit_state == LimitState::BothHit { warn!("Limit switches not detected. Aborting auto mode."); } else { self.auto_mode = AutoMode::Allowed; } } - _ => {} // TODO: we should warn, but we're going to fix these types next. } } - fn adjust_limit(&mut self, limit: LimitState, pressed: &u8) { + fn adjust_limit(&mut self, limit: LimitState, pressed: &Button) { match pressed { - 0 => { + Button::Released => { match limit { LimitState::NoLimitsHit => { unreachable!("There is no way to press NoLimits") @@ -400,7 +391,7 @@ impl Controller { } } } - 1 => { + Button::Pressed => { match limit { LimitState::NoLimitsHit => { unreachable!("There is no way to press BothHit") @@ -426,22 +417,21 @@ impl Controller { } } } - _ => {} //TODO should warn but this will go away with real pressed types } } } -fn pressed_warning(data: &u8, warn: &str) { +fn pressed_warning(data: &Button, warn: &str) { match data { - 1 => {warn!("{}", warn);} // TODO: user warning, not intenral - _ => {} + Button::Pressed => {warn!("{}", warn);} // TODO: user warning, not intenral + Button::Released => {} } } -fn released_warning(data: &u8, warn: &str) { +fn released_warning(data: &Button, warn: &str) { match data { - 0 => {warn!("{}", warn);} // TODO: user warning, not intenral - _ => {} + Button::Released => {warn!("{}", warn);} // TODO: user warning, not intenral + Button::Pressed => {} } } \ No newline at end of file diff --git a/gem-remotes-esp32/src/test_console.rs b/gem-remotes-esp32/src/test_console.rs index a18c04c..37785f5 100644 --- a/gem-remotes-esp32/src/test_console.rs +++ b/gem-remotes-esp32/src/test_console.rs @@ -24,7 +24,7 @@ use ::{ }, core::time::Duration, }; -use crate::commands::Commands; +use crate::commands::{Button, Commands}; use async_channel::Sender; use log::*; //{trace, debug, info, warn, error} @@ -100,6 +100,14 @@ pub enum Menu{//<'a> { } +fn input_to_button(i: u8) -> Option