Add auto move, rework state machine

This commit is contained in:
2024-08-30 00:32:34 -04:00
parent 326660ab43
commit bbec72aeb8
5 changed files with 417 additions and 129 deletions

View File

@ -9,7 +9,7 @@ use std::str::FromStr;
use std::sync::Arc;
use crate::dispatch::{Dispatch, RecvQ, SendQ};
use crate::commands::Commands;
use crate::commands::{Button, Commands};
// TODO HARDWARE: test these values to see if they are suitable
const BLE_MIN_INTERVAL: u16 = 24; // x 1.25ms
@ -37,6 +37,9 @@ const UUID_BUTTON_DOWN: BleUuid = uuid128!("c1401122-8dda-45a3-959b-d23a0f8f53d7
const UUID_BUTTON_STOP: BleUuid = uuid128!("c1401123-8dda-45a3-959b-d23a0f8f53d7");
const UUID_BLUETOOTH_NAME: BleUuid = uuid128!("c1401224-8dda-45a3-959b-d23a0f8f53d7");
const BLE_BUTTON_RELEASE: u8 = 0;
const BLE_BUTTON_PRESS: u8 = 1;
pub struct BleServer {
send_q: SendQ,
recv_q: RecvQ,
@ -48,9 +51,9 @@ pub struct BleServer {
impl BleServer {
pub fn new(dp: &mut Dispatch) -> Self {
let cmds = vec![
Commands::NotifyMotorDown { data: 0 },
Commands::NotifyMotorStop { data: 0 },
Commands::NotifyMotorUp { data: 0 },
Commands::NotifyMotorDown { data: Button::Released },
Commands::NotifyMotorStop { data: Button::Released },
Commands::NotifyMotorUp { data: Button::Released },
Commands::PairTimerExpired,
Commands::AllowPairing,
Commands::EraseBleBonds,
@ -110,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: 0})
}));
// --- Device Name Bluetooth GATT --------------------------------------------------------
let device_name = lift_service.lock().create_characteristic(
@ -131,13 +134,13 @@ impl BleServer {
match cmd {
// TODO DISCUSS: This logic (if one button is pressed others are released) could be done in app instead.
Commands::NotifyMotorUp{data} => {
button_up.lock().set_value(&[data]).notify();
button_up.lock().set_value(&button_to_ble_button(data)).notify();
}
Commands::NotifyMotorDown{data} => {
button_down.lock().set_value(&[data]).notify();
button_down.lock().set_value(&button_to_ble_button(data)).notify();
}
Commands::NotifyMotorStop{data} => {
button_up.lock().set_value(&[data]).notify();
button_up.lock().set_value(&button_to_ble_button(data)).notify();
}
Commands::PairTimerExpired => {
self.advertise_unpairable()?;
@ -216,9 +219,9 @@ fn on_bluetooth_cmd(sender: &SendQ, args: &mut OnWriteArgs, cmd: Commands) {
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: v[0]} )
sender.send_blocking(Commands::BluetoothStop {data: v[0]} )
} else {error!("Received zero-byte bluetooth characteristic update {:?}", cmd); Ok(())}
}
Commands::BluetoothName { data: _ } => {
@ -272,5 +275,12 @@ fn set_server_callbacks(server: &mut BLEServer, sender: SendQ) {
});
}
fn button_to_ble_button(but: Button) -> [u8; 1] {
match but {
Button::Released => {[BLE_BUTTON_RELEASE]}
Button::Pressed => {[BLE_BUTTON_PRESS]}
}
}
//TODO set maximum pairs to remember?
//TODO after disconnect, it returns to scanning - will it return to directed scanning? Find out when directed is working.