feat(advert): osm úrovní baterie místo dvou stavů, blesk na externím napájení

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5 committed 2026-09-21 20:26:51 +02:00
1 parent 5f56a80a78
commit f2b952ec4a
5 files changed
+312 -152

No files matched your search

+60 -34
View File
@@ -175,25 +175,34 @@ void CommonCLI::savePrefs() {
_prefs->clearDirty();
}
// Name as it goes on the air: the configured name, with a battery icon
// appended when 'batt.icon' is on and there is room for it. Returns the bare
// name in every other case -- an icon that does not fit is dropped whole
// rather than letting the UTF-8 clamp eat it silently.
// Name as it goes on the air: the configured name, plus the solar marker and
// either a charge level or the external-power glyph. Returns the bare name in
// every other case -- an indicator that does not fit is dropped whole rather
// than letting the UTF-8 clamp eat half of it silently.
const char* CommonCLI::advertName(bool with_loc) {
if (!_prefs->batt_icon) return _prefs->node_name;
uint16_t mv = _board->getBattMilliVolts();
if (mv == 0) return _prefs->node_name; // board has no battery sense, or hasn't measured yet
size_t nlen = strlen(_prefs->node_name);
if (!mesh::battIconFits(nlen, with_loc)) return _prefs->node_name;
uint16_t low_mv = _prefs->batt_icon_mv ? _prefs->batt_icon_mv : BATT_ICON_LOW_MV;
_batt_low = mesh::battIconIsLow(_batt_low, mv, low_mv, BATT_ICON_HYST_MV);
char glyph[8];
if (_board->isExternalPowered()) {
// On USB the cell (if any) is being charged and VBAT reads the charger,
// not the battery -- a level drawn from it would be fiction.
StrHelper::strncpy(glyph, BATT_ICON_EXTERNAL, sizeof(glyph));
} else {
uint16_t mv = _board->getBattMilliVolts();
if (mv == 0) return _prefs->node_name; // board has no battery sense
uint16_t lo = _prefs->batt_icon_lo ? _prefs->batt_icon_lo : BATT_ICON_LO_MV;
uint16_t hi = _prefs->batt_icon_hi ? _prefs->batt_icon_hi : BATT_ICON_HI_MV;
_batt_level = mesh::battIconLevel(_batt_level, mv, lo, hi,
mesh::battIconLevels(), BATT_ICON_HYST_MV);
mesh::battIconGlyph(glyph, _batt_level);
}
StrHelper::strncpy(_adv_name, _prefs->node_name, sizeof(_adv_name));
strncat(_adv_name, _batt_low ? BATT_ICON_LOW : BATT_ICON_OK,
sizeof(_adv_name) - strlen(_adv_name) - 1);
strncat(_adv_name, BATT_ICON_MARKER, sizeof(_adv_name) - strlen(_adv_name) - 1);
strncat(_adv_name, glyph, sizeof(_adv_name) - strlen(_adv_name) - 1);
return _adv_name;
}
@@ -803,14 +812,23 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
savePrefs();
strcpy(reply, "OK");
#endif
} else if (memcmp(config, "batt.icon.mv ", 13) == 0) {
} else if (memcmp(config, "batt.icon.lo ", 13) == 0 || memcmp(config, "batt.icon.hi ", 13) == 0) {
bool is_lo = config[10] == 'l';
int mv = atoi(&config[13]);
if (mv != 0 && (mv < 3000 || mv > 4200)) {
strcpy(reply, "Error: expected 3000..4200, or 0 for default");
if (mv != 0 && (mv < 2500 || mv > 4500)) {
strcpy(reply, "Error: expected 2500..4500, or 0 for default");
} else {
_prefs->batt_icon_mv = mv;
if (is_lo) _prefs->batt_icon_lo = mv; else _prefs->batt_icon_hi = mv;
savePrefs();
sprintf(reply, "OK - low below %d mV", mv ? mv : BATT_ICON_LOW_MV);
uint16_t lo = _prefs->batt_icon_lo ? _prefs->batt_icon_lo : BATT_ICON_LO_MV;
uint16_t hi = _prefs->batt_icon_hi ? _prefs->batt_icon_hi : BATT_ICON_HI_MV;
if (hi <= lo) {
strcpy(reply, "OK - WARNING: hi <= lo, level will stay at 0");
} else {
sprintf(reply, "OK - scale %d..%d mV, %d levels, step %d mV",
(uint32_t)lo, (uint32_t)hi, (uint32_t)mesh::battIconLevels(),
(uint32_t)((hi - lo) / mesh::battIconLevels()));
}
}
} else if (memcmp(config, "batt.icon ", 10) == 0) {
_prefs->batt_icon = memcmp(&config[10], "on", 2) == 0 ? 1 : 0;
@@ -824,7 +842,7 @@ void CommonCLI::handleSetCmd(uint32_t sender_timestamp, char* command, char* rep
sprintf(reply, "on - BUT name is %d B, max %d B with icon - icon omitted",
(int)strlen(_prefs->node_name),
(int)(mesh::advertNameBudget(_prefs->advert_loc_policy != ADVERT_LOC_NONE)
- (sizeof(BATT_ICON_OK) - 1)));
- mesh::battIconWidth()));
} else {
strcpy(reply, "on - shows on next advert");
}
@@ -884,29 +902,37 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep
int len = _callbacks->getSelfId().writeTo(prv_key, PRV_KEY_SIZE);
mesh::Utils::toHex(tmp, prv_key, len);
sprintf(reply, "> %s", tmp);
} else if (memcmp(config, "batt.icon.mv", 12) == 0) {
sprintf(reply, "> %d", (uint32_t)(_prefs->batt_icon_mv ? _prefs->batt_icon_mv : BATT_ICON_LOW_MV));
} else if (memcmp(config, "batt.icon.lo", 12) == 0) {
sprintf(reply, "> %d", (uint32_t)(_prefs->batt_icon_lo ? _prefs->batt_icon_lo : BATT_ICON_LO_MV));
} else if (memcmp(config, "batt.icon.hi", 12) == 0) {
sprintf(reply, "> %d", (uint32_t)(_prefs->batt_icon_hi ? _prefs->batt_icon_hi : BATT_ICON_HI_MV));
} else if (memcmp(config, "batt.icon", 9) == 0) {
// Report what actually goes on the air, not just the flag: the icon is
// dropped when the name leaves no room, and the measured voltage is the
// only way to tell "threshold not reached" from "board reads nothing".
// Report what actually goes on the air, not just the flag: the indicator
// is dropped when the name leaves no room, and the voltage is the only way
// to tell "not there yet" from "board reads nothing".
bool with_loc = _prefs->advert_loc_policy != ADVERT_LOC_NONE;
uint16_t mv = _board->getBattMilliVolts();
uint16_t low_mv = _prefs->batt_icon_mv ? _prefs->batt_icon_mv : BATT_ICON_LOW_MV;
if (!_prefs->batt_icon) {
sprintf(reply, "> off (%d mV)", (uint32_t)mv);
strcpy(reply, "> off");
} else if (!mesh::battIconFits(strlen(_prefs->node_name), with_loc)) {
strcpy(reply, "> on (omitted, name too long)");
} else if (mv == 0) {
strcpy(reply, "> on (omitted, no battery reading)");
} else if (_board->isExternalPowered()) {
strcpy(reply, "> on (external power)");
} else {
// What the NEXT advert would carry, not what the last one did. Reporting
// the stored flag had `get` saying "ok" moments before an advert went out
// with the empty icon, because the state only advances in advertName().
// Predicting is read-only: _batt_low is deliberately not updated here.
bool next_low = mesh::battIconIsLow(_batt_low, mv, low_mv, BATT_ICON_HYST_MV);
sprintf(reply, "> on (%s, %d mV, low below %d)",
next_low ? "low" : "ok", (uint32_t)mv, (uint32_t)low_mv);
uint16_t mv = _board->getBattMilliVolts();
if (mv == 0) {
strcpy(reply, "> on (omitted, no battery reading)");
} else {
// What the NEXT advert would carry, not what the last one did: the
// state only advances in advertName(), so reporting the stored level
// had `get` disagreeing with the advert that went out moments later.
// Predicting is read-only -- _batt_level is deliberately not updated.
uint16_t lo = _prefs->batt_icon_lo ? _prefs->batt_icon_lo : BATT_ICON_LO_MV;
uint16_t hi = _prefs->batt_icon_hi ? _prefs->batt_icon_hi : BATT_ICON_HI_MV;
uint8_t n = mesh::battIconLevels();
uint8_t next = mesh::battIconLevel(_batt_level, mv, lo, hi, n, BATT_ICON_HYST_MV);
sprintf(reply, "> on (level %d/%d, %d mV, scale %d..%d)",
(uint32_t)(next + 1), (uint32_t)n, (uint32_t)mv, (uint32_t)lo, (uint32_t)hi);
}
}
} else if (memcmp(config, "name", 4) == 0) {
sprintf(reply, "> %s", _prefs->node_name);