#pragma once // CUSTOM (TeTeHacko): battery state as an icon appended to the advertised // node name. // // Why at all: the node already reports its battery in telemetry, but only // someone who can log into it gets to read that. An icon in the advert reaches // everyone who hears the node, and costs a handful of bytes of app_data. // // The budget is tight. app_data is MAX_ADVERT_DATA_SIZE (32) bytes, of which // the flags take 1 and lat/lon another 8, leaving 23 bytes for the name on a // node that advertises its position. The whole indicator has to fit in what // the name leaves over -- for the deployed names that is 6 bytes. // // Which rules out emoji for the level. Unicode has exactly TWO battery emoji, // U+1F50B and U+1FAAB; there is no "half full" one, and at 4 bytes each a // sun-plus-battery pair costs 7 bytes, one too many. Block elements live in // the BMP, so they are 3 bytes rather than 4, and come in eight steps -- the // most states for the fewest bytes: // // marker U+2600 3 B sun, permanent -- "this is a solar node". // Deliberately WITHOUT the U+FE0F variation // selector, which costs 3 more bytes and buys // only a colour glyph. // level U+2581..88 3 B eight steps of charge // // Sampling rate is a non-issue: a solar node drifts by about 13 mV/day // (measured on mast-repeater-one over 30 days), so even at eight steps the // name changes roughly once a week. The flood advert interval of 25 h is far // finer than the thing being measured. #include #include #include #include // Permanent marker, prepended to the level. Empty string disables it. #ifndef BATT_ICON_MARKER #define BATT_ICON_MARKER "☀" // sun, 3 bytes #endif // Level glyphs, low to high. All must be the same byte length. #ifndef BATT_ICON_LEVELS #define BATT_ICON_LEVELS "▁▂▃▄▅▆▇█" #endif #ifndef BATT_ICON_LEVEL_BYTES #define BATT_ICON_LEVEL_BYTES 3 #endif // Shown INSTEAD of the level while the node runs on external power, which // nRF52 reports straight from VBUS detect (MainBoard::isExternalPowered()). // That covers the bench, where a board with no cell reads a meaningless // floating VBAT -- better to say "on USB" than to draw a charge level out of // noise. It is 3 bytes like a level glyph, so the budget does not move. #ifndef BATT_ICON_EXTERNAL #define BATT_ICON_EXTERNAL "\u26A1" // high voltage, 3 bytes #endif // Scale ends in millivolts for a 1S Li-Ion/LiPo cell -- the same 3300..4200 // the dashboards use, so the icon and the Grafana percentage tell one story. #ifndef BATT_ICON_LO_MV #define BATT_ICON_LO_MV 3300 #endif #ifndef BATT_ICON_HI_MV #define BATT_ICON_HI_MV 4200 #endif // Hysteresis per boundary. Has to clear the measured daily swing of a solar // node under weak sun (38-46 mV) or the level would follow sunrise and // sunset, and stay well under one step (112 mV at eight levels) or the level // could never climb at all. #ifndef BATT_ICON_HYST_MV #define BATT_ICON_HYST_MV 50 #endif namespace mesh { // Number of level glyphs. inline uint8_t battIconLevels() { return (uint8_t)((sizeof(BATT_ICON_LEVELS) - 1) / BATT_ICON_LEVEL_BYTES); } // Bytes the indicator adds to the name: marker plus one level glyph. // // Every level is the same width, and that is the point rather than a detail: // a name whose length changed with the charge would be re-truncated by the // UTF-8 clamp at some steps and not others, and every client that hears it // would rewrite the contact. inline size_t battIconWidth() { const size_t lvl = BATT_ICON_LEVEL_BYTES; const size_t ext = sizeof(BATT_ICON_EXTERNAL) - 1; return (sizeof(BATT_ICON_MARKER) - 1) + (lvl > ext ? lvl : ext); } // Does the name still fit with the indicator appended? // // `budget` musí přijít z AdvertDataBuilder::nameBudget(), ne z druhého výpočtu // tady: rozpočet závisí i na FEAT1/FEAT2, a kopie, která o nich nevěděla, by // tvrdila o 2-4 bajty víc, než je pravda. Pak by UTF-8 clamp uřízl glyf // vpůli -- přesně to tiché selhání, které tenhle soubor jinde odmítá. inline bool battIconFits(size_t name_len, size_t budget) { return name_len + battIconWidth() <= budget; } // Copy the glyph for `level` into dest, which must hold // BATT_ICON_LEVEL_BYTES + 1 bytes. inline void battIconGlyph(char dest[], uint8_t level) { uint8_t n = battIconLevels(); if (n == 0) { dest[0] = 0; return; } if (level >= n) level = n - 1; const char* tbl = BATT_ICON_LEVELS; memcpy(dest, tbl + (size_t)level * BATT_ICON_LEVEL_BYTES, BATT_ICON_LEVEL_BYTES); dest[BATT_ICON_LEVEL_BYTES] = 0; } // Charge level 0..n-1 for `mv`, with hysteresis against the previous level. // // Asymmetric on purpose: the level drops as soon as the voltage falls below a // boundary, but climbs only once it is hyst_mv clear of one. Falling is the // direction that matters -- an optimistic icon on a node that is browning out // is worse than a pessimistic one on a node that is charging. // // Starting from 0 the climb loop walks up to wherever the voltage actually // is, so a fresh boot settles on the right level (one hysteresis band low, // the safe side) without needing a separate "unknown" state. inline uint8_t battIconLevel(uint8_t prev, uint16_t mv, uint16_t lo_mv, uint16_t hi_mv, uint8_t n, uint16_t hyst_mv) { if (n <= 1 || hi_mv <= lo_mv) return 0; if (prev >= n) prev = n - 1; const uint32_t span = (uint32_t)hi_mv - lo_mv; uint8_t lvl = prev; // The boundary into level k sits at lo + k*span/n, for k = 1..n-1. while (lvl + 1 < n) { uint32_t edge = lo_mv + (uint32_t)(lvl + 1) * span / n; if ((uint32_t)mv >= edge + hyst_mv) lvl++; else break; } while (lvl > 0) { uint32_t edge = lo_mv + (uint32_t)lvl * span / n; if ((uint32_t)mv < edge) lvl--; else break; } return lvl; } // On sampling: one analogRead per decision is deliberate, and it is the same // call that feeds the battery metric (simple_repeater/MyMesh.cpp:482 for // stats, :510 for telemetry). There is no smoothed copy of the voltage // anywhere in the firmware to prefer instead -- what looks steady in Grafana // is steady because the cell is. // // A cell-less XIAO on USB reads bimodally -- measured over 20 reads on x4: // 4160 3760 4160 3760 4140 4150 3750 ... two levels ~390 mV apart, alternating // roughly 50/50. A median over five samples was tried and does NOT help // (spread 420 mV raw vs 380 mV median): it is not an outlier to reject, it is // VBAT floating against the charger with nothing to clamp it. // // It is also not what a deployed node sees. mast-repeater-one, with a real // cell, holds a max-min spread of 0-22 mV over 10-minute windows (queried // from Prometheus). Against a 112 mV step and 50 mV of hysteresis that is // quiet, so the noise is a property of the bench, not of the firmware. // A bench board without a cell WILL step around; that is expected. }