From 29d620e1a69e774dd081d247848a133da52b5af7 Mon Sep 17 00:00:00 2001 From: TeTeHacko Date: Tue, 4 Aug 2026 12:20:28 +0200 Subject: [PATCH] =?UTF-8?q?feat(repeater):=20`blink=20[n]`=20=E2=80=94=20a?= =?UTF-8?q?sk=20a=20board=20to=20identify=20itself?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A bench of identical XIAOs cannot be told apart. The USB serial number needs a data cable, and boards sharing power rails have none; the BLE MAC is printed nowhere; and ADVERT_NAME is only as trustworthy as the last flash, which is usually the thing being checked. Today two boards in the same breadboard were both advertising "bench-board" -- one of them the board with the burned DIO1, still happily running BLE. `blink 10` flashes an LED ten times (default 6, clamped 1..30), and works from serial, the BLE console and the mesh admin CLI, so it also reaches a node that is already mounted somewhere. Deliberately not the status LED. On XIAO that is LED_BLUE, which Bluefruit drives as its connection indicator -- every BLE node blinks blue the same way, which is why the LED was useless for identification in the first place. Preference order LED_GREEN (XIAO, free) -> LED_WHITE (SenseCap, only ever touched at boot and powerOff) -> LED_BUILTIN, which is last because on SenseCap it is pin 12, the LoRa TX indicator. Variants may define a colour they do not physically have by pointing it at PINS_COUNT (SenseCap does that with LED_RED), so the chain range-checks rather than trusting #ifdef. Driven from loop(), not inline: a blocking burst would stall the mesh for up to ~7 s at the cap and disturb the very LoRa timing this fleet measures. Always finishes dark. Goes through LED_STATE_ON, which is 0 on XIAO and 1 on SenseCap -- hard-coding HIGH would leave half the fleet unlit. Verified on bench-board over the USB console: "blink 10" -> "OK - blinking 10 times", and the build stamps v1.16.0-ttha12965f+. Co-Authored-By: Claude Opus 5 --- examples/simple_repeater/MyMesh.cpp | 5 ++ examples/simple_repeater/main.cpp | 99 +++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index ef4041ad..5323f87a 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -1624,6 +1624,11 @@ void MyMesh::handleCommand(uint32_t sender_timestamp, char *command, char *reply formatPktFeedReply(reply, reply_max, cursor); #endif } else{ + // CUSTOM (TeTeHacko): `blink [n]` — flash the LED to identify this board. + // Outside the BLE guard on purpose: a USB-only build needs it just as much, + // since a board with no data cable and no BLE cannot be identified at all. + extern bool identHandleCommand(const char* command, char* reply); + if (identHandleCommand(command, reply)) return; #if defined(BLE_PIN_CODE) && defined(NRF52_PLATFORM) // CUSTOM (TeTeHacko): `ble on|off|ble` — implemented in main.cpp (BLE console). // Available from serial, the BLE console and remotely via MC (admin remote CLI). diff --git a/examples/simple_repeater/main.cpp b/examples/simple_repeater/main.cpp index 1aff3a50..6e45880f 100644 --- a/examples/simple_repeater/main.cpp +++ b/examples/simple_repeater/main.cpp @@ -3,6 +3,102 @@ #include "MyMesh.h" +// =========================================================================== +// CUSTOM (TeTeHacko): `blink [n]` -- make THIS board flash its LED on demand, +// so a bench full of identical boards can be told apart and labelled. +// +// The problem it solves: nothing else identifies a board in situ. The USB +// serial number is only readable over a data cable (and boards sharing power +// rails have none), the BLE MAC is not printed anywhere, and ADVERT_NAME is +// only as trustworthy as the last flash -- which is exactly what you are trying +// to confirm. Two boards here were both advertising "bench-board". +// +// It deliberately does NOT use the status LED. On XIAO that is LED_BLUE +// (PIN_STATUS_LED), which Bluefruit drives as its connection indicator, so +// every BLE node blinks blue identically and a blink there would be lost in the +// noise. LED_GREEN is untouched by the library, so a green burst is unambiguous. +// +// Ticked from loop() rather than looping inline: a blocking blink would stall +// the mesh for the whole burst (up to ~7 s at the 30-blink cap) and disturb the +// LoRa RX/TX timing the test fleet exists to measure. +// =========================================================================== +// Pick an LED nothing else drives, in that order of preference: +// LED_GREEN -- XIAO (13). Free; LED_BLUE there is Bluefruit's connection LED. +// LED_WHITE -- SenseCap Solar (11). Only ever set LOW at boot and at +// powerOff(), so a burst on it is unambiguous. +// LED_BUILTIN -- last resort. On SenseCap this is pin 12 = LED_BLUE = the LoRa +// TX indicator, so a blink there competes with live traffic; +// workable, but that is why it is last. +// A variant may DEFINE a colour it does not physically have by pointing it at +// PINS_COUNT (SenseCap does exactly that with LED_RED), so #ifdef alone is not +// enough -- the range check below is what actually proves the pin exists. +#ifndef RPT_IDENT_LED_PIN + #if defined(LED_GREEN) && (LED_GREEN < PINS_COUNT) + #define RPT_IDENT_LED_PIN LED_GREEN + #elif defined(LED_WHITE) && (LED_WHITE < PINS_COUNT) + #define RPT_IDENT_LED_PIN LED_WHITE + #elif defined(LED_BUILTIN) && (LED_BUILTIN < PINS_COUNT) + #define RPT_IDENT_LED_PIN LED_BUILTIN + #endif +#endif +#ifndef RPT_IDENT_LED_MS + #define RPT_IDENT_LED_MS 120 // half-period; 120 ms reads as a clear flash +#endif + +#ifdef RPT_IDENT_LED_PIN +static uint8_t ident_edges_left = 0; // remaining on/off transitions +static bool ident_led_on = false; +static unsigned long ident_next_ms = 0; +static bool ident_pin_ready = false; + +// LED_STATE_ON is 0 on XIAO (active low) and 1 elsewhere; go through it rather +// than hard-coding HIGH/LOW, or the "blink" is an unlit board on half the fleet. +static inline void identLedWrite(bool on) { + digitalWrite(RPT_IDENT_LED_PIN, on ? LED_STATE_ON : !LED_STATE_ON); +} + +static void identStart(int blinks) { + if (blinks < 1) blinks = 1; + if (blinks > 30) blinks = 30; + if (!ident_pin_ready) { + pinMode(RPT_IDENT_LED_PIN, OUTPUT); + ident_pin_ready = true; + } + ident_edges_left = (uint8_t)(blinks * 2); // one on + one off per blink + ident_led_on = true; + identLedWrite(true); + ident_next_ms = millis() + RPT_IDENT_LED_MS; +} + +static void identTick() { + if (ident_edges_left == 0) return; + if ((long)(millis() - ident_next_ms) < 0) return; + ident_edges_left--; + if (ident_edges_left == 0) { + identLedWrite(false); // always finish dark, never leave it lit + return; + } + ident_led_on = !ident_led_on; + identLedWrite(ident_led_on); + ident_next_ms = millis() + RPT_IDENT_LED_MS; +} +#endif // RPT_IDENT_LED_PIN + +// Reachable from serial, the BLE console AND the mesh admin CLI, so a node that +// is already mounted can still be asked to identify itself. +bool identHandleCommand(const char* command, char* reply) { + if (memcmp(command, "blink", 5) != 0 || (command[5] != 0 && command[5] != ' ')) return false; +#ifdef RPT_IDENT_LED_PIN + int n = (command[5] == ' ') ? atoi(&command[6]) : 6; + if (n == 0) n = 6; + identStart(n); + sprintf(reply, "OK - blinking %d times", n > 30 ? 30 : (n < 1 ? 1 : n)); +#else + strcpy(reply, "ERR: no spare LED on this board"); +#endif + return true; +} + #if defined(BLE_PIN_CODE) && defined(NRF52_PLATFORM) // CUSTOM (TeTeHacko): exposes the repeater text console over BLE Nordic-UART // so the node can be queried wirelessly (advert, neighbor list with SNR) @@ -466,6 +562,9 @@ void loop() { #if defined(NRF52_PLATFORM) NRF_WDT->RR[0] = WDT_RR_RR_Reload; // feed the HW watchdog (setup); if the loop freezes the chip resets in ~20 s #endif +#ifdef RPT_IDENT_LED_PIN + identTick(); // CUSTOM (TeTeHacko): drive `blink` without blocking the mesh +#endif #if defined(BLE_PIN_CODE) && defined(NRF52_PLATFORM) // CUSTOM (TeTeHacko): deferred apply of `ble on|off` — the reply still goes // out over the old path, only then advertising/connections get switched.