Improve BLE serial performance + add BLE battery display

This commit is contained in:
jacob.eva 2024-09-10 22:37:26 +01:00
parent c9fd5f7d3b
commit 7f787d5910
No known key found for this signature in database
GPG Key ID: 0B92E083BBCCAA1E
4 changed files with 31 additions and 0 deletions

View 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) { void bt_connect_callback(uint16_t conn_handle) {
bt_state = BT_STATE_CONNECTED; bt_state = BT_STATE_CONNECTED;
cable_state = CABLE_STATE_DISCONNECTED; 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) { 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.Security.setSecuredCallback(bt_connect_callback);
Bluefruit.Periph.setDisconnectCallback(bt_disconnect_callback); Bluefruit.Periph.setDisconnectCallback(bt_disconnect_callback);
Bluefruit.Security.setPairCompleteCallback(bt_pairing_complete); Bluefruit.Security.setPairCompleteCallback(bt_pairing_complete);
Bluefruit.Periph.setConnInterval(6, 12); // 7.5 - 15 ms
const ble_gap_addr_t gap_addr = Bluefruit.getAddr(); const ble_gap_addr_t gap_addr = Bluefruit.getAddr();
char *data = (char*)malloc(BT_DEV_ADDR_LEN+1); char *data = (char*)malloc(BT_DEV_ADDR_LEN+1);
for (int i = 0; i < BT_DEV_ADDR_LEN; i++) { for (int i = 0; i < BT_DEV_ADDR_LEN; i++) {
@ -372,6 +379,9 @@ void bt_start() {
// start device information service // start device information service
bledis.begin(); bledis.begin();
SerialBT.bufferTXD(true); // enable buffering
SerialBT.setPermission(SECMODE_ENC_WITH_MITM, SECMODE_ENC_WITH_MITM); // enable encryption for BLE serial
SerialBT.begin(); SerialBT.begin();
blebas.begin(); blebas.begin();

View File

@ -90,6 +90,7 @@
uint8_t seq = 0xFF; uint8_t seq = 0xFF;
uint16_t read_len = 0; uint16_t read_len = 0;
bool serial_in_frame = false;
FIFOBuffer packet_rdy_interfaces; FIFOBuffer packet_rdy_interfaces;

10
Power.h
View File

@ -273,6 +273,16 @@ void measure_battery() {
if (battery_percent >= 98) { if (battery_percent >= 98) {
battery_state = BATTERY_STATE_CHARGED; 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 #endif
if (battery_ready) { if (battery_ready) {

View File

@ -578,6 +578,16 @@ void serial_write(uint8_t byte) {
Serial.write(byte); Serial.write(byte);
} else { } else {
SerialBT.write(byte); 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 #else
Serial.write(byte); Serial.write(byte);