feat(advert): stav baterie jako ikonka na konci advertovaného jména

Napětí uzlu je v telemetrii, jenže tu přečte jen ten, kdo se umí přihlásit.
Ikonka v advertu doletí každému, kdo uzel slyší, za pár bajtů app_data.
Vypnuté defaultem (`set batt.icon on`), práh `set batt.icon.mv` (3700 mV).

Rozpočet je těsný a plný pastí, proto `BatteryIcon.h` s testy:

  * app_data má 32 B, z toho 1 B flagy a 8 B lat/lon -> na jméno zbývá 23 B;
  * ☀️ je ŠEST bajtů (U+2600 + neviditelný variation selector), 🪫 čtyři;
  * budgetuje se vždy ta DELŠÍ z ikonek. Kdyby se rozhodovalo podle aktuální,
    jméno by při každém přechodu změnilo délku a klienti by přepsali kontakt;
  * když se ikonka nevejde, zahodí se CELÁ a `set`/`get` to řeknou. Tiché
    uříznutí UTF-8 clampem je zvlášť ošklivé: jméno vypadá v pořádku.

Přepínání je hysterezní (100 mV), aby ikonka nesledovala denní cyklus
solárního uzlu: změřený denní rozkmit je 38-46 mV a drift 13 mV/den, takže
dvoustavová ikonka překlopí zhruba jednou za měsíc. 25h flood advert je proti
tomu jemný, ne hrubý.

Vzorkuje se jedním čtením záměrně. Deska bez článku na USB čte bimodálně
(4160/3760, dvě úrovně 390 mV od sebe) a medián z pěti vzorků to NEŘEŠÍ
(420 -> 380 mV) -- není co odfiltrovat, je to plovoucí VBAT proti nabíječce.
Uzel s článkem drží rozptyl 0-22 mV v 10min oknech, což hystereze pokrývá
několikanásobně; podrobnosti v BatteryIcon.h, ať to nikdo nezkouší znovu.

Ověřeno na lavici svědkem (x1 čte advert z x4, ne x4 sám o sobě): celý cyklus
☀️ -> 🪫 -> ☀️ -> bez ikonky, fleet_test 13/14 shodně s baseline.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5 committed 2026-09-21 19:13:38 +02:00
1 parent 77e3accd1f
commit 78b3c8cce8
7 files changed
+331 -3

No files matched your search

+3
View File
@@ -2,6 +2,9 @@
#include <cstdint>
#include <cmath>
// Real Arduino.h pulls stdio in; code under test (AdvertTimeHelper) relies on
// sprintf being available in the global namespace from this header alone.
#include <stdio.h>
#include "Stream.h"
inline uint32_t g_mock_millis = 0;
@@ -0,0 +1,110 @@
#include <gtest/gtest.h>
#include <helpers/BatteryIcon.h>
#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
}
TEST(BatteryIcon, BudgetMatchesAdvertLayout) {
EXPECT_EQ(23u, mesh::advertNameBudget(true)); // 32 - 1 flags - 8 lat/lon
EXPECT_EQ(31u, mesh::advertNameBudget(false));
}
// The real names of the three nodes this was written for.
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));
}
TEST(BatteryIcon, DroppingPositionBuysEightBytes) {
EXPECT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), true));
EXPECT_TRUE(mesh::battIconFits(strlen("tth-mast-repeater-one"), false));
}
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);
}
}
// 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);
ASSERT_EQ(23u, strlen(name));
uint8_t app_data[MAX_ADVERT_DATA_SIZE];
AdvertDataBuilder builder(ADV_TYPE_REPEATER, name, 50.38, 12.82);
uint8_t len = builder.encodeTo(app_data);
AdvertDataParser parser(app_data, len);
ASSERT_TRUE(parser.isValid());
ASSERT_TRUE(parser.hasName());
EXPECT_STREQ(name, parser.getName());
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);
ASSERT_EQ(27u, strlen(name));
ASSERT_FALSE(mesh::battIconFits(strlen("tth-mast-repeater-one"), true));
uint8_t app_data[MAX_ADVERT_DATA_SIZE];
AdvertDataBuilder builder(ADV_TYPE_REPEATER, name, 50.38, 12.82);
uint8_t len = builder.encodeTo(app_data);
AdvertDataParser parser(app_data, len);
ASSERT_TRUE(parser.isValid());
EXPECT_STREQ("tth-mast-repeater-one", parser.getName()); // icon 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));
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}