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 f3c81b559b
commit 018d68063f
5 files changed
+75 -31

No files matched your search

+10 -13
View File
@@ -19,16 +19,12 @@
#define CHSC6X_READ_LEN 5
// Bounds a single read. The shared bus can wedge, and the ESP32 default lets a
// failed transfer burn ~1 s inside the UI loop.
#ifndef CHSC6X_I2C_TIMEOUT_MS
#define CHSC6X_I2C_TIMEOUT_MS 10
#endif
class CHSC6XTouch {
public:
// Probes the bus. Returns false (and disables itself) when nothing answers,
// so a board without the touch panel simply carries on without it.
// Probes the bus for diagnostics only. The result must NOT gate polling: this
// controller NACKs its address whenever it has nothing to report, so a single
// idle probe at boot is indistinguishable from absent hardware. checkTap()
// keeps polling either way, and a NACK there costs one quiet, fast bus cycle.
bool begin(TwoWire& wire = Wire) {
_wire = &wire;
_wire->beginTransmission((uint8_t)CHSC6X_I2C_ADDR);
@@ -58,8 +54,12 @@ public:
// True exactly once per new touch.
bool checkTap(uint32_t now_ms) {
if (!_present) return false;
return _detector.update(now_ms, readPressed());
bool pressed = readPressed();
if (pressed && !_present) {
_present = true; // answered late; the boot probe caught it mid-idle
Serial.println("Touch: CHSC6X responding");
}
return _detector.update(now_ms, pressed);
}
private:
@@ -75,15 +75,12 @@ private:
_wire->beginTransmission((uint8_t)CHSC6X_I2C_ADDR);
if (_wire->endTransmission() != 0) return false;
const uint16_t prev_timeout = _wire->getTimeOut();
_wire->setTimeOut(CHSC6X_I2C_TIMEOUT_MS);
uint8_t got = _wire->requestFrom((uint8_t)CHSC6X_I2C_ADDR, (uint8_t)CHSC6X_READ_LEN);
uint8_t buf[CHSC6X_READ_LEN];
for (uint8_t i = 0; i < CHSC6X_READ_LEN; i++) {
buf[i] = i < got ? (uint8_t)_wire->read() : 0xFF;
}
while (_wire->available()) _wire->read(); // drain a short read
_wire->setTimeOut(prev_timeout);
if (got != CHSC6X_READ_LEN) {
logRaw(got, NULL);
+5 -1
View File
@@ -9,8 +9,12 @@
// elapsed-time comparisons are unsigned subtractions, so millis() rollover is
// a non-event.
// Must exceed the caller's poll interval, or a state change is confirmed by the
// very next sample and the debounce does nothing. At the 50 ms touch poll this
// requires two consecutive consistent reads, so one NACK or short read during a
// continuous touch cannot fake a release (and therefore a second tap).
#ifndef TOUCH_TAP_DEBOUNCE_MS
#define TOUCH_TAP_DEBOUNCE_MS 40
#define TOUCH_TAP_DEBOUNCE_MS 80
#endif
// Ignores a second tap arriving this soon after an accepted one, so a bouncy