diff --git a/Bluetooth.h b/Bluetooth.h index 6329964..4761e24 100644 --- a/Bluetooth.h +++ b/Bluetooth.h @@ -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(); diff --git a/Config.h b/Config.h index 7fe2140..acc6a81 100644 --- 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 a6e2400..56bcf16 100644 --- 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) { diff --git a/Utilities.h b/Utilities.h index 1afe6ce..84b2de8 100644 --- a/Utilities.h +++ b/Utilities.h @@ -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);