]> git.tnoah.ca Git - lora-radio.git/commitdiff
Improve BLE serial performance + add BLE battery display
authorjacob.eva <x00010@disroot.org>
Tue, 10 Sep 2024 21:37:26 +0000 (22:37 +0100)
committerjacob.eva <x00010@disroot.org>
Tue, 10 Sep 2024 21:37:26 +0000 (22:37 +0100)
Bluetooth.h
Config.h
Power.h
Utilities.h

index 632996417b27385ecdbbb6d68cbe8289576c5ff1..4761e24f212907cda8cf523b68eab97162ca1fa9 100644 (file)
@@ -307,6 +307,11 @@ bool bt_passkey_callback(uint16_t conn_handle, uint8_t const passkey[6], bool ma
 void bt_connect_callback(uint16_t conn_handle) {
     bt_state = BT_STATE_CONNECTED;
     cable_state = CABLE_STATE_DISCONNECTED;
+
+    BLEConnection* conn = Bluefruit.Connection(conn_handle);
+    conn->requestPHY(BLE_GAP_PHY_2MBPS);
+    conn->requestMtuExchange(512+3);
+    conn->requestDataLengthUpdate();
 }
 
 void bt_disconnect_callback(uint16_t conn_handle, uint8_t reason) {
@@ -342,6 +347,8 @@ bool bt_setup_hw() {
       Bluefruit.Security.setSecuredCallback(bt_connect_callback);
       Bluefruit.Periph.setDisconnectCallback(bt_disconnect_callback);
       Bluefruit.Security.setPairCompleteCallback(bt_pairing_complete);
+      Bluefruit.Periph.setConnInterval(6, 12); // 7.5 - 15 ms
+
       const ble_gap_addr_t gap_addr = Bluefruit.getAddr();
       char *data = (char*)malloc(BT_DEV_ADDR_LEN+1);
       for (int i = 0; i < BT_DEV_ADDR_LEN; i++) {
@@ -372,6 +379,9 @@ void bt_start() {
     // start device information service
     bledis.begin();
 
+    SerialBT.bufferTXD(true); // enable buffering
+
+    SerialBT.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM); // enable encryption for BLE serial
     SerialBT.begin();
 
     blebas.begin();
index 7fe2140e31a0a7c49b15e0c576ec3edeb0e1d802..acc6a8194f2e8dfe8deb441604fb16526dd86e7b 100644 (file)
--- a/Config.h
+++ b/Config.h
@@ -90,6 +90,7 @@
        uint8_t seq                             = 0xFF;
        uint16_t read_len               = 0;
 
+    bool serial_in_frame = false;
 
     FIFOBuffer packet_rdy_interfaces;
 
diff --git a/Power.h b/Power.h
index a6e24007eb3677fcc7af38054a59b0516bcdaa1b..56bcf167443df1dc97f5194fadc8045b4e350771 100644 (file)
--- a/Power.h
+++ b/Power.h
@@ -273,6 +273,16 @@ void measure_battery() {
     if (battery_percent >= 98) {
         battery_state = BATTERY_STATE_CHARGED;
     }
+
+    #if HAS_BLE
+    if ((bt_state == BT_STATE_ON) || bt_state == BT_STATE_CONNECTED) {
+        if (battery_state != BATTERY_STATE_CHARGING) {
+            blebas.write(battery_percent);
+        } else {
+            blebas.write(100);
+        }
+    }
+    #endif
   #endif
 
   if (battery_ready) {
index 1afe6ce76587f2bfe9257313fd013254bc7e67db..84b2de842fa8e14d17ea87d9cc249a1c88798367 100644 (file)
@@ -578,6 +578,16 @@ void serial_write(uint8_t byte) {
                        Serial.write(byte);
                } else {
                        SerialBT.write(byte);
+
+            // This ensures that the TX buffer is flushed after a frame is queued in serial.
+            // serial_in_frame is used to ensure that the flush only happens at the end of the frame
+            if (serial_in_frame && byte == FEND) {
+                SerialBT.flushTXD();
+                serial_in_frame = false;
+            }
+            else if (!serial_in_frame && byte == FEND) {
+                serial_in_frame = true;
+            }
                }
        #else
                Serial.write(byte);