refactor(bot,ui): dedupe bot token parsing, drop redundant turnOn, fix comment

Post-review cleanups, no behaviour change:
- botScanCommands() parsed the command name and its two args with three
  near-identical read-token loops; extracted a single readToken() lambda.
- Fn+Esc lock branch turned the display on twice (the unlock arm repeated
  what the branch head already did); dropped the redundant call.
- setGpioMode()'s comment said "Cycle" (cycling lives in GpioScreen); now
  describes what it actually does — set a specific mode + persist.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Jakub
2026-07-25 00:00:07 +02:00
parent 0a61b7ef15
commit 5844fa2390
2 changed files with 16 additions and 18 deletions

View File

@@ -388,6 +388,15 @@ int MyMesh::botScanCommands(const char* body, uint8_t hops, uint32_t ts, char* o
const char* sender_name, bool actions_allowed) { const char* sender_name, bool actions_allowed) {
resetPendingBotActions(); // pending actions/flags below are set by tokens in this scan; resetPendingBotActions(); // pending actions/flags below are set by tokens in this scan;
// caller applies or clears them once it knows if the reply sent // caller applies or clears them once it knows if the reply sent
// Reads one space-delimited token at p into buf (lowercased, truncated to
// cap-1 chars with any overflow consumed too), leaving p on the space/NUL
// that ended it. Shared by the command name and its two optional args.
auto readToken = [](const char*& p, char* buf, int cap) {
int i = 0;
while (*p && *p != ' ' && i < cap - 1) buf[i++] = (char)tolower((uint8_t)*p++);
buf[i] = '\0';
while (*p && *p != ' ') p++;
};
int oi = 0; int oi = 0;
int matched = 0; int matched = 0;
for (const char* p = body; *p; ) { for (const char* p = body; *p; ) {
@@ -398,11 +407,7 @@ int MyMesh::botScanCommands(const char* body, uint8_t hops, uint32_t ts, char* o
} }
p++; // step past '!' p++; // step past '!'
char cmd[16]; char cmd[16];
int ci = 0; readToken(p, cmd, sizeof(cmd));
while (*p && *p != ' ' && ci < (int)sizeof(cmd) - 1)
cmd[ci++] = (char)tolower((uint8_t)*p++);
cmd[ci] = '\0';
while (*p && *p != ' ') p++; // consume any overflow of this token
if (!cmd[0]) continue; if (!cmd[0]) continue;
// Up to two optional arguments: the next one or two space-delimited // Up to two optional arguments: the next one or two space-delimited
@@ -416,17 +421,9 @@ int MyMesh::botScanCommands(const char* body, uint8_t hops, uint32_t ts, char* o
while (*p == ' ') p++; while (*p == ' ') p++;
char arg1[16] = "", arg2[16] = ""; char arg1[16] = "", arg2[16] = "";
if (*p && *p != '!') { if (*p && *p != '!') {
int ai = 0; readToken(p, arg1, sizeof(arg1));
while (*p && *p != ' ' && ai < (int)sizeof(arg1) - 1) arg1[ai++] = (char)tolower((uint8_t)*p++);
arg1[ai] = '\0';
while (*p && *p != ' ') p++; // consume any overflow of this token
while (*p == ' ') p++; while (*p == ' ') p++;
if (*p && *p != '!') { if (*p && *p != '!') readToken(p, arg2, sizeof(arg2));
int bi = 0;
while (*p && *p != ' ' && bi < (int)sizeof(arg2) - 1) arg2[bi++] = (char)tolower((uint8_t)*p++);
arg2[bi] = '\0';
while (*p && *p != ' ') p++; // consume any overflow of this token
}
} }
char seg[80]; char seg[80];

View File

@@ -2146,7 +2146,6 @@ void UITask::pollCardKB() {
if (_locked) { if (_locked) {
_lock_wake_until = millis() + 2000; _lock_wake_until = millis() + 2000;
} else { } else {
if (_display && !_display->isOn()) _display->turnOn();
uint32_t aoff = autoOffMillis(); uint32_t aoff = autoOffMillis();
if (aoff > 0) _auto_off = millis() + aoff; if (aoff > 0) _auto_off = millis() + aoff;
} }
@@ -3159,8 +3158,10 @@ static uint16_t readAnalogMv(uint32_t psel) {
} }
#endif #endif
// Cycle a user GPIO pin's mode (Off/In/Out-low/Out-high), apply it to the // Set a user GPIO pin to a specific mode (0=Off 1=In 2=Out-low 3=Out-high
// actual pin, and persist. Called by GpioScreen on Enter. // 4=Analog), apply it to the actual pin, and persist. The Off->In->Out->...
// cycling itself lives in GpioScreen; the bot's !gpioN on/off and boot
// restore also route through here.
void UITask::setGpioMode(int idx, uint8_t mode) { void UITask::setGpioMode(int idx, uint8_t mode) {
#if defined(PIN_GPIO1) #if defined(PIN_GPIO1)
if (!_node_prefs) return; if (!_node_prefs) return;