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

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 b0a2776be1
commit 793c309a4b
7 files changed
+331 -3

No files matched your search

+88
View File
@@ -0,0 +1,88 @@
#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 icons are not one byte each:
//
// U+2600 U+FE0F (sun, emoji presentation) 6 bytes -- the variation
// selector is half of it
// U+1FAAB (low battery) 4 bytes
//
// Sampling rate is a non-issue: a solar node drifts by about 13 mV/day
// (measured on mast-repeater-one over 30 days), so on a 3300-4200 mV scale a
// two-state icon flips roughly once a month. The flood advert interval of 25 h
// is far finer than the thing being measured.
#include <MeshCore.h>
#include <stddef.h>
#include <stdint.h>
#ifndef BATT_ICON_OK
#define BATT_ICON_OK "\u2600\uFE0F" // sun, 6 bytes
#endif
#ifndef BATT_ICON_LOW
#define BATT_ICON_LOW "\U0001FAAB" // low battery, 4 bytes
#endif
// Threshold and hysteresis in millivolts, for a 1S Li-Ion/LiPo cell. 3700 mV
// is roughly 44 % on the curve the dashboards use, which on the measured slope
// leaves about a month of warning before the node browns out.
#ifndef BATT_ICON_LOW_MV
#define BATT_ICON_LOW_MV 3700
#endif
// The daily swing of a solar node under weak sun was 38-46 mV (measured), so
// anything near that would make the icon flip with sunrise and sunset.
#ifndef BATT_ICON_HYST_MV
#define BATT_ICON_HYST_MV 100
#endif
namespace mesh {
// Bytes of app_data left for the name. Mirrors AdvertDataBuilder::encodeTo():
// one byte of flags, plus 4+4 for lat/lon when a position is advertised.
inline size_t advertNameBudget(bool with_loc) {
return MAX_ADVERT_DATA_SIZE - 1 - (with_loc ? 8 : 0);
}
// Does the name still fit with an icon appended?
//
// This deliberately budgets for the LONGER of the two icons, never the one
// currently selected. Decide per state and a name that fits the 4-byte icon
// but not the 6-byte one would get silently truncated by the UTF-8 clamp in
// AdvertDataBuilder every time the battery crossed the threshold -- the name
// itself would change length, and every client would rewrite the contact.
inline bool battIconFits(size_t name_len, bool with_loc) {
const size_t ok_len = sizeof(BATT_ICON_OK) - 1;
const size_t low_len = sizeof(BATT_ICON_LOW) - 1;
const size_t worst = ok_len > low_len ? ok_len : low_len;
return name_len + worst <= advertNameBudget(with_loc);
}
// Hysteresis: drop to "low" at the threshold, recover only hyst_mv above it.
inline bool battIconIsLow(bool prev_low, uint16_t mv, uint16_t low_mv, uint16_t hyst_mv) {
if (prev_low) return mv <= (uint32_t)low_mv + hyst_mv;
return mv < low_mv;
}
// On sampling: one analogRead per decision is deliberate.
//
// 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), which the 100 mV hysteresis covers several times over.
// So the noise is a property of the bench, not of the firmware, and averaging
// it away here would only add code that the measurement says does nothing.
}