Skip to content

Commit 2b32b79

Browse files
committed
Decouple BLE UI updates; expose pairing state
Remove direct UI calls from BLE core and move BLE pairing icon management into the LVGL UI task. Added isBLEPairingModeActive() to ble_core.h/.cpp so the UI can query pairing state, removed updateBLEStatusIcon() and its calls from ble_core.cpp to decouple BLE logic from display code, and updated lvgl_updates.cpp to include BLE headers and keep the pairing icon in sync (managing the flash timer and visibility). This reduces cross-module coupling and ensures missed callback updates are recovered by the UI task.
1 parent 496e2f2 commit 2b32b79

3 files changed

Lines changed: 35 additions & 26 deletions

File tree

inc/sp140/ble/ble_core.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ void requestNormalConnParams();
1717
// Temporarily disable whitelist filtering so a new device can bond.
1818
// Advertising reopens for ~60 seconds then whitelisting is restored.
1919
void enterBLEPairingMode();
20+
bool isBLEPairingModeActive();
2021

2122
#endif // INC_SP140_BLE_BLE_CORE_H_

src/sp140/ble/ble_core.cpp

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <freertos/timers.h>
99

1010
#include "sp140/ble.h"
11-
#include "sp140/lvgl/lvgl_updates.h"
1211
#include "sp140/ble/ble_ids.h"
1312
#include "sp140/ble/config_service.h"
1413
#include "sp140/ble/fastlink_service.h"
@@ -38,7 +37,6 @@ uint16_t activeConnHandle = 0;
3837

3938
bool shouldAdvertiseWhilePowered();
4039
bool startAdvertising(NimBLEServer *server);
41-
void updateBLEStatusIcon();
4240

4341
// Builds gAdvertisingName from the BT MAC and returns the full MAC address
4442
// as an uppercase string for use as a unique device ID. Must be called before
@@ -153,22 +151,8 @@ bool shouldAdvertiseWhilePowered() {
153151
(pairingModeActive || reconnectWindowActive);
154152
}
155153

156-
void updateBLEStatusIcon() {
157-
if (deviceConnected) {
158-
showBLEStatusIcon();
159-
return;
160-
}
161-
162-
if (pairingModeActive) {
163-
startBLEPairingIconFlash();
164-
} else {
165-
hideBLEStatusIcon();
166-
}
167-
}
168-
169154
bool startAdvertising(NimBLEServer *server) {
170155
if (server == nullptr || pairingModeTransitionActive) {
171-
updateBLEStatusIcon();
172156
return false;
173157
}
174158

@@ -185,14 +169,12 @@ bool startAdvertising(NimBLEServer *server) {
185169
if (!allowOpenAdvertising && bondCount == 0) {
186170
USBSerial.println(
187171
"[BLE] No bonds present and pairing mode inactive; advertising stopped");
188-
updateBLEStatusIcon();
189172
return false;
190173
}
191174

192175
if (!allowOpenAdvertising && bondCount > 0 && !reconnectWindowActive) {
193176
USBSerial.println(
194177
"[BLE] Reconnect window inactive; whitelist advertising not started");
195-
updateBLEStatusIcon();
196178
return false;
197179
}
198180

@@ -230,7 +212,6 @@ bool startAdvertising(NimBLEServer *server) {
230212
configured, started,
231213
allowOpenAdvertising ? "OPEN" : "BONDED",
232214
static_cast<unsigned>(bondCount), static_cast<unsigned>(whiteListCount));
233-
updateBLEStatusIcon();
234215
return started;
235216
#else
236217
// Configure payload once — NimBLE accumulates addServiceUUID calls
@@ -260,7 +241,6 @@ bool startAdvertising(NimBLEServer *server) {
260241
allowOpenAdvertising ? "OPEN" : "BONDED",
261242
static_cast<unsigned>(bondCount),
262243
static_cast<unsigned>(whiteListCount));
263-
updateBLEStatusIcon();
264244
return started;
265245
#endif
266246
}
@@ -304,7 +284,6 @@ class BleServerConnectionCallbacks : public NimBLEServerCallbacks {
304284
connectedHandle, connInfo.getAddress().toString().c_str(),
305285
connInfo.isBonded() ? 1 : 0, connInfo.isEncrypted() ? 1 : 0,
306286
pairingModeActive ? 1 : 0);
307-
updateBLEStatusIcon();
308287

309288
// During pairing mode, proactively request fresh security negotiation.
310289
// This helps recover from stale iOS bonds where iOS tries to restore
@@ -342,7 +321,6 @@ class BleServerConnectionCallbacks : public NimBLEServerCallbacks {
342321
}
343322
startAdvertising(server);
344323
}
345-
updateBLEStatusIcon();
346324
}
347325

348326
void onAuthenticationComplete(NimBLEConnInfo &connInfo) override {
@@ -380,7 +358,6 @@ class BleServerConnectionCallbacks : public NimBLEServerCallbacks {
380358
}
381359
}
382360
}
383-
updateBLEStatusIcon();
384361
}
385362

386363
void onIdentity(NimBLEConnInfo &connInfo) override {
@@ -447,8 +424,6 @@ void setupBLE() {
447424
initReconnectWindowFromBoot();
448425
if (shouldAdvertiseWhilePowered()) {
449426
restartBLEAdvertising();
450-
} else {
451-
updateBLEStatusIcon();
452427
}
453428
}
454429

@@ -470,7 +445,6 @@ void requestNormalConnParams() {
470445

471446
void restartBLEAdvertising() {
472447
if (pServer == nullptr) {
473-
updateBLEStatusIcon();
474448
return;
475449
}
476450

@@ -531,3 +505,5 @@ void enterBLEPairingMode() {
531505
USBSerial.println("[BLE] Pairing mode active for 60s");
532506
restartBLEAdvertising();
533507
}
508+
509+
bool isBLEPairingModeActive() { return pairingModeActive; }

src/sp140/lvgl/lvgl_updates.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "../../../inc/sp140/globals.h"
55
#include "../../../inc/sp140/vibration_pwm.h"
66
#include "../../../inc/sp140/shared-config.h"
7+
#include "../../../inc/sp140/ble.h"
8+
#include "../../../inc/sp140/ble/ble_core.h"
79
#include <math.h>
810

911
// Flash timer globals - definitions
@@ -851,6 +853,36 @@ void updateLvglMainScreen(
851853
lv_obj_set_style_image_recolor(arm_fail_warning_icon_img, original_arm_fail_icon_color, LV_PART_MAIN);
852854
}
853855

856+
// Keep the BLE icon synced from the UI task so missed callback updates recover.
857+
if (ble_pairing_icon != NULL) {
858+
if (deviceConnected) {
859+
if (ble_pairing_flash_timer != NULL) {
860+
lv_timer_del(ble_pairing_flash_timer);
861+
ble_pairing_flash_timer = NULL;
862+
}
863+
lv_obj_remove_flag(ble_pairing_icon, LV_OBJ_FLAG_HIDDEN);
864+
isFlashingBLEPairingIcon = false;
865+
} else if (isBLEPairingModeActive()) {
866+
if (!isFlashingBLEPairingIcon) {
867+
isFlashingBLEPairingIcon = true;
868+
lv_obj_remove_flag(ble_pairing_icon, LV_OBJ_FLAG_HIDDEN);
869+
ble_pairing_flash_timer = lv_timer_create(ble_pairing_flash_timer_cb, 500, NULL);
870+
if (ble_pairing_flash_timer == NULL) {
871+
isFlashingBLEPairingIcon = false;
872+
lv_obj_add_flag(ble_pairing_icon, LV_OBJ_FLAG_HIDDEN);
873+
USBSerial.println("Error: Failed to create BLE pairing flash timer!");
874+
}
875+
}
876+
} else {
877+
if (ble_pairing_flash_timer != NULL) {
878+
lv_timer_del(ble_pairing_flash_timer);
879+
ble_pairing_flash_timer = NULL;
880+
}
881+
lv_obj_add_flag(ble_pairing_icon, LV_OBJ_FLAG_HIDDEN);
882+
isFlashingBLEPairingIcon = false;
883+
}
884+
}
885+
854886
// Update climb rate indicator
855887
static float lastAltitude = 0.0f;
856888
static uint32_t lastAltitudeTime = 0;

0 commit comments

Comments
 (0)