Storing actual runtimes to NV store at heater off, resetting intermediates at same time.

Improved restart behaviour of persistent variable handling
This commit is contained in:
Ray Jones 2019-07-25 22:31:31 +10:00
parent 2084c7d60c
commit 1dbfa67163
6 changed files with 74 additions and 17 deletions

View file

@ -458,11 +458,9 @@ void setup() {
// bCyclicEngaged = RTC_Store.getCyclicEngaged(); // bCyclicEngaged = RTC_Store.getCyclicEngaged();
DebugPort.printf("Previous cyclic active = %d\r\n", RTC_Store.getCyclicEngaged()); // state flag required for cyclic mode to persist properly after a WD reboot :-) DebugPort.printf("Previous cyclic active = %d\r\n", RTC_Store.getCyclicEngaged()); // state flag required for cyclic mode to persist properly after a WD reboot :-)
pHourMeter = new CHourMeter(persistentRunTime, persistentGlowTime); pHourMeter = new CHourMeter(persistentRunTime, persistentGlowTime); // persistent vars are only trustworthy with SW reboots
if(bESP32PowerUpInit || RTC_Store.getBootInit()) { // ensure we reset out persistent vars on power up, or OTA update pHourMeter->init(bESP32PowerUpInit || RTC_Store.getBootInit()); // ensure persistent memory variable are reset after powerup, or OTA update
RTC_Store.setBootInit(false); RTC_Store.setBootInit(false);
pHourMeter->powerOnInit(); // ensure persistent memory variable are reset after powerup
}
delay(1000); // just to hold the splash screeen for while delay(1000); // just to hold the splash screeen for while
} }
@ -1250,6 +1248,16 @@ void checkDebugCommands()
else if(rxVal == 'r') { else if(rxVal == 'r') {
ESP.restart(); // reset the esp ESP.restart(); // reset the esp
} }
else if(rxVal == ('h' & 0x1f)) { // CTRL-H hourmeter reset
sHourMeter FLASHmem = NVstore.getHourMeter();
FLASHmem.RunTime = 0;
FLASHmem.GlowTime = 0;
NVstore.setHourMeter(FLASHmem);
RTC_Store.resetRunTime();
RTC_Store.resetGlowTime();
persistentRunTime = 0;
persistentGlowTime = 0;
}
} }
#ifdef PROTOCOL_INVESTIGATION #ifdef PROTOCOL_INVESTIGATION
if(bSendVal) { if(bSendVal) {

View file

@ -154,6 +154,19 @@ CRTC_Store::getDesiredPump()
return _demandPump; return _demandPump;
} }
void
CRTC_Store::resetRunTime()
{
_RunTime = 0;
_PackAndSaveByte6();
}
void
CRTC_Store::resetGlowTime()
{
_GlowTime = 0;
_PackAndSaveByte6();
}
bool bool
CRTC_Store::incRunTime() CRTC_Store::incRunTime()

View file

@ -46,6 +46,8 @@ public:
void setFuelGauge(float val); void setFuelGauge(float val);
void setDesiredTemp(uint8_t val); void setDesiredTemp(uint8_t val);
void setDesiredPump(uint8_t val); void setDesiredPump(uint8_t val);
void resetRunTime();
void resetGlowTime();
bool incRunTime(); bool incRunTime();
bool incGlowTime(); bool incGlowTime();
void setCyclicEngaged(bool _CyclicEngaged); void setCyclicEngaged(bool _CyclicEngaged);

View file

@ -30,11 +30,25 @@ const int baseSeconds = 60 * 15; // 15 minutes
void void
CHourMeter::powerOnInit() CHourMeter::init(bool poweron)
{ {
if(poweron) {
_RunTime = 0; // difinitely untrustworthy after power on or OTA updates
_GlowTime = 0;
}
int rt = RTC_Store.getRunTime();
int gt = RTC_Store.getGlowTime();
if(rt || gt) {
sHourMeter FLASHmem = NVstore.getHourMeter();
FLASHmem.RunTime += _RunTime + baseSeconds * rt;
FLASHmem.GlowTime += _GlowTime + baseSeconds * gt;
NVstore.setHourMeter(FLASHmem);
RTC_Store.resetRunTime();
RTC_Store.resetGlowTime();
_RunTime = 0; _RunTime = 0;
_GlowTime = 0; _GlowTime = 0;
} }
}
void void
CHourMeter::monitor(const CProtocol& frame) CHourMeter::monitor(const CProtocol& frame)
@ -58,37 +72,55 @@ CHourMeter::monitor(const CProtocol& frame)
_lastRunTime = now; _lastRunTime = now;
sHourMeter FLASHmem = NVstore.getHourMeter(); sHourMeter FLASHmem = NVstore.getHourMeter();
if(_RunTime > baseSeconds) { if(_RunTime > baseSeconds) {
if(RTC_Store.incRunTime()) if(RTC_Store.incRunTime()) {
FLASHmem.RunTime++; // rolled RTC intermediate store - push into FLASH
FLASHmem.RunTime += baseSeconds * RTC_Store.getMaxRunTime();
}
_RunTime -= baseSeconds; _RunTime -= baseSeconds;
} }
if(_GlowTime > baseSeconds) { if(_GlowTime > baseSeconds) {
if(RTC_Store.incGlowTime()) if(RTC_Store.incGlowTime()) {
FLASHmem.GlowTime++; // rolled RTC intermediate store - push into FLASH
FLASHmem.GlowTime += baseSeconds * RTC_Store.getMaxGlowTime(); // store whole seconds to flash memory
}
_GlowTime -= baseSeconds; _GlowTime -= baseSeconds;
} }
NVstore.setHourMeter(FLASHmem); NVstore.setHourMeter(FLASHmem);
} }
else { else {
// heater has been stopped - save remant times to flash
if(_lastRunTime) {
sHourMeter FLASHmem = NVstore.getHourMeter();
FLASHmem.RunTime += _RunTime + baseSeconds * RTC_Store.getRunTime();
FLASHmem.GlowTime += _GlowTime + baseSeconds * RTC_Store.getGlowTime();
NVstore.setHourMeter(FLASHmem);
_RunTime = 0;
_GlowTime = 0;
RTC_Store.resetRunTime();
RTC_Store.resetGlowTime();
}
_lastRunTime = 0; _lastRunTime = 0;
_lastGlowTime = 0; _lastGlowTime = 0;
} }
DebugPort.printf("CHourMeter %f %f\r\n", _RunTime, _GlowTime); // DebugPort.printf("CHourMeter %f %f\r\n", _RunTime, _GlowTime);
} }
uint32_t uint32_t
CHourMeter::getRunTime() CHourMeter::getRunTime()
{ {
uint32_t rt = (uint32_t)_RunTime; uint32_t rt = (uint32_t)_RunTime;
DebugPort.printf("HrMtr: %d %d %d\r\n", rt, RTC_Store.getRunTime(), NVstore.getHourMeter().RunTime); DebugPort.printf("HrMtr Run: %d %d %d\r\n", rt, RTC_Store.getRunTime(), NVstore.getHourMeter().RunTime);
return rt + baseSeconds * RTC_Store.getRunTime() + baseSeconds * RTC_Store.getMaxRunTime() * NVstore.getHourMeter().RunTime; // return rt + baseSeconds * RTC_Store.getRunTime() + baseSeconds * RTC_Store.getMaxRunTime() * NVstore.getHourMeter().RunTime;
return rt + baseSeconds * RTC_Store.getRunTime() + NVstore.getHourMeter().RunTime;
} }
uint32_t uint32_t
CHourMeter::getGlowTime() CHourMeter::getGlowTime()
{ {
uint32_t gt = (uint32_t)_GlowTime; uint32_t gt = (uint32_t)_GlowTime;
return gt + baseSeconds * RTC_Store.getGlowTime() + baseSeconds * RTC_Store.getMaxGlowTime() * NVstore.getHourMeter().GlowTime; // return gt + baseSeconds * RTC_Store.getGlowTime() + baseSeconds * RTC_Store.getMaxGlowTime() * NVstore.getHourMeter().GlowTime;
DebugPort.printf("HrMtr Glow: %d %d %d\r\n", gt, RTC_Store.getGlowTime(), NVstore.getHourMeter().GlowTime);
return gt + baseSeconds * RTC_Store.getGlowTime() + NVstore.getHourMeter().GlowTime;
} }

View file

@ -43,7 +43,7 @@ public:
_RunTime = runtime; _RunTime = runtime;
_GlowTime = glowtime; _GlowTime = glowtime;
}; };
void powerOnInit(); void init(bool poweron);
void monitor(const CProtocol& frame); void monitor(const CProtocol& frame);
uint32_t getRunTime(); uint32_t getRunTime();
uint32_t getGlowTime(); uint32_t getGlowTime();

View file

@ -464,6 +464,7 @@ sHourMeter::save()
preferences.putULong("RunTime", RunTime); preferences.putULong("RunTime", RunTime);
preferences.putULong("GlowTime", GlowTime); preferences.putULong("GlowTime", GlowTime);
preferences.end(); preferences.end();
DebugPort.printf("Hourmeter NV save: Run=%d, Glow=%d\r\n", RunTime, GlowTime);
} }
void void
@ -474,4 +475,5 @@ sHourMeter::load()
validatedLoad("RunTime", RunTime, 0, 0, 0xffffffffL); validatedLoad("RunTime", RunTime, 0, 0, 0xffffffffL);
validatedLoad("GlowTime", GlowTime, 0, 0, 0xffffffffL); validatedLoad("GlowTime", GlowTime, 0, 0, 0xffffffffL);
preferences.end(); preferences.end();
DebugPort.printf("Hourmeter NV read: Run=%d, Glow=%d\r\n", RunTime, GlowTime);
} }