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)