tidied Hourmeter class, staged NV changes to write in correct time slot
This commit is contained in:
parent
1dbfa67163
commit
f86ae7cffb
|
@ -26,37 +26,56 @@
|
|||
#include "../Protocol/Protocol.h"
|
||||
#include "../Utility/NVStorage.h"
|
||||
|
||||
const int baseSeconds = 60 * 15; // 15 minutes
|
||||
|
||||
|
||||
void
|
||||
CHourMeter::init(bool poweron)
|
||||
{
|
||||
if(poweron) {
|
||||
_RunTime = 0; // difinitely untrustworthy after power on or OTA updates
|
||||
_RunTime = 0; // definitely 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;
|
||||
_GlowTime = 0;
|
||||
if(_RunTime || _GlowTime || RTC_Store.getRunTime() || RTC_Store.getGlowTime()) {
|
||||
store();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
CHourMeter::reset()
|
||||
{
|
||||
RTC_Store.resetRunTime();
|
||||
RTC_Store.resetGlowTime();
|
||||
_RunTime = 0;
|
||||
_GlowTime = 0;
|
||||
}
|
||||
|
||||
void
|
||||
CHourMeter::store()
|
||||
{
|
||||
sHourMeter NV = NVstore.getHourMeter();
|
||||
NV.RunTime += _RunTime + baseSeconds * RTC_Store.getRunTime(); // add any residual to the real NV stored value
|
||||
NV.GlowTime += _GlowTime + baseSeconds * RTC_Store.getGlowTime();
|
||||
NVstore.setHourMeter(NV);
|
||||
reset();
|
||||
}
|
||||
|
||||
void
|
||||
CHourMeter::monitor(const CProtocol& frame)
|
||||
{
|
||||
// long now = Clock.get().secondstime();
|
||||
unsigned long now = millis();
|
||||
if(frame.getRunState() != 0) {
|
||||
if(frame.getRunState() == 0) {
|
||||
// heater is stopped - save remnant times to flash
|
||||
if(_lastRunTime) {
|
||||
store(); // heater hass stopped - save remnant times to flash
|
||||
}
|
||||
_lastRunTime = 0;
|
||||
_lastGlowTime = 0;
|
||||
}
|
||||
else {
|
||||
// heater is running
|
||||
if(_lastRunTime != 0) {
|
||||
// first sample after start is ignored
|
||||
float tDelta = float(now - _lastRunTime) * 0.001;
|
||||
_RunTime += tDelta;
|
||||
if(frame.getGlowPlug_Voltage() != 0) {
|
||||
|
@ -70,57 +89,57 @@ CHourMeter::monitor(const CProtocol& frame)
|
|||
}
|
||||
}
|
||||
_lastRunTime = now;
|
||||
sHourMeter FLASHmem = NVstore.getHourMeter();
|
||||
sHourMeter NV = NVstore.getHourMeter();
|
||||
// test for rollover of our local run time tracking
|
||||
if(_RunTime > baseSeconds) {
|
||||
if(RTC_Store.incRunTime()) {
|
||||
// rolled RTC intermediate store - push into FLASH
|
||||
FLASHmem.RunTime += baseSeconds * RTC_Store.getMaxRunTime();
|
||||
}
|
||||
_RunTime -= baseSeconds;
|
||||
}
|
||||
if(_GlowTime > baseSeconds) {
|
||||
if(RTC_Store.incGlowTime()) {
|
||||
if(RTC_Store.incRunTime()) { // returns true if rolled back to zero
|
||||
// rolled RTC intermediate store - push into FLASH
|
||||
FLASHmem.GlowTime += baseSeconds * RTC_Store.getMaxGlowTime(); // store whole seconds to flash memory
|
||||
NV.RunTime += baseSeconds * RTC_Store.getMaxRunTime(); // bump by our maximum storable time
|
||||
}
|
||||
}
|
||||
// test for rollover of our local glow time tracking
|
||||
if(_GlowTime > baseSeconds) {
|
||||
_GlowTime -= baseSeconds;
|
||||
if(RTC_Store.incGlowTime()) { // returns true if rolled back to zero
|
||||
// rolled RTC intermediate store - push into FLASH
|
||||
NV.GlowTime += baseSeconds * RTC_Store.getMaxGlowTime(); // bump by our maximum storable time
|
||||
}
|
||||
}
|
||||
NVstore.setHourMeter(FLASHmem);
|
||||
}
|
||||
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;
|
||||
_lastGlowTime = 0;
|
||||
NVstore.setHourMeter(NV); // internally moderated, only actually saves if values have changed
|
||||
}
|
||||
// DebugPort.printf("CHourMeter %f %f\r\n", _RunTime, _GlowTime);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
CHourMeter::_getLclRunTime()
|
||||
{
|
||||
uint32_t rt = (uint32_t)_RunTime;
|
||||
DebugPort.printf("HrMtr _GetLclRunTime(): %d %d\r\n", rt, RTC_Store.getRunTime());
|
||||
return rt + baseSeconds * RTC_Store.getRunTime();
|
||||
}
|
||||
|
||||
uint32_t
|
||||
CHourMeter::_getLclGlowTime()
|
||||
{
|
||||
uint32_t gt = (uint32_t)_GlowTime;
|
||||
DebugPort.printf("HrMtr _GetLclGlowTime(): %d %d\r\n", gt, RTC_Store.getGlowTime());
|
||||
return gt + baseSeconds * RTC_Store.getGlowTime();
|
||||
}
|
||||
|
||||
uint32_t
|
||||
CHourMeter::getRunTime()
|
||||
{
|
||||
uint32_t rt = (uint32_t)_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() + NVstore.getHourMeter().RunTime;
|
||||
uint32_t rt = _getLclRunTime();
|
||||
DebugPort.printf("HrMtr GetRunTime(): %d %d\r\n", rt, NVstore.getHourMeter().RunTime);
|
||||
return rt + NVstore.getHourMeter().RunTime;
|
||||
}
|
||||
|
||||
uint32_t
|
||||
CHourMeter::getGlowTime()
|
||||
{
|
||||
uint32_t gt = (uint32_t)_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;
|
||||
uint32_t gt = _getLclGlowTime();
|
||||
DebugPort.printf("HrMtr GetGlowTime(): %d %d\r\n", gt, NVstore.getHourMeter().RunTime);
|
||||
return gt + NVstore.getHourMeter().GlowTime;
|
||||
}
|
||||
|
||||
|
|
|
@ -26,10 +26,14 @@
|
|||
class CProtocol;
|
||||
|
||||
class CHourMeter {
|
||||
const int baseSeconds = 60 * 15; // 15 minutes
|
||||
|
||||
float& _RunTime;
|
||||
float& _GlowTime;
|
||||
unsigned long _lastRunTime;
|
||||
unsigned long _lastGlowTime;
|
||||
uint32_t _getLclRunTime(); // volatile persistent variable + RTC stored rollovers
|
||||
uint32_t _getLclGlowTime(); // volatile persistent variable + RTC stored rollovers
|
||||
public:
|
||||
CHourMeter(float &runtime, float& glowtime) :
|
||||
_RunTime(runtime),
|
||||
|
@ -44,9 +48,11 @@ public:
|
|||
_GlowTime = glowtime;
|
||||
};
|
||||
void init(bool poweron);
|
||||
void reset();
|
||||
void store(); // transfer current state to permanent NV storage
|
||||
void monitor(const CProtocol& frame);
|
||||
uint32_t getRunTime();
|
||||
uint32_t getGlowTime();
|
||||
uint32_t getRunTime(); // total time, local tracked + last NV stored value
|
||||
uint32_t getGlowTime(); // total time, local tracked + last NV stored value
|
||||
};
|
||||
|
||||
extern CHourMeter* pHourMeter;
|
|
@ -181,8 +181,8 @@ CHeaterStorage::setHourMeter(const sHourMeter& newVals)
|
|||
{
|
||||
// if(_calValues.hourMeter != newVals) {
|
||||
if(newVals != _calValues.hourMeter) {
|
||||
_calValues.hourMeter = newVals;
|
||||
_calValues.hourMeter.save();
|
||||
_calValues.hourMeter = newVals; // stage changes
|
||||
save(); // request save to NV
|
||||
return true;
|
||||
};
|
||||
return false;
|
||||
|
@ -221,6 +221,7 @@ CESP32HeaterStorage::load()
|
|||
_calValues.userSettings.load();
|
||||
_calValues.MQTT.load();
|
||||
_calValues.Credentials.load();
|
||||
_calValues.hourMeter.load();
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -228,15 +229,16 @@ CESP32HeaterStorage::doSave()
|
|||
{
|
||||
if(_bShouldSave) {
|
||||
_bShouldSave = false;
|
||||
DebugPort.println("Saving to NV storage");
|
||||
_calValues.heaterTuning.save();
|
||||
for(int i=0; i<14; i++) {
|
||||
_calValues.timer[i].save();
|
||||
DebugPort.println("Saving to NV storage");
|
||||
_calValues.heaterTuning.save();
|
||||
for(int i=0; i<14; i++) {
|
||||
_calValues.timer[i].save();
|
||||
}
|
||||
_calValues.userSettings.save();
|
||||
_calValues.MQTT.save();
|
||||
_calValues.Credentials.save();
|
||||
_calValues.hourMeter.save();
|
||||
}
|
||||
_calValues.userSettings.save();
|
||||
_calValues.MQTT.save();
|
||||
_calValues.Credentials.save();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
|
|
Loading…
Reference in a new issue