2019-07-24 09:25:07 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the "bluetoothheater" distribution
|
|
|
|
* (https://gitlab.com/mrjones.id.au/bluetoothheater)
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 Ray Jones <ray@mrjones.id.au>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "../RTC/RTCStore.h"
|
|
|
|
#include "NVStorage.h"
|
|
|
|
|
2019-07-26 12:13:46 +00:00
|
|
|
#define DEBUG_HOURMETER
|
|
|
|
|
|
|
|
|
2019-07-24 09:25:07 +00:00
|
|
|
class CProtocol;
|
|
|
|
|
2019-07-26 12:13:46 +00:00
|
|
|
class sRunTime {
|
|
|
|
float& persistentVal;
|
|
|
|
unsigned long lastSampleTime;
|
|
|
|
public:
|
|
|
|
sRunTime(float& refVal) : persistentVal(refVal) {
|
|
|
|
lastSampleTime = 0;
|
|
|
|
};
|
|
|
|
void reset() {
|
|
|
|
persistentVal = 0;
|
|
|
|
};
|
|
|
|
uint32_t get() const {
|
|
|
|
return (uint32_t)persistentVal;
|
|
|
|
}
|
|
|
|
bool active() const {
|
|
|
|
return lastSampleTime != 0;
|
|
|
|
}
|
|
|
|
void stop() {
|
|
|
|
lastSampleTime = 0;
|
|
|
|
}
|
|
|
|
float recordTime(unsigned long now) {
|
|
|
|
float rVal = 0;
|
|
|
|
if(lastSampleTime)
|
|
|
|
rVal = float((unsigned long)(now - lastSampleTime)) * 0.001;
|
|
|
|
lastSampleTime = now;
|
|
|
|
persistentVal += rVal;
|
|
|
|
return rVal;
|
|
|
|
}
|
|
|
|
void offset(float ofs) {
|
|
|
|
persistentVal += ofs;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-07-24 09:25:07 +00:00
|
|
|
class CHourMeter {
|
2019-07-26 12:13:46 +00:00
|
|
|
const int RTC_storageInterval = 60 * 10; // 10 minutes
|
2019-07-25 20:39:56 +00:00
|
|
|
|
2019-07-26 12:13:46 +00:00
|
|
|
sRunTime RunTime;
|
|
|
|
sRunTime GlowTime;
|
2019-07-25 20:39:56 +00:00
|
|
|
uint32_t _getLclRunTime(); // volatile persistent variable + RTC stored rollovers
|
|
|
|
uint32_t _getLclGlowTime(); // volatile persistent variable + RTC stored rollovers
|
2019-07-24 09:25:07 +00:00
|
|
|
public:
|
2019-07-24 20:06:47 +00:00
|
|
|
CHourMeter(float &runtime, float& glowtime) :
|
2019-07-26 12:13:46 +00:00
|
|
|
RunTime(runtime),
|
|
|
|
GlowTime(glowtime)
|
2019-07-24 20:06:47 +00:00
|
|
|
{
|
2019-07-26 12:13:46 +00:00
|
|
|
#ifdef DEBUG_HOURMETER
|
2019-07-27 14:28:39 +00:00
|
|
|
DebugPort.printf("CHourMeter::CHourMeter %d %d\r\n", RunTime.get(), GlowTime.get());
|
2019-07-26 12:13:46 +00:00
|
|
|
#endif
|
2019-07-24 09:25:07 +00:00
|
|
|
};
|
2019-07-25 12:31:31 +00:00
|
|
|
void init(bool poweron);
|
2019-07-25 20:39:56 +00:00
|
|
|
void reset();
|
|
|
|
void store(); // transfer current state to permanent NV storage
|
2019-07-24 09:25:07 +00:00
|
|
|
void monitor(const CProtocol& frame);
|
2019-07-25 20:39:56 +00:00
|
|
|
uint32_t getRunTime(); // total time, local tracked + last NV stored value
|
|
|
|
uint32_t getGlowTime(); // total time, local tracked + last NV stored value
|
2019-07-26 12:13:46 +00:00
|
|
|
void resetHard();
|
2019-07-24 09:25:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
extern CHourMeter* pHourMeter;
|