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

Dvoustavová ikonka nesla málo informace a ☀️ v ní znamenalo "baterka OK", ne
"jsem solár", jak bylo zamýšleno. Teď je ☀ trvalá značka solárního uzlu
a za ní jedna z osmi úrovní ▁▂▃▄▅▆▇█.

Proč ne emoji: Unicode má přesně DVĚ baterková emoji (U+1F50B, U+1FAAB),
žádné "napůl", a po 4 B se dvojice ☀️+🪫 nevejde (7 B suffixu proti 6 B, co
jména dovolují). Blokové znaky jsou v BMP, tedy 3 B, a je jich osm -- nejvíc
stavů za nejmíň bajtů. Suffix zůstal 6 B, takže se nic nepřejmenovává.

⚡ místo úrovně, když uzel běží na externím napájení. Bere se z VBUS detektu
(`MainBoard::isExternalPowered()`, nRF52 to má v hardwaru), ne z heuristiky
nad šumem. Řeší to lavici, kde deska bez článku čte plovoucí VBAT a jakákoli
úroveň z toho by byla vymyšlená -- a zároveň to na stožáru pozná nabíjení.

Hystereze je teď na sedmi hranicích a je asymetrická: úroveň padá hned pod
hranicí, stoupá až hyst_mv nad ní. Klesající baterka je ten směr, kde je
optimistická ikonka horší než pesimistická. 50 mV pokryje naměřený denní
rozkmit 38-46 mV a vejde se do kroku 112 mV.

Prefs `bicon_mv` (práh) nahrazeno `bicon_lo`/`bicon_hi` (konce škály),
CLI `set batt.icon.lo|.hi`. Default 3300..4200 = táž škála, kterou počítají
dashboardy, takže ikonka a Grafana říkají totéž.

Ověřeno na lavici svědkem (x4 čte advert z x1): `bench-board☀⚡` opakovaně.
Úrovně zatím jen unit testy -- deska na USB vždycky hlásí externí napájení,
takže ▁..█ se na lavici ukázat nedá; to ověří až uzel na článku.

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 78b3c8cce8
commit c2fb7d1610
5 files changed
+312 -152

No files matched your search

+108 -57
View File
@@ -4,12 +4,14 @@
#include <helpers/AdvertDataHelpers.h>
#include <string.h>
// The icons are not one byte each, and the sun carries a variation selector
// that is invisible in an editor but costs three bytes. Pin both down: the
// whole budget math rests on these numbers.
TEST(BatteryIcon, IconsHaveTheExpectedByteLength) {
EXPECT_EQ(6u, strlen(BATT_ICON_OK)); // U+2600 + U+FE0F
EXPECT_EQ(4u, strlen(BATT_ICON_LOW)); // U+1FAAB
// The whole budget rests on these being 3 bytes, not 4. Block elements and
// U+2600/U+26A1 are BMP; battery emoji are not, which is why they lost.
TEST(BatteryIcon, GlyphsAreThreeBytesAndThereAreEightLevels) {
EXPECT_EQ(3u, strlen(BATT_ICON_MARKER)); // U+2600 sun, no variation selector
EXPECT_EQ(3u, strlen(BATT_ICON_EXTERNAL)); // U+26A1 high voltage
EXPECT_EQ(24u, strlen(BATT_ICON_LEVELS)); // 8 x 3 B
EXPECT_EQ(8, mesh::battIconLevels());
EXPECT_EQ(6u, mesh::battIconWidth()); // marker + one glyph
}
TEST(BatteryIcon, BudgetMatchesAdvertLayout) {
@@ -17,57 +19,103 @@ TEST(BatteryIcon, BudgetMatchesAdvertLayout) {
EXPECT_EQ(31u, mesh::advertNameBudget(false));
}
// The real names of the three nodes this was written for.
// The three deployed names, at 6 bytes of indicator.
TEST(BatteryIcon, FitsTheDeployedNames) {
EXPECT_TRUE(mesh::battIconFits(strlen("mast-rpt-two"), true)); // 12 + 6 = 18
EXPECT_TRUE(mesh::battIconFits(strlen("mast-repeater-one"), true)); // 17 + 6 = 23, exactly
EXPECT_TRUE(mesh::battIconFits(strlen("mast-rpt-three.cz"), true)); // 17 + 6 = 23, exactly
EXPECT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), true)); // 21 + 6 = 27
}
// A name is only accepted when the LONGER icon fits. Budget for the current
// state instead and the name would change length as the battery crosses the
// threshold, rewriting the contact in every client that hears it.
TEST(BatteryIcon, BudgetsForTheLongerIconNotTheCurrentOne) {
// 18 bytes: the 4-byte low icon would fit (22), the 6-byte one would not (24).
EXPECT_FALSE(mesh::battIconFits(18, true));
EXPECT_TRUE(mesh::battIconFits(17, true));
EXPECT_TRUE(mesh::battIconFits(strlen("mast-rpt-two"), true)); // 12 + 6 = 18
EXPECT_TRUE(mesh::battIconFits(strlen("mast-repeater-one"), true)); // 17 + 6 = 23, exactly
EXPECT_TRUE(mesh::battIconFits(strlen("mast-rpt-three.cz"), true)); // 17 + 6 = 23, exactly
EXPECT_FALSE(mesh::battIconFits(18, true)); // one byte over
}
TEST(BatteryIcon, DroppingPositionBuysEightBytes) {
EXPECT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), true));
EXPECT_TRUE(mesh::battIconFits(strlen("tth-mast-repeater-one"), false));
EXPECT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), true)); // 21 + 6 = 27
EXPECT_TRUE(mesh::battIconFits(strlen("tth-mast-repeater-one"), false)); // 31 B budget
}
TEST(BatteryIcon, HysteresisNeedsARealRecoveryNotJustTheThreshold) {
const uint16_t low = 3700, hyst = 100;
EXPECT_FALSE(mesh::battIconIsLow(false, 3701, low, hyst)); // above, stays ok
EXPECT_TRUE(mesh::battIconIsLow(false, 3699, low, hyst)); // crosses down
EXPECT_TRUE(mesh::battIconIsLow(true, 3750, low, hyst)); // back above, but inside hysteresis
EXPECT_TRUE(mesh::battIconIsLow(true, 3800, low, hyst)); // still inside, boundary
EXPECT_FALSE(mesh::battIconIsLow(true, 3801, low, hyst)); // genuinely recovered
}
// The measured daily swing of a solar node under weak sun was 38-46 mV. The
// icon must not follow sunrise and sunset.
TEST(BatteryIcon, SurvivesTheMeasuredDailySwing) {
bool low = mesh::battIconIsLow(false, 3690, 3700, BATT_ICON_HYST_MV);
ASSERT_TRUE(low);
// night down, day up, +-23 mV around 3690 -- must stay low the whole time
for (int i = 0; i < 10; i++) {
low = mesh::battIconIsLow(low, 3667, 3700, BATT_ICON_HYST_MV);
EXPECT_TRUE(low);
low = mesh::battIconIsLow(low, 3713, 3700, BATT_ICON_HYST_MV);
EXPECT_TRUE(low);
// Every level must be the same width, or the name length would change with
// the charge and clients would rewrite the contact on each step.
TEST(BatteryIcon, EveryGlyphIsTheSameWidth) {
char g[8];
for (uint8_t i = 0; i < mesh::battIconLevels(); i++) {
mesh::battIconGlyph(g, i);
EXPECT_EQ(3u, strlen(g)) << "level " << (int)i;
}
}
TEST(BatteryIcon, GlyphIndexIsClampedNotWrapped) {
char lo[8], hi[8], over[8];
mesh::battIconGlyph(lo, 0);
mesh::battIconGlyph(hi, 7);
mesh::battIconGlyph(over, 99);
EXPECT_STREQ("▁", lo);
EXPECT_STREQ("█", hi);
EXPECT_STREQ(hi, over); // clamped to the top, not wrapped to 0
}
TEST(BatteryIcon, LevelSpansTheScale) {
const uint8_t n = 8;
// No hysteresis here: check the raw mapping across the 3300..4200 scale.
EXPECT_EQ(0, mesh::battIconLevel(0, 3300, 3300, 4200, n, 0));
EXPECT_EQ(0, mesh::battIconLevel(0, 3200, 3300, 4200, n, 0)); // below scale
EXPECT_EQ(7, mesh::battIconLevel(0, 4200, 3300, 4200, n, 0));
EXPECT_EQ(7, mesh::battIconLevel(0, 4500, 3300, 4200, n, 0)); // above scale
EXPECT_EQ(4, mesh::battIconLevel(0, 3750, 3300, 4200, n, 0)); // exactly on a boundary
EXPECT_EQ(3, mesh::battIconLevel(0, 3749, 3300, 4200, n, 0)); // one mV below it
}
// From a cold start the climb loop has to reach the right level on its own.
TEST(BatteryIcon, ColdStartClimbsToTheRightLevel) {
const uint8_t n = 8;
// mast-repeater-one as measured: 3794 mV on the 3300..4200 scale.
uint8_t lvl = mesh::battIconLevel(0, 3794, 3300, 4200, n, BATT_ICON_HYST_MV);
EXPECT_EQ(3, lvl); // step is 112 mV; 3794 sits in the 4th band
}
// Falls immediately, climbs only once clear of the boundary.
TEST(BatteryIcon, HysteresisIsAsymmetric) {
// POZOR na typy: `const uint8_t n = 8, lo = 3300, ...` by dalo uint8_t VŠEM
// a 3300/4200 by přeteklo na 228/104 -- funkce pak vrací 0 a test projde
// pro nic. Stalo se to tady 21. 9. 2026.
const uint8_t n = 8;
const uint16_t lo = 3300, hi = 4200, hyst = 50;
// Boundary into level 4 is 3300 + 4*900/8 = 3750.
EXPECT_EQ(3, mesh::battIconLevel(3, 3749, lo, hi, n, hyst)); // just under
EXPECT_EQ(3, mesh::battIconLevel(3, 3780, lo, hi, n, hyst)); // over, inside hysteresis
EXPECT_EQ(4, mesh::battIconLevel(3, 3800, lo, hi, n, hyst)); // clear of it -> climbs
EXPECT_EQ(4, mesh::battIconLevel(4, 3750, lo, hi, n, hyst)); // at boundary, holds
EXPECT_EQ(3, mesh::battIconLevel(4, 3749, lo, hi, n, hyst)); // below -> drops at once
}
// The measured daily swing of a solar node under weak sun was 38-46 mV. The
// level must not follow sunrise and sunset.
TEST(BatteryIcon, SurvivesTheMeasuredDailySwing) {
const uint8_t n = 8;
const uint16_t lo = 3300, hi = 4200;
// Park just under a boundary (3750) and swing +-23 mV around it all week.
uint8_t lvl = mesh::battIconLevel(0, 3740, lo, hi, n, BATT_ICON_HYST_MV);
const uint8_t settled = lvl;
for (int day = 0; day < 7; day++) {
lvl = mesh::battIconLevel(lvl, 3717, lo, hi, n, BATT_ICON_HYST_MV);
EXPECT_EQ(settled, lvl) << "night, day " << day;
lvl = mesh::battIconLevel(lvl, 3763, lo, hi, n, BATT_ICON_HYST_MV);
EXPECT_EQ(settled, lvl) << "day, day " << day;
}
}
TEST(BatteryIcon, DegenerateScalesDoNotDivideByZero) {
EXPECT_EQ(0, mesh::battIconLevel(3, 3800, 4200, 3300, 8, 50)); // hi <= lo
EXPECT_EQ(0, mesh::battIconLevel(3, 3800, 3300, 3300, 8, 50)); // empty span
EXPECT_EQ(0, mesh::battIconLevel(3, 3800, 3300, 4200, 1, 50)); // single level
EXPECT_EQ(7, mesh::battIconLevel(200, 4200, 3300, 4200, 8, 50)); // prev out of range
}
// End to end against the real encoder: a name sized to the budget keeps the
// icon intact, and the parser on the far side reads it back byte for byte.
TEST(BatteryIcon, EncoderKeepsTheIconOnAFittingName) {
char name[32];
snprintf(name, sizeof(name), "mast-repeater-one%s", BATT_ICON_OK);
// indicator intact, and the parser on the far side reads it back byte for byte.
TEST(BatteryIcon, EncoderKeepsTheIndicatorOnAFittingName) {
char glyph[8];
mesh::battIconGlyph(glyph, 3);
char name[40];
snprintf(name, sizeof(name), "mast-repeater-one%s%s", BATT_ICON_MARKER, glyph);
ASSERT_EQ(23u, strlen(name));
uint8_t app_data[MAX_ADVERT_DATA_SIZE];
@@ -81,11 +129,13 @@ TEST(BatteryIcon, EncoderKeepsTheIconOnAFittingName) {
EXPECT_TRUE(parser.hasLatLon());
}
// And the failure mode we refuse to ship: one byte over and the clamp eats
// the icon whole, leaving a name that looks fine and silently lost its state.
TEST(BatteryIcon, EncoderSilentlyDropsAnIconThatDoesNotFit) {
char name[40];
snprintf(name, sizeof(name), "tth-mast-repeater-one%s", BATT_ICON_OK);
// The failure mode we refuse to ship: one byte over and the clamp eats the
// indicator, leaving a name that looks fine and silently lost its state.
TEST(BatteryIcon, EncoderSilentlyDropsAnIndicatorThatDoesNotFit) {
char glyph[8];
mesh::battIconGlyph(glyph, 3);
char name[48];
snprintf(name, sizeof(name), "tth-mast-repeater-one%s%s", BATT_ICON_MARKER, glyph);
ASSERT_EQ(27u, strlen(name));
ASSERT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), true));
@@ -95,13 +145,14 @@ TEST(BatteryIcon, EncoderSilentlyDropsAnIconThatDoesNotFit) {
AdvertDataParser parser(app_data, len);
ASSERT_TRUE(parser.isValid());
EXPECT_STREQ("tth-mast-repeater-one", parser.getName()); // icon gone, no error
EXPECT_STREQ("tth-mast-repeater-one", parser.getName()); // indicator gone, no error
}
// Whatever the icons are changed to, they must survive `set name`'s filter.
TEST(BatteryIcon, IconsPassTheNameCharacterFilter) {
EXPECT_TRUE(AdvertDataParser::isValidName(BATT_ICON_OK));
EXPECT_TRUE(AdvertDataParser::isValidName(BATT_ICON_LOW));
// Whatever the glyphs are changed to, they must survive `set name`'s filter.
TEST(BatteryIcon, GlyphsPassTheNameCharacterFilter) {
EXPECT_TRUE(AdvertDataParser::isValidName(BATT_ICON_MARKER));
EXPECT_TRUE(AdvertDataParser::isValidName(BATT_ICON_EXTERNAL));
EXPECT_TRUE(AdvertDataParser::isValidName(BATT_ICON_LEVELS));
}
int main(int argc, char** argv) {