Changed message type from u8 to button

This commit is contained in:
2024-08-30 01:05:27 -04:00
parent bbec72aeb8
commit 4d8a2970c4
4 changed files with 115 additions and 74 deletions

View File

@ -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)

View File

@ -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<String>},
//TODO: Allow auto mode to be set via bluetooth as well

View File

@ -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 => {}
}
}

View File

@ -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<Button> {
match i {
0 => {Some(Button::Released)}
1 => {Some(Button::Pressed)}
_ => {None}
}
}
pub fn process_menu(
cli: &mut CliHandle<'_, SimpleWriter, Infallible>,
command: Menu,//<'_>,
@ -109,35 +117,68 @@ pub fn process_menu(
// We ignore sending errors throughout because the Cli interface is only for
// testing and debugging.
Menu::PicRecvUp {data} => {
cli.writer().write_str("Sending PicButtonUp Received command")?;
let _ = dispatch.send_blocking(Commands::PicRecvUp{data});
let but = input_to_button(data);
match but {
Some(d) => {
println!("Sending PicRecvUp command");
let _ = dispatch.send_blocking(Commands::PicRecvUp{data: d});
}
None => {println!("Incorrect value; enter 0 or 1")}
}
}
Menu::PicRecvDown{data} => {
cli.writer().write_str("Sending PicButtonDown command")?;
let _ = dispatch.send_blocking(Commands::PicRecvDown{data});
let but = input_to_button(data);
match but {
Some(d) => {
println!("Sending PicRecvUp command");
let _ = dispatch.send_blocking(Commands::PicRecvDown{data: d});
}
None => {println!("Incorrect value; enter 0 or 1")}
}
}
Menu::PicRecvStop{data} => {
cli.writer().write_str("Sending PicButtonStop command")?;
let _ = dispatch.send_blocking(Commands::PicRecvStop{data});
let but = input_to_button(data);
match but {
Some(d) => {
println!("Sending PicRecvUp command");
let _ = dispatch.send_blocking(Commands::PicRecvStop{data: d});
}
None => {println!("Incorrect value; enter 0 or 1")}
}
}
Menu::PicRecvPair => {
cli.writer().write_str("Sending PIC command and timer reset (the job of the pair button driver, once PIC interface exists)")?; //TODO: PIC get this.
let _ = dispatch.send_blocking(Commands::AllowPairing);
}
Menu::BluetoothUp { data } => {
cli.writer()
.write_str("Sending BluetoothUp")?;
let _ = dispatch.send_blocking(Commands::BluetoothUp { data: data });
let but = input_to_button(data);
match but {
Some(d) => {
println!("Sending PicRecvUp command");
let _ = dispatch.send_blocking(Commands::BluetoothUp{data: d});
}
None => {println!("Incorrect value; enter 0 or 1")}
}
}
Menu::BluetoothDown { data } => {
cli.writer()
.write_str("Sending BluetoothDown")?;
let _ = dispatch.send_blocking(Commands::BluetoothDown { data: data });
let but = input_to_button(data);
match but {
Some(d) => {
println!("Sending PicRecvUp command");
let _ = dispatch.send_blocking(Commands::BluetoothDown{data: d});
}
None => {println!("Incorrect value; enter 0 or 1")}
}
}
Menu::BluetoothStop { data } => {
cli.writer()
.write_str("SendingBluetoothStop")?;
let _ = dispatch.send_blocking(Commands::BluetoothStop { data: data });
let but = input_to_button(data);
match but {
Some(d) => {
println!("Sending PicRecvUp command");
let _ = dispatch.send_blocking(Commands::BluetoothStop{data: d});
}
None => {println!("Incorrect value; enter 0 or 1")}
}
}
Menu::BluetoothLearn { data } => {
cli.writer()