From 72c07df8b3adb6319bbc41e54df9f3786ff7ac91 Mon Sep 17 00:00:00 2001 From: Jakub <106778416+MarekZegare4@users.noreply.github.com> Date: Thu, 21 May 2026 22:08:28 +0200 Subject: [PATCH] fix: code review corrections in DualSerialInterface and NearbyScreen - DualSerialInterface: guard writeFrame/isWriteBusy/isBLEConnected with _ble_enabled to avoid BLE-after-disable race - NearbyScreen: initialize all fields in constructor; promote D_* layout constants to class scope; clamp _dscroll on result count change; replace hardcoded 2 with D_VISIBLE in scroll logic; allow re-scan from detail view via KEY_CONTEXT_MENU/KEY_ENTER - variant.cpp: fix inconsistent indentation in initVariant() Co-Authored-By: Claude Sonnet 4.6 --- .../companion_radio/ui-new/NearbyScreen.h | 28 +++++++++++++------ src/helpers/nrf52/DualSerialInterface.h | 6 ++-- variants/wio-tracker-l1/variant.cpp | 1 - 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/examples/companion_radio/ui-new/NearbyScreen.h b/examples/companion_radio/ui-new/NearbyScreen.h index 2e3c0c51..1f656d27 100644 --- a/examples/companion_radio/ui-new/NearbyScreen.h +++ b/examples/companion_radio/ui-new/NearbyScreen.h @@ -54,6 +54,11 @@ class NearbyScreen : public UIScreen { int _dsel; bool _ddetail; + static const int D_BOX_H = 19; + static const int D_ITEM_H = 21; + static const int D_VISIBLE = 2; + static const int D_START_Y = 11; + // ── helpers ────────────────────────────────────────────────────────────────── static void pubKeyToBase64(const uint8_t* key, char* out, int out_len) { static const char T[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -188,11 +193,6 @@ class NearbyScreen : public UIScreen { } int renderDiscover(DisplayDriver& display) { - static const int D_BOX_H = 19; - static const int D_ITEM_H = 21; - static const int D_VISIBLE = 2; - static const int D_START_Y = 11; - if (_ddetail) { // ── full-screen detail for selected node ────────────────────────────── const DiscoverResult& r = _dresults[_dsel]; @@ -257,6 +257,9 @@ class NearbyScreen : public UIScreen { _discovering ? "Waiting for replies..." : "No nodes found"); } else { if (_dsel >= _dresult_count) _dsel = _dresult_count - 1; + if (_dscroll > _dresult_count - D_VISIBLE) + _dscroll = _dresult_count > D_VISIBLE ? _dresult_count - D_VISIBLE : 0; + if (_dscroll < 0) _dscroll = 0; for (int i = 0; i < D_VISIBLE && (_dscroll + i) < _dresult_count; i++) { int idx = _dscroll + i; bool sel = (idx == _dsel); @@ -312,7 +315,11 @@ class NearbyScreen : public UIScreen { bool handleInputDiscover(char c) { if (_ddetail) { - if (c == KEY_CANCEL || c == KEY_CONTEXT_MENU) { _ddetail = false; return true; } + if (c == KEY_CANCEL) { _ddetail = false; return true; } + if (c == KEY_CONTEXT_MENU || c == KEY_ENTER) { + enterDiscoverMode(); // re-scan; clears _ddetail + return true; + } return true; } if (c == KEY_CANCEL) { @@ -335,7 +342,7 @@ class NearbyScreen : public UIScreen { } if (c == KEY_DOWN && _dsel < _dresult_count - 1) { _dsel++; - if (_dsel >= _dscroll + 2) _dscroll = _dsel - 1; + if (_dsel >= _dscroll + D_VISIBLE) _dscroll = _dsel - D_VISIBLE + 1; return true; } return true; @@ -343,8 +350,11 @@ class NearbyScreen : public UIScreen { public: NearbyScreen(UITask* task) - : _task(task), _filter(0), _discover_mode(false), _discovering(false), - _ddetail(false), _dsel(0) {} + : _task(task), _count(0), _sel(0), _scroll(0), _detail(false), + _own_lat(0), _own_lon(0), _own_gps(false), _filter(0), + _detail_refresh_ms(0), + _discover_mode(false), _discovering(false), _discover_started_ms(0), + _dresult_count(0), _dscroll(0), _dsel(0), _ddetail(false) {} void enter() { _sel = _scroll = 0; diff --git a/src/helpers/nrf52/DualSerialInterface.h b/src/helpers/nrf52/DualSerialInterface.h index 6fed9e52..fe239552 100644 --- a/src/helpers/nrf52/DualSerialInterface.h +++ b/src/helpers/nrf52/DualSerialInterface.h @@ -32,14 +32,14 @@ public: // Always true — USB is always available as fallback, so the mesh can send. bool isConnected() const override { return true; } // True only when a BLE companion app is paired and connected. - bool isBLEConnected() const override { return _ble.isConnected(); } + bool isBLEConnected() const override { return _ble_enabled && _ble.isConnected(); } bool isWriteBusy() const override { - return _ble.isConnected() ? _ble.isWriteBusy() : _usb.isWriteBusy(); + return (_ble_enabled && _ble.isConnected()) ? _ble.isWriteBusy() : _usb.isWriteBusy(); } size_t writeFrame(const uint8_t src[], size_t len) override { - return _ble.isConnected() ? _ble.writeFrame(src, len) : _usb.writeFrame(src, len); + return (_ble_enabled && _ble.isConnected()) ? _ble.writeFrame(src, len) : _usb.writeFrame(src, len); } size_t checkRecvFrame(uint8_t dest[]) override { diff --git a/variants/wio-tracker-l1/variant.cpp b/variants/wio-tracker-l1/variant.cpp index d5a77736..d09e4a90 100644 --- a/variants/wio-tracker-l1/variant.cpp +++ b/variants/wio-tracker-l1/variant.cpp @@ -78,5 +78,4 @@ void initVariant() { // set buzzer pin as output and set it low pinMode(12, OUTPUT); digitalWrite(12, LOW); - pinMode(12, OUTPUT); }