Skip to content

Commit f241320

Browse files
committed
Add monitor state reset on device reconnection
Introduces a resetState() method to IMonitor and its implementations, allowing monitor alert states to be reset to OK when ESC or BMS devices reconnect. This ensures alerts are re-evaluated cleanly after a device is reconnected, improving reliability and user feedback.
1 parent f99cf14 commit f241320

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

inc/sp140/simple_monitor.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ struct IMonitor {
117117
virtual void check() = 0;
118118
virtual SensorID getSensorID() const = 0;
119119
virtual SensorCategory getCategory() const = 0; // Each monitor knows its category
120+
virtual void resetState() = 0; // Reset internal alert state to OK
120121
};
121122

122123
// Sensor monitor for analog values
@@ -154,6 +155,10 @@ struct SensorMonitor : public IMonitor {
154155
SensorCategory getCategory() const override {
155156
return category;
156157
}
158+
159+
void resetState() override {
160+
last = AlertLevel::OK;
161+
}
157162
};
158163

159164
// New monitor for boolean conditions
@@ -189,6 +194,10 @@ struct BooleanMonitor : public IMonitor {
189194
SensorCategory getCategory() const override {
190195
return category;
191196
}
197+
198+
void resetState() override {
199+
lastState = !alertOnTrue; // Reset to the non-alerting state
200+
}
192201
};
193202

194203
// Global monitor registry

src/sp140/simple_monitor.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,25 @@ void checkAllSensorsWithData(const STR_ESC_TELEMETRY_140& escData,
338338
}
339339
}
340340

341+
// If a device reconnected, reset monitor states so they can alert fresh (like at boot)
342+
if (!prevEscConnected && escConnected) {
343+
USBSerial.println("[MONITOR] ESC reconnected - resetting ESC monitor states");
344+
for (auto* monitor : monitors) {
345+
if (monitor && monitor->getCategory() == SensorCategory::ESC) {
346+
monitor->resetState();
347+
}
348+
}
349+
}
350+
351+
if (!prevBmsConnected && bmsConnected) {
352+
USBSerial.println("[MONITOR] BMS reconnected - resetting BMS monitor states");
353+
for (auto* monitor : monitors) {
354+
if (monitor && monitor->getCategory() == SensorCategory::BMS) {
355+
monitor->resetState();
356+
}
357+
}
358+
}
359+
341360
// Update previous connection states
342361
prevEscConnected = escConnected;
343362
prevBmsConnected = bmsConnected;

0 commit comments

Comments
 (0)