Changed message type from u8 to button
This commit is contained in:
@ -95,7 +95,7 @@ impl BleServer {
|
|||||||
);
|
);
|
||||||
button_up.lock().set_value(&[0])
|
button_up.lock().set_value(&[0])
|
||||||
.on_write(closure!(clone sender, |args: &mut OnWriteArgs| {
|
.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 --------------------------------------------------------
|
// --- Button Down Bluetooth GATT --------------------------------------------------------
|
||||||
let button_down = lift_service.lock().create_characteristic(
|
let button_down = lift_service.lock().create_characteristic(
|
||||||
@ -104,7 +104,7 @@ impl BleServer {
|
|||||||
);
|
);
|
||||||
button_down.lock().set_value(&[0])
|
button_down.lock().set_value(&[0])
|
||||||
.on_write(closure!(clone sender, |args: &mut OnWriteArgs| {
|
.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 --------------------------------------------------------
|
// --- Button Stop Bluetooth GATT --------------------------------------------------------
|
||||||
let button_stop = lift_service.lock().create_characteristic(
|
let button_stop = lift_service.lock().create_characteristic(
|
||||||
@ -113,7 +113,7 @@ impl BleServer {
|
|||||||
);
|
);
|
||||||
button_stop.lock().set_value(&[1])
|
button_stop.lock().set_value(&[1])
|
||||||
.on_write(closure!(clone sender, |args: &mut OnWriteArgs| {
|
.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 --------------------------------------------------------
|
// --- Device Name Bluetooth GATT --------------------------------------------------------
|
||||||
let device_name = lift_service.lock().create_characteristic(
|
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.
|
// receiving incorrect data isn't fatal, but being unable to send events is.
|
||||||
let attempt = match cmd {
|
let attempt = match cmd {
|
||||||
Commands::BluetoothUp { data: _ } => {
|
Commands::BluetoothUp { data: _ } => {
|
||||||
if v.len() > 0 {
|
sender.send_blocking(Commands::BluetoothUp {data: ble_to_button(v)} )
|
||||||
sender.send_blocking(Commands::BluetoothUp {data: v[0]} )
|
|
||||||
} else {error!("Received zero-byte bluetooth characteristic update {:?}", cmd); Ok(())}
|
|
||||||
}
|
}
|
||||||
Commands::BluetoothDown { data: _ } => {
|
Commands::BluetoothDown { data: _ } => {
|
||||||
if v.len() > 0 {
|
sender.send_blocking(Commands::BluetoothDown {data: ble_to_button(v)} )
|
||||||
sender.send_blocking(Commands::BluetoothDown {data: v[0]} )
|
|
||||||
} else {error!("Received zero-byte bluetooth characteristic update {:?}", cmd); Ok(())}
|
|
||||||
}
|
}
|
||||||
Commands::BluetoothStop { data: _ } => {
|
Commands::BluetoothStop { data: _ } => {
|
||||||
if v.len() > 0 {
|
sender.send_blocking(Commands::BluetoothStop {data: ble_to_button(v)} )
|
||||||
sender.send_blocking(Commands::BluetoothStop {data: v[0]} )
|
|
||||||
} else {error!("Received zero-byte bluetooth characteristic update {:?}", cmd); Ok(())}
|
|
||||||
}
|
}
|
||||||
Commands::BluetoothName { data: _ } => {
|
Commands::BluetoothName { data: _ } => {
|
||||||
if v.len() > 0 {
|
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) {
|
fn set_device_security(dev: &mut BLEDevice) {
|
||||||
dev.security()
|
dev.security()
|
||||||
// Enable all security protections (including bond, so that bond info is saved)
|
// Enable all security protections (including bond, so that bond info is saved)
|
||||||
|
|||||||
@ -12,19 +12,19 @@ pub enum Commands {
|
|||||||
// TODO: move these to buttons driver, for eventual move to ESP only?
|
// 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
|
// PIC button commands are considered "remote" due to the delay in sending notification
|
||||||
PicRecvUp {data: u8},
|
PicRecvUp {data: Button},
|
||||||
PicRecvDown {data: u8},
|
PicRecvDown {data: Button},
|
||||||
PicRecvStop {data: u8},
|
PicRecvStop {data: Button},
|
||||||
PicRecvLimitUp {data: u8}, // 0 for not hit, 1 for hit
|
PicRecvLimitUp {data: Button}, // 0 for not hit, 1 for hit
|
||||||
PicRecvLimitDown {data: u8}, // 0 for not hit, 1 for hit
|
PicRecvLimitDown {data: Button}, // 0 for not hit, 1 for hit
|
||||||
PicRecvAutoMode {data: u8}, // 0 for disallowed, 1 for allowed
|
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.
|
// 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
|
// Inputs from bluetooth
|
||||||
BluetoothUp {data: u8}, //TODO change these to real button states and change them on input.
|
BluetoothUp {data: Button}, //TODO change these to real button states and change them on input.
|
||||||
BluetoothDown {data: u8},
|
BluetoothDown {data: Button},
|
||||||
BluetoothStop {data: u8}, // There is no state where releasing the stop button induces a change.
|
BluetoothStop {data: Button}, // There is no state where releasing the stop button induces a change.
|
||||||
BluetoothName {data: Arc<String>},
|
BluetoothName {data: Arc<String>},
|
||||||
//TODO: Allow auto mode to be set via bluetooth as well
|
//TODO: Allow auto mode to be set via bluetooth as well
|
||||||
|
|
||||||
|
|||||||
@ -60,15 +60,15 @@ impl Controller {
|
|||||||
/// a callback channel that receives those messages.
|
/// a callback channel that receives those messages.
|
||||||
pub fn prepare_controller(dp: &mut Dispatch) -> RecvQ {
|
pub fn prepare_controller(dp: &mut Dispatch) -> RecvQ {
|
||||||
let cmds = vec![
|
let cmds = vec![
|
||||||
Commands::PicRecvUp{data: 0},
|
Commands::PicRecvUp{data: Button::Released},
|
||||||
Commands::PicRecvDown{data: 0},
|
Commands::PicRecvDown{data: Button::Released},
|
||||||
Commands::PicRecvStop{data: 0},
|
Commands::PicRecvStop{data: Button::Released},
|
||||||
Commands::BluetoothUp{data: 0},
|
Commands::BluetoothUp{data: Button::Released},
|
||||||
Commands::BluetoothDown{data: 0},
|
Commands::BluetoothDown{data: Button::Released},
|
||||||
Commands::BluetoothStop{data: 0},
|
Commands::BluetoothStop{data: Button::Released},
|
||||||
Commands::PicRecvLimitUp{data: 0},
|
Commands::PicRecvLimitUp{data: Button::Released},
|
||||||
Commands::PicRecvLimitDown{data: 0},
|
Commands::PicRecvLimitDown{data: Button::Released},
|
||||||
Commands::PicRecvAutoMode{data: 0},
|
Commands::PicRecvAutoMode{data: Button::Released},
|
||||||
Commands::StopTimerExpired,
|
Commands::StopTimerExpired,
|
||||||
Commands::ButtonTimerExpired,
|
Commands::ButtonTimerExpired,
|
||||||
];
|
];
|
||||||
@ -132,25 +132,17 @@ impl Controller {
|
|||||||
Ok(())
|
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 {
|
match data {
|
||||||
0 => {new_state}
|
Button::Released => {new_state}
|
||||||
1 => {self.state.clone()}
|
Button::Pressed => {self.state.clone()}
|
||||||
_ => {
|
|
||||||
warn!("Data that was not a 0 or 1 received {}", data); //TODO: internal error
|
|
||||||
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 {
|
match data {
|
||||||
0 => {self.state.clone()}
|
Button::Released => {self.state.clone()}
|
||||||
1 => {new_state}
|
Button::Pressed => {new_state}
|
||||||
_ => {
|
|
||||||
warn!("Data that was not a 0 or 1 received {}", data);
|
|
||||||
self.state.clone()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -356,25 +348,24 @@ impl Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_auto(&mut self, data: &u8) {
|
fn set_auto(&mut self, data: &Button) {
|
||||||
match data {
|
match data {
|
||||||
0 => {
|
Button::Released => {
|
||||||
self.auto_mode = AutoMode::Disallowed;
|
self.auto_mode = AutoMode::Disallowed;
|
||||||
}
|
}
|
||||||
1 => {
|
Button::Pressed => {
|
||||||
if self.limit_state == LimitState::BothHit {
|
if self.limit_state == LimitState::BothHit {
|
||||||
warn!("Limit switches not detected. Aborting auto mode.");
|
warn!("Limit switches not detected. Aborting auto mode.");
|
||||||
} else {
|
} else {
|
||||||
self.auto_mode = AutoMode::Allowed;
|
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 {
|
match pressed {
|
||||||
0 => {
|
Button::Released => {
|
||||||
match limit {
|
match limit {
|
||||||
LimitState::NoLimitsHit => {
|
LimitState::NoLimitsHit => {
|
||||||
unreachable!("There is no way to press NoLimits")
|
unreachable!("There is no way to press NoLimits")
|
||||||
@ -400,7 +391,7 @@ impl Controller {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
1 => {
|
Button::Pressed => {
|
||||||
match limit {
|
match limit {
|
||||||
LimitState::NoLimitsHit => {
|
LimitState::NoLimitsHit => {
|
||||||
unreachable!("There is no way to press BothHit")
|
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 {
|
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 {
|
match data {
|
||||||
0 => {warn!("{}", warn);} // TODO: user warning, not intenral
|
Button::Released => {warn!("{}", warn);} // TODO: user warning, not intenral
|
||||||
_ => {}
|
Button::Pressed => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -24,7 +24,7 @@ use ::{
|
|||||||
},
|
},
|
||||||
core::time::Duration,
|
core::time::Duration,
|
||||||
};
|
};
|
||||||
use crate::commands::Commands;
|
use crate::commands::{Button, Commands};
|
||||||
use async_channel::Sender;
|
use async_channel::Sender;
|
||||||
use log::*; //{trace, debug, info, warn, error}
|
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(
|
pub fn process_menu(
|
||||||
cli: &mut CliHandle<'_, SimpleWriter, Infallible>,
|
cli: &mut CliHandle<'_, SimpleWriter, Infallible>,
|
||||||
command: Menu,//<'_>,
|
command: Menu,//<'_>,
|
||||||
@ -109,35 +117,68 @@ pub fn process_menu(
|
|||||||
// We ignore sending errors throughout because the Cli interface is only for
|
// We ignore sending errors throughout because the Cli interface is only for
|
||||||
// testing and debugging.
|
// testing and debugging.
|
||||||
Menu::PicRecvUp {data} => {
|
Menu::PicRecvUp {data} => {
|
||||||
cli.writer().write_str("Sending PicButtonUp Received command")?;
|
let but = input_to_button(data);
|
||||||
let _ = dispatch.send_blocking(Commands::PicRecvUp{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} => {
|
Menu::PicRecvDown{data} => {
|
||||||
cli.writer().write_str("Sending PicButtonDown command")?;
|
let but = input_to_button(data);
|
||||||
let _ = dispatch.send_blocking(Commands::PicRecvDown{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} => {
|
Menu::PicRecvStop{data} => {
|
||||||
cli.writer().write_str("Sending PicButtonStop command")?;
|
let but = input_to_button(data);
|
||||||
let _ = dispatch.send_blocking(Commands::PicRecvStop{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 => {
|
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.
|
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);
|
let _ = dispatch.send_blocking(Commands::AllowPairing);
|
||||||
}
|
}
|
||||||
Menu::BluetoothUp { data } => {
|
Menu::BluetoothUp { data } => {
|
||||||
cli.writer()
|
let but = input_to_button(data);
|
||||||
.write_str("Sending BluetoothUp")?;
|
match but {
|
||||||
let _ = dispatch.send_blocking(Commands::BluetoothUp { data: data });
|
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 } => {
|
Menu::BluetoothDown { data } => {
|
||||||
cli.writer()
|
let but = input_to_button(data);
|
||||||
.write_str("Sending BluetoothDown")?;
|
match but {
|
||||||
let _ = dispatch.send_blocking(Commands::BluetoothDown { data: data });
|
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 } => {
|
Menu::BluetoothStop { data } => {
|
||||||
cli.writer()
|
let but = input_to_button(data);
|
||||||
.write_str("SendingBluetoothStop")?;
|
match but {
|
||||||
let _ = dispatch.send_blocking(Commands::BluetoothStop { data: data });
|
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 } => {
|
Menu::BluetoothLearn { data } => {
|
||||||
cli.writer()
|
cli.writer()
|
||||||
|
|||||||
Reference in New Issue
Block a user