fix(display): harden CHSC6X polling and long-gap activity accounting

Addresses a Codex review of the preceding three commits.

Touch could disable itself for the whole session. begin() latched _present
from a single address probe, but this controller NACKs whenever it has nothing
to report, so an idle probe at boot was indistinguishable from absent
hardware. The probe is now diagnostics only; checkTap() polls regardless, and
a NACK costs one quiet bus cycle.

Touch no longer mutates shared bus state. Wrapping the read in
setTimeOut()/restore was an unsynchronised global write, and the MQTT task
drives the same Wire through AutoDiscoverRTCClock in its NTP fallback, so it
could inherit the short timeout. The address probe alone removes the
ESP_ERR_TIMEOUT stalls that motivated it, and it also ran before the timeout
was installed, so the transaction most exposed to a wedged bus was unprotected
anyway.

A single failed read could fake a release. The debounce window was 40 ms
against a 50 ms poll, so a state change was confirmed by the very next sample
and one NACK mid-touch produced a release followed by a second tap. It is now
80 ms - two consecutive consistent reads.

RadioActivityWindow froze after a gap longer than ~24.8 days. tick() treated
any delta past the signed halfway mark as an out-of-order timestamp, so a node
left unserviced that long kept a month-old packet in the 20-minute window and
reported it as recently received. A backwards step is now only believed when
it is small, which is what an out-of-order reading between two call sites
actually looks like.

Not changed: a downgrade that then saves prefs drops the display group, since
/mqtt.json is rewritten from the older serializer's known schema. That is
inherent to every appended field in this format, and bumping the JSON version
would be worse - older firmware would reject the file rather than ignore one
group. Documented instead.
This commit is contained in:
agessaman committed 2026-08-28 14:55:44 -07:00
1 parent ca17c2980c
commit 970ae395a8
5 files changed
+75 -31

No files matched your search

@@ -355,6 +355,23 @@ TEST(RadioActivityWindow, AnOlderTimestampDoesNotExpireTheWindow) {
EXPECT_EQ(10000u, s.window_ms);
}
TEST(RadioActivityWindow, AGapLongerThanHalfTheMillisRangeStillExpiresTheRing) {
// Display off and no traffic for ~25 days: the elapsed time passes the signed
// halfway mark, which must not be mistaken for an out-of-order reading, or a
// month-old packet would still be sitting in the "last 20 minutes".
RadioActivityWindow w;
w.reset(0);
recordTypical(w, 1000);
ASSERT_EQ(1u, snapshotAt(w, 2000).packets);
const uint32_t twenty_five_days = 25UL * 24 * 3600 * 1000;
ASSERT_GT(twenty_five_days, 0x80000000u) << "gap must cross the halfway mark";
RadioActivitySnapshot s = snapshotAt(w, twenty_five_days);
EXPECT_TRUE(s.isEmpty());
EXPECT_FALSE(s.has_last_packet);
}
TEST(RadioActivityWindow, AveragesHandleNegativeSnrAndMixedSigns) {
RadioActivityWindow w;
w.reset(0);
@@ -43,7 +43,7 @@ TEST(TouchTapDetector, HoldingAFingerDownDoesNotRepeat) {
uint32_t now = 1000;
d.reset(now);
EXPECT_EQ(1, hold(d, now, true, 100));
EXPECT_EQ(1, hold(d, now, true, 200));
EXPECT_EQ(0, hold(d, now, true, 10000)) << "a long press must not toggle repeatedly";
}
@@ -79,9 +79,9 @@ TEST(TouchTapDetector, SecondTapTooSoonIsSuppressed) {
uint32_t now = 1000;
d.reset(now);
EXPECT_EQ(1, hold(d, now, true, 100));
EXPECT_EQ(0, hold(d, now, false, 100));
EXPECT_EQ(0, hold(d, now, true, 100)) << "inside TOUCH_TAP_MIN_GAP_MS";
EXPECT_EQ(1, hold(d, now, true, 200));
EXPECT_EQ(0, hold(d, now, false, 150)); // release long enough to be confirmed
EXPECT_EQ(0, hold(d, now, true, 200)) << "inside TOUCH_TAP_MIN_GAP_MS";
}
TEST(TouchTapDetector, DeliberateSecondTapIsAccepted) {
@@ -89,9 +89,25 @@ TEST(TouchTapDetector, DeliberateSecondTapIsAccepted) {
uint32_t now = 1000;
d.reset(now);
EXPECT_EQ(1, hold(d, now, true, 100));
EXPECT_EQ(1, hold(d, now, true, 200));
EXPECT_EQ(0, hold(d, now, false, 600)); // past the min gap
EXPECT_EQ(1, hold(d, now, true, 100));
EXPECT_EQ(1, hold(d, now, true, 200));
}
TEST(TouchTapDetector, ASingleBadSampleDuringATouchDoesNotFakeARelease) {
// A NACK or short read reports "not pressed" for that poll. One of those must
// not confirm a release, or recovery reads as a second tap and the display
// toggles twice on one touch.
TouchTapDetector d;
uint32_t now = 1000;
d.reset(now);
EXPECT_EQ(1, hold(d, now, true, 200));
EXPECT_FALSE(d.update(now, false));
now += POLL;
EXPECT_TRUE(d.isTouched()) << "one bad sample must not confirm a release";
EXPECT_EQ(0, hold(d, now, true, 2000)) << "and must not produce a second tap";
}
TEST(TouchTapDetector, SurvivesMillisRollover) {
@@ -100,10 +116,10 @@ TEST(TouchTapDetector, SurvivesMillisRollover) {
uint32_t now = start;
d.reset(now);
EXPECT_EQ(1, hold(d, now, true, 200)); // the touch itself crosses the wrap
EXPECT_EQ(1, hold(d, now, true, 250)); // the touch itself crosses the wrap
ASSERT_LT(now, start) << "test setup must actually wrap";
EXPECT_EQ(0, hold(d, now, false, 600));
EXPECT_EQ(1, hold(d, now, true, 200));
EXPECT_EQ(1, hold(d, now, true, 250));
}
TEST(TouchTapDetector, ResetClearsPendingState) {