From 92283a3e0d24dc5d350dc2863f28412dc545c0cc Mon Sep 17 00:00:00 2001 From: Ray Jones Date: Wed, 24 Jul 2019 19:25:07 +1000 Subject: [PATCH] Added module files for fuelgauge, RTCStore, HourMeter --- data/favicon.ico | Bin 1150 -> 0 bytes data/index.html | 609 ------------------------------ data/update.html | 191 ---------- src/Afterburner.cpp | 19 +- src/OLED/FuelCalScreen.cpp | 299 +++++++++++++++ src/OLED/HeaterSettingsScreen.cpp | 278 -------------- src/RTC/Clock.cpp | 150 -------- src/RTC/Clock.h | 25 -- src/RTC/RTCStore.cpp | 238 ++++++++++++ src/RTC/RTCStore.h | 62 +++ src/Utility/BTC_JSON.cpp | 1 + src/Utility/FuelGauge.cpp | 2 +- src/Utility/HourMeter.cpp | 53 +++ src/Utility/HourMeter.h | 46 +++ 14 files changed, 715 insertions(+), 1258 deletions(-) delete mode 100644 data/favicon.ico delete mode 100644 data/index.html delete mode 100644 data/update.html create mode 100644 src/OLED/FuelCalScreen.cpp create mode 100644 src/RTC/RTCStore.cpp create mode 100644 src/RTC/RTCStore.h create mode 100644 src/Utility/HourMeter.cpp create mode 100644 src/Utility/HourMeter.h diff --git a/data/favicon.ico b/data/favicon.ico deleted file mode 100644 index f225d9dcb96277272ee402fefa503c48a823dcb2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1150 zcmeHHxoSdD5WRtPX(UDJl=-y$la+{tB8Z5Es8}g3xGOHW;)+}0g5q}a&d7)fsZwVi z@7}qyo;mmNjCryio6T73yZL-J=FJ%MVZD~gvfS*}W{l-~eYw4Iwif)$oq_B1DmwQ^ z{c^e7V}2@(Q$O|d`79yF;}M6$;U2CvwXQn!d^(*Fi^Y&gBv7x{;rILDbUHDc&5%qc zvE6PZMdNi}E|;iODrhtsD3{BaOeTVd_ExKfLZJYk&nLCp?KVcE5jLC69ka*df!%J$ zbUGE^U@-XB=Mm?6y~c1jM6p - - - - - - - - - -Chinese Diesel Heater Web Controller Interface - - -
- - - -
- -

Power Control

- -
- - -
- - -
-

Temperature Control

-
- -
- Desired Temp: -
-
-Fixed Hz: - -
-
-Current Temp: -
- -
- -
-

Advanced Settings

-
-

Minimum Fuel Settings

-
-Pump Min: - -
-
-Fan Min: - -
-
-

Maximum Fuel Settings

-
-Pump Max: - -
-
-Fan Max: - -
-
- - -
-Current Date:
- - -
-Current Time (24 Hour Format):
- - -

-
- -

MQTT Settings

-Enabled: -
-Host/Server:
-Port:
-Username:
-Password:

- -
-

- -
-

-

-Timer2:
- -
-
- - diff --git a/data/update.html b/data/update.html deleted file mode 100644 index c1699e8..0000000 --- a/data/update.html +++ /dev/null @@ -1,191 +0,0 @@ - - - - - - - - - - - Afterburner update - - -

Afterburner update

-
- - -

- - -

- - - -
-

- - - - diff --git a/src/Afterburner.cpp b/src/Afterburner.cpp index 7992e75..23a9e10 100644 --- a/src/Afterburner.cpp +++ b/src/Afterburner.cpp @@ -90,6 +90,7 @@ #include "cfg/pins.h" #include "RTC/Timers.h" #include "RTC/Clock.h" +#include "RTC/RTCStore.h" #include "WiFi/BTCWifi.h" #include "WiFi/BTCWebServer.h" #include "WiFi/BTCota.h" @@ -109,6 +110,7 @@ #include "OLED/KeyPad.h" #include "Utility/TempSense.h" #include "Utility/DataFilter.h" +#include "Utility/HourMeter.h" #include #include #include @@ -183,6 +185,10 @@ bool bHasOEMLCDController = false; bool bHasHtrData = false; // these variables will persist over a soft reboot. +__NOINIT_ATTR int persistentRunTime; +__NOINIT_ATTR int persistentGlowTime; +CHourMeter* pHourMeter = NULL; + __NOINIT_ATTR bool bForceInit; // = false; //__NOINIT_ATTR bool bUserON; // = false; //__NOINIT_ATTR uint8_t demandDegC; @@ -310,11 +316,11 @@ extern "C" unsigned long __wrap_millis() { void setup() { // ensure cyclic mode is disabled after power on -// bool bPowerUpInit = false; -// if(rtc_get_reset_reason(0) == 1/* || bForceInit*/) { -// bPowerUpInit = true; + bool bESP32PowerUpInit = false; + if(rtc_get_reset_reason(0) == 1/* || bForceInit*/) { + bESP32PowerUpInit = true; // bForceInit = false; -// } + } // initially, ensure the GPIO outputs are not activated during startup // (GPIO2 tends to be one with default chip startup) @@ -459,6 +465,11 @@ void setup() { // 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 :-) + pHourMeter = new CHourMeter(persistentRunTime, persistentGlowTime); + if(bESP32PowerUpInit) { + pHourMeter->powerOnInit(); // ensure persistent memory variable are reset after powerup + } + delay(1000); // just to hold the splash screeen for while } diff --git a/src/OLED/FuelCalScreen.cpp b/src/OLED/FuelCalScreen.cpp new file mode 100644 index 0000000..b15cb54 --- /dev/null +++ b/src/OLED/FuelCalScreen.cpp @@ -0,0 +1,299 @@ +/* + * This file is part of the "bluetoothheater" distribution + * (https://gitlab.com/mrjones.id.au/bluetoothheater) + * + * Copyright (C) 2018 Ray Jones + * + * 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 . + * + */ + +#include "128x64OLED.h" +#include "FuelCalScreen.h" +#include "KeyPad.h" +#include "../Utility/helpers.h" +#include "../Utility/macros.h" +#include "../Utility/NVStorage.h" +#include "../Protocol/Protocol.h" +#include "fonts/Icons.h" + +static const int Line3 = 14; +static const int Line2 = 27; +static const int Line1 = 40; + + +CFuelCalScreen::CFuelCalScreen(C128x64_OLED& display, CScreenManager& mgr) : CPasswordScreen(display, mgr) +{ + _initUI(); + _mlPerStroke = 0.02; + _LVC = 115; + _tOfs = 0; +} + +void +CFuelCalScreen::onSelect() +{ + CPasswordScreen::onSelect(); + _initUI(); + _mlPerStroke = NVstore.getHeaterTuning().pumpCal; + _LVC = NVstore.getHeaterTuning().lowVolts; + _tOfs = NVstore.getHeaterTuning().tempOfs; +} + +void +CFuelCalScreen::_initUI() +{ + _rowSel = 0; + _animateCount = 0; +} + +bool +CFuelCalScreen::show() +{ + char msg[20]; + const int col = 90; + + _display.fillRect(0, 50, 128, 14, BLACK); + _display.fillRect(col-border, Line3-border, 128-(col-1), 64-Line3-border, BLACK); + + if(!CPasswordScreen::show()) { // for showing "saving settings" + + if(_rowSel == 4) { + _printInverted(_display.xCentre(), 0, " Saving Settings ", true, eCentreJustify); + _printMenuText(_display.xCentre(), 35, "Press UP to", false, eCentreJustify); + _printMenuText(_display.xCentre(), 43, "confirm save", false, eCentreJustify); + } + else { + if(_animateCount < 0) { + _display.clearDisplay(); + _animateCount = 0; + } + _printInverted(_display.xCentre(), 0, " Special Features ", true, eCentreJustify); + // fuel calibration + int yPos = Line1; + _printMenuText(col, yPos, "mL/stroke : ", false, eRightJustify); + sprintf(msg, "%.03f", _mlPerStroke); + _printMenuText(col, yPos, msg, _rowSel == 1); + // low voltage cutout + yPos = Line2; + _printMenuText(col, yPos, "L.V.C. < ", false, eRightJustify); + if(_LVC) + sprintf(msg, "%.1fV", float(_LVC) * 0.1); + else + strcpy(msg, "OFF"); + _printMenuText(col, yPos, msg, _rowSel == 2); + // temp offset + yPos = Line3; + _printMenuText(col, yPos, "\367C offset : ", false, eRightJustify); + sprintf(msg, "%+.1f", _tOfs); + _printMenuText(col, yPos, msg, _rowSel == 3); + // navigation line + yPos = 53; + int xPos = _display.xCentre(); + + switch(_rowSel) { + case 0: + _printMenuText(xPos, yPos, " \021 Exit \020 ", true, eCentreJustify); + break; + default: + _display.drawFastHLine(0, 52, 128, WHITE); + _printMenuText(xPos, 56, "\030\031Sel \033\032 Adj", false, eCentreJustify); + _printMenuText(xPos, 56, "Save", false, eCentreJustify); + break; + } + } + } + + return true; +} + + +bool +CFuelCalScreen::animate() +{ + if(_animateCount >= 0) { + switch(_animateCount) { + case 0: + _display.fillRect(0, Line3-4, BatteryIconInfo.width, 40, BLACK); + _drawBitmap(6, Line1-3, FuelIconSmallInfo); + _drawBitmap(0, Line2-1 , BatteryIconInfo); + _drawBitmap(5, Line3-4, miniThermoIconInfo); + break; + case 2: + _display.fillRect(6, Line1-3, FuelIconSmallInfo.width, FuelIconSmallInfo.height, BLACK); // scrub prior drip + _drawBitmap(6, Line1-2, FuelIconSmallInfo); // drip fuel + _display.fillRect(BatteryIconInfo.width - 4, Line2+2, 2, 5, BLACK); // deplete battery + _display.fillRect(7, Line3+2, 2, 2, WHITE); // grow thermometer + break; + case 4: + _display.fillRect(6, Line1-2, FuelIconSmallInfo.width, FuelIconSmallInfo.height, BLACK); // scrub prior drip + _drawBitmap(6, Line1-1, FuelIconSmallInfo); // drip fuel + _display.fillRect(BatteryIconInfo.width - 7, Line2+2, 2, 5, BLACK); // deplete battery + _display.fillRect(7, Line3+1, 2, 1, WHITE); // grow thermometer + break; + case 6: + _display.fillRect(6, Line1-1, FuelIconSmallInfo.width, FuelIconSmallInfo.height, BLACK); // scrub prior drip + _drawBitmap(6, Line1, FuelIconSmallInfo); // drip fuel + _display.fillRect(BatteryIconInfo.width - 10, Line2+2, 2, 5, BLACK); // deplete battery + _display.fillRect(7, Line3, 2, 1, WHITE); // grow thermometer + break; + case 8: + _display.fillRect(6, Line1, FuelIconSmallInfo.width, FuelIconSmallInfo.height, BLACK); // scrub prior drip + _drawBitmap(6, Line1+1, FuelIconSmallInfo); // drip fuel + _display.fillRect(BatteryIconInfo.width - 13, Line2+2, 2, 5, BLACK); // deplete battery + _display.fillRect(7, Line3-1, 2, 1, WHITE); // grow thermometer + break; + } + + _animateCount++; + WRAPUPPERLIMIT(_animateCount, 9, 0); + } + + return true; +} + + +bool +CFuelCalScreen::keyHandler(uint8_t event) +{ + sHeaterTuning tuning; + + if(event & keyRepeat) { + if(event & key_Left) { + _adjust(-1); + } + if(event & key_Right) { + _adjust(+1); + } + } + + if(event & keyPressed) { + // press LEFT to select previous screen + if(event & key_Left) { + switch(_rowSel) { + case 0: + _ScreenManager.prevMenu(); + break; + case 1: + case 2: + case 3: + _adjust(-1); + break; + case 4: + _rowSel = 0; // abort save + break; + } + } + // press RIGHT to select next screen + if(event & key_Right) { + switch(_rowSel) { + case 0: + _ScreenManager.nextMenu(); + break; + case 1: + case 2: + case 3: + _adjust(+1); + break; + case 4: + _rowSel = 0; // abort save + break; + } + } + if(event & key_Down) { + _rowSel--; + LOWERLIMIT(_rowSel, 0); + } + // UP press + if(event & key_Up) { + switch(_rowSel) { + case 0: + case 1: + case 2: + case 3: + _rowSel++; + UPPERLIMIT(_rowSel, 3); + break; + case 4: // confirmed save + _display.clearDisplay(); + _animateCount = -1; + _showStoringMessage(); + tuning = NVstore.getHeaterTuning(); + tuning.pumpCal = _mlPerStroke; + tuning.lowVolts = _LVC; + tuning.tempOfs = _tOfs; + NVstore.setHeaterTuning(tuning); + saveNV(); + _rowSel = 0; + break; + } + } + // CENTRE press + if(event & key_Centre) { + switch(_rowSel) { + case 0: + _ScreenManager.selectMenu(CScreenManager::RootMenuLoop); + break; + case 1: + case 2: + case 3: + _animateCount = -1; + _display.clearDisplay(); + _rowSel = 4; + break; + } + } + _ScreenManager.reqUpdate(); + } + + return true; +} + +void +CFuelCalScreen::_adjust(int dir) +{ + switch(_rowSel) { + case 1: + _mlPerStroke += dir * 0.001; + BOUNDSLIMIT(_mlPerStroke, 0.001, 1); + break; + case 2: + if(_LVC == 0) { + if(NVstore.getHeaterTuning().sysVoltage == 120) + _LVC = dir > 0 ? 115 : 0; + else + _LVC = dir > 0 ? 230 : 0; + } + else { + _LVC += dir; + if(NVstore.getHeaterTuning().sysVoltage == 120) { + if(_LVC < 100) + _LVC = 0; + else + UPPERLIMIT(_LVC, 125); + } + else { + if(_LVC < 200) + _LVC = 0; + else + UPPERLIMIT(_LVC, 250); + } + } + break; + case 3: + _tOfs += dir * 0.1; + BOUNDSLIMIT(_tOfs, -10, 10); + break; + } +} diff --git a/src/OLED/HeaterSettingsScreen.cpp b/src/OLED/HeaterSettingsScreen.cpp index f2c1c50..9519925 100644 --- a/src/OLED/HeaterSettingsScreen.cpp +++ b/src/OLED/HeaterSettingsScreen.cpp @@ -27,7 +27,6 @@ #include "../Utility/macros.h" #include "../Utility/NVStorage.h" #include "../Protocol/Protocol.h" -#include "fonts/Icons.h" /////////////////////////////////////////////////////////////////////////// // @@ -249,280 +248,3 @@ CHeaterSettingsScreen::_adjust(int dir) break; } } - - - - - - - - - - - - - -CFuelCalScreen::CFuelCalScreen(C128x64_OLED& display, CScreenManager& mgr) : CPasswordScreen(display, mgr) -{ - _initUI(); - _mlPerStroke = 0.02; - _LVC = 115; - _tOfs = 0; -} - -void -CFuelCalScreen::onSelect() -{ - CPasswordScreen::onSelect(); - _initUI(); - _mlPerStroke = NVstore.getHeaterTuning().pumpCal; - _LVC = NVstore.getHeaterTuning().lowVolts; - _tOfs = NVstore.getHeaterTuning().tempOfs; -} - -void -CFuelCalScreen::_initUI() -{ - _rowSel = 0; - _animateCount = 0; -} - -bool -CFuelCalScreen::show() -{ - char msg[20]; - const int col = 90; - - _display.fillRect(0, 50, 128, 14, BLACK); - _display.fillRect(col-border, Line3-border, 128-(col-1), 64-Line3-border, BLACK); - - if(!CPasswordScreen::show()) { // for showing "saving settings" - - if(_rowSel == 4) { - _printInverted(_display.xCentre(), 0, " Saving Settings ", true, eCentreJustify); - _printMenuText(_display.xCentre(), 35, "Press UP to", false, eCentreJustify); - _printMenuText(_display.xCentre(), 43, "confirm save", false, eCentreJustify); - } - else { - if(_animateCount < 0) { - _display.clearDisplay(); - _animateCount = 0; - } - _printInverted(_display.xCentre(), 0, " Special Features ", true, eCentreJustify); - // fuel calibration - int yPos = Line1; - _printMenuText(col, yPos, "mL/stroke : ", false, eRightJustify); - sprintf(msg, "%.03f", _mlPerStroke); - _printMenuText(col, yPos, msg, _rowSel == 1); - // low voltage cutout - yPos = Line2; - _printMenuText(col, yPos, "L.V.C. < ", false, eRightJustify); - if(_LVC) - sprintf(msg, "%.1fV", float(_LVC) * 0.1); - else - strcpy(msg, "OFF"); - _printMenuText(col, yPos, msg, _rowSel == 2); - // temp offset - yPos = Line3; - _printMenuText(col, yPos, "\367C offset : ", false, eRightJustify); - sprintf(msg, "%+.1f", _tOfs); - _printMenuText(col, yPos, msg, _rowSel == 3); - // navigation line - yPos = 53; - int xPos = _display.xCentre(); - - switch(_rowSel) { - case 0: - _printMenuText(xPos, yPos, " \021 Exit \020 ", true, eCentreJustify); - break; - default: - _display.drawFastHLine(0, 52, 128, WHITE); - _printMenuText(xPos, 56, "\030\031Sel \033\032 Adj", false, eCentreJustify); - _printMenuText(xPos, 56, "Save", false, eCentreJustify); - break; - } - } - } - - return true; -} - - -bool -CFuelCalScreen::animate() -{ - if(_animateCount >= 0) { - switch(_animateCount) { - case 0: - _display.fillRect(0, Line3-4, BatteryIconInfo.width, 40, BLACK); - _drawBitmap(6, Line1-3, FuelIconSmallInfo); - _drawBitmap(0, Line2-1 , BatteryIconInfo); - _drawBitmap(5, Line3-4, miniThermoIconInfo); - break; - case 2: - _display.fillRect(6, Line1-3, FuelIconSmallInfo.width, FuelIconSmallInfo.height, BLACK); // scrub prior drip - _drawBitmap(6, Line1-2, FuelIconSmallInfo); // drip fuel - _display.fillRect(BatteryIconInfo.width - 4, Line2+2, 2, 5, BLACK); // deplete battery - _display.fillRect(7, Line3+2, 2, 2, WHITE); // grow thermometer - break; - case 4: - _display.fillRect(6, Line1-2, FuelIconSmallInfo.width, FuelIconSmallInfo.height, BLACK); // scrub prior drip - _drawBitmap(6, Line1-1, FuelIconSmallInfo); // drip fuel - _display.fillRect(BatteryIconInfo.width - 7, Line2+2, 2, 5, BLACK); // deplete battery - _display.fillRect(7, Line3+1, 2, 1, WHITE); // grow thermometer - break; - case 6: - _display.fillRect(6, Line1-1, FuelIconSmallInfo.width, FuelIconSmallInfo.height, BLACK); // scrub prior drip - _drawBitmap(6, Line1, FuelIconSmallInfo); // drip fuel - _display.fillRect(BatteryIconInfo.width - 10, Line2+2, 2, 5, BLACK); // deplete battery - _display.fillRect(7, Line3, 2, 1, WHITE); // grow thermometer - break; - case 8: - _display.fillRect(6, Line1, FuelIconSmallInfo.width, FuelIconSmallInfo.height, BLACK); // scrub prior drip - _drawBitmap(6, Line1+1, FuelIconSmallInfo); // drip fuel - _display.fillRect(BatteryIconInfo.width - 13, Line2+2, 2, 5, BLACK); // deplete battery - _display.fillRect(7, Line3-1, 2, 1, WHITE); // grow thermometer - break; - } - - _animateCount++; - WRAPUPPERLIMIT(_animateCount, 9, 0); - } - - return true; -} - - -bool -CFuelCalScreen::keyHandler(uint8_t event) -{ - sHeaterTuning tuning; - - if(event & keyRepeat) { - if(event & key_Left) { - _adjust(-1); - } - if(event & key_Right) { - _adjust(+1); - } - } - - if(event & keyPressed) { - // press LEFT to select previous screen - if(event & key_Left) { - switch(_rowSel) { - case 0: - _ScreenManager.prevMenu(); - break; - case 1: - case 2: - case 3: - _adjust(-1); - break; - case 4: - _rowSel = 0; // abort save - break; - } - } - // press RIGHT to select next screen - if(event & key_Right) { - switch(_rowSel) { - case 0: - _ScreenManager.nextMenu(); - break; - case 1: - case 2: - case 3: - _adjust(+1); - break; - case 4: - _rowSel = 0; // abort save - break; - } - } - if(event & key_Down) { - _rowSel--; - LOWERLIMIT(_rowSel, 0); - } - // UP press - if(event & key_Up) { - switch(_rowSel) { - case 0: - case 1: - case 2: - case 3: - _rowSel++; - UPPERLIMIT(_rowSel, 3); - break; - case 4: // confirmed save - _display.clearDisplay(); - _animateCount = -1; - _showStoringMessage(); - tuning = NVstore.getHeaterTuning(); - tuning.pumpCal = _mlPerStroke; - tuning.lowVolts = _LVC; - tuning.tempOfs = _tOfs; - NVstore.setHeaterTuning(tuning); - saveNV(); - _rowSel = 0; - break; - } - } - // CENTRE press - if(event & key_Centre) { - switch(_rowSel) { - case 0: - _ScreenManager.selectMenu(CScreenManager::RootMenuLoop); - break; - case 1: - case 2: - case 3: - _animateCount = -1; - _display.clearDisplay(); - _rowSel = 4; - break; - } - } - _ScreenManager.reqUpdate(); - } - - return true; -} - -void -CFuelCalScreen::_adjust(int dir) -{ - switch(_rowSel) { - case 1: - _mlPerStroke += dir * 0.001; - BOUNDSLIMIT(_mlPerStroke, 0.001, 1); - break; - case 2: - if(_LVC == 0) { - if(NVstore.getHeaterTuning().sysVoltage == 120) - _LVC = dir > 0 ? 115 : 0; - else - _LVC = dir > 0 ? 230 : 0; - } - else { - _LVC += dir; - if(NVstore.getHeaterTuning().sysVoltage == 120) { - if(_LVC < 100) - _LVC = 0; - else - UPPERLIMIT(_LVC, 125); - } - else { - if(_LVC < 200) - _LVC = 0; - else - UPPERLIMIT(_LVC, 250); - } - } - break; - case 3: - _tOfs += dir * 0.1; - BOUNDSLIMIT(_tOfs, -10, 10); - break; - } -} diff --git a/src/RTC/Clock.cpp b/src/RTC/Clock.cpp index 0951f25..af38dc1 100644 --- a/src/RTC/Clock.cpp +++ b/src/RTC/Clock.cpp @@ -205,153 +205,3 @@ RTC_DS3231Ex::resetLostPower() Wire.endTransmission(); } -// RTC storage, using alarm registers as GP storage -// MAXIMUM OF 7 BYTES -// -// [0..3] float fuelGauge strokes -// [4] uint8_t DesiredTemp (typ. 8-35) -// [5] uint8_t DesiredPump (typ. 8-35) -// [6] uint8_t spare -// -// ____________________________________________________ -// | b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 | -// |---------------|------|-----------------------------| -// Byte[4]: | CyclicEngaged | bit6 | Desired Deg Celcius | -// |---------------|------|-----------------------------| -// Byte[5]: | | | Desired Pump Speed | -// ---------------------------------------------------- - -CRTC_Store::CRTC_Store() -{ - _accessed[0] = false; - _accessed[1] = false; - _accessed[2] = false; - _accessed[3] = false; - _fuelgauge = 0; - _demandDegC = 22; - _demandPump = 22; - _CyclicEngaged = false; -} - -void -CRTC_Store::begin() -{ - if(Clock.lostPower()) { - // RTC lost power - reset internal NV values to defaults - DebugPort.println("CRTC_Store::begin() RTC lost power, re-initialising NV aspect"); - _demandPump = _demandDegC = 22; - _CyclicEngaged = false; - setFuelGauge(0); - setDesiredTemp(_demandDegC); - setDesiredPump(_demandPump); - Clock.resetLostPower(); - } - getFuelGauge(); - getDesiredTemp(); - getDesiredPump(); -} - -void -CRTC_Store::setFuelGauge(float val) -{ - _accessed[0] = true; - _fuelgauge = val; - Clock.saveData((uint8_t*)&val, 4, 0); -} - -float -CRTC_Store::getFuelGauge() -{ - if(!_accessed[0]) { - float NVval; - Clock.readData((uint8_t*)&NVval, 4, 0); - _fuelgauge = NVval; - _accessed[0] = true; - DebugPort.printf("RTC_Store - read fuel gauge %.2f\r\n", _fuelgauge); - } - return _fuelgauge; -} - -void -CRTC_Store::setDesiredTemp(uint8_t val) -{ - _demandDegC = val; - _PackAndSaveByte4(); -} - -uint8_t -CRTC_Store::getDesiredTemp() -{ - _ReadAndUnpackByte4(); - return _demandDegC; -} - -bool -CRTC_Store::getCyclicEngaged() -{ - _ReadAndUnpackByte4(); - return _CyclicEngaged; -} - -void -CRTC_Store::setCyclicEngaged(bool active) -{ - _CyclicEngaged = active; - _PackAndSaveByte4(); -} - -void -CRTC_Store::setDesiredPump(uint8_t val) -{ - _demandPump = val; - _PackAndSaveByte5(); -} - -uint8_t -CRTC_Store::getDesiredPump() -{ - _ReadAndUnpackByte5(); - return _demandPump; -} - -void -CRTC_Store::_ReadAndUnpackByte4() -{ - if(!_accessed[1]) { - uint8_t NVval = 0; - Clock.readData((uint8_t*)&NVval, 1, 4); - _demandDegC = NVval & 0x3f; - _CyclicEngaged = (NVval & 0x80) != 0; - _bit6 = (NVval & 0x40) != 0; - _accessed[1] = true; - DebugPort.printf("RTC_Store - read byte4: degC=%d, CyclicOn=%d, bit6=%d\r\n", _demandDegC, _CyclicEngaged, _bit6); - } -} - -void -CRTC_Store::_PackAndSaveByte4() -{ - uint8_t NVval = (_CyclicEngaged ? 0x80 : 0x00) - | (_bit6 ? 0x40 : 0x00) - | (_demandDegC & 0x3f); - Clock.saveData((uint8_t*)&NVval, 1, 4); -} - -void -CRTC_Store::_ReadAndUnpackByte5() -{ - if(!_accessed[2]) { - uint8_t NVval = 0; - Clock.readData((uint8_t*)&NVval, 1, 5); - _demandPump = NVval & 0x3f; - _accessed[2] = true; - DebugPort.printf("RTC_Store - read byte5: pump=%d\r\n", _demandPump); - } -} - -void -CRTC_Store::_PackAndSaveByte5() -{ - uint8_t NVval = (_demandPump & 0x3f); - Clock.saveData((uint8_t*)&NVval, 1, 5); -} diff --git a/src/RTC/Clock.h b/src/RTC/Clock.h index 3c9b0a5..a0ac4ba 100644 --- a/src/RTC/Clock.h +++ b/src/RTC/Clock.h @@ -73,31 +73,6 @@ public: void resetLostPower(); }; -class CRTC_Store { - bool _accessed[4]; // [0] - bytes 0..3, [1] byte 4, [2] byte 5, [3] byte 6 - float _fuelgauge; - uint8_t _demandDegC; - uint8_t _demandPump; - bool _CyclicEngaged; - bool _bit6; - void _ReadAndUnpackByte4(); - void _PackAndSaveByte4(); - void _ReadAndUnpackByte5(); - void _PackAndSaveByte5(); -public: - CRTC_Store(); - void begin(); - void setFuelGauge(float val); - void setDesiredTemp(uint8_t val); - void setDesiredPump(uint8_t val); - void setCyclicEngaged(bool _CyclicEngaged); - float getFuelGauge(); - uint8_t getDesiredTemp(); - uint8_t getDesiredPump(); - bool getCyclicEngaged(); -}; - extern CClock Clock; -extern CRTC_Store RTC_Store; #endif // __BTC_TIMERS_H__ diff --git a/src/RTC/RTCStore.cpp b/src/RTC/RTCStore.cpp new file mode 100644 index 0000000..61aa064 --- /dev/null +++ b/src/RTC/RTCStore.cpp @@ -0,0 +1,238 @@ +/* + * This file is part of the "bluetoothheater" distribution + * (https://gitlab.com/mrjones.id.au/bluetoothheater) + * + * Copyright (C) 2018 Ray Jones + * + * 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 . + * + */ + +#include +#include "RTCStore.h" +#include "Clock.h" +#include +//#include "../Utility/helpers.h" +//#include "../Utility/NVStorage.h" +#include "../Utility/DebugPort.h" + + + +// RTC storage, using alarm registers as GP storage +// MAXIMUM OF 7 BYTES +// +// [0..3] float fuelGauge strokes +// [4] uint8_t DesiredTemp (typ. 8-35) +// [5] uint8_t DesiredPump (typ. 8-35) +// [6] uint8_t spare +// +// ____________________________________________________ +// | b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 | +// |---------------|------|-----------------------------| +// Byte[4]: | CyclicEngaged | bit6 | Desired Deg Celcius | +// |---------------|------|-----------------------------| +// Byte[5]: | | | Desired Pump Speed | +// ---------------------------------------------------- + +CRTC_Store::CRTC_Store() +{ + _accessed[0] = false; + _accessed[1] = false; + _accessed[2] = false; + _accessed[3] = false; + _fuelgauge = 0; + _demandDegC = 22; + _demandPump = 22; + _CyclicEngaged = false; + _RunTime = 0; + _GlowTime = 0; +} + +void +CRTC_Store::begin() +{ + if(Clock.lostPower()) { + // RTC lost power - reset internal NV values to defaults + DebugPort.println("CRTC_Store::begin() RTC lost power, re-initialising NV aspect"); + _demandPump = _demandDegC = 22; + _CyclicEngaged = false; + setFuelGauge(0); + setDesiredTemp(_demandDegC); + setDesiredPump(_demandPump); + Clock.resetLostPower(); + } + getFuelGauge(); + getDesiredTemp(); + getDesiredPump(); + getRunTime(); +} + +void +CRTC_Store::setFuelGauge(float val) +{ + _accessed[0] = true; + _fuelgauge = val; + Clock.saveData((uint8_t*)&val, 4, 0); +} + +float +CRTC_Store::getFuelGauge() +{ + if(!_accessed[0]) { + float NVval; + Clock.readData((uint8_t*)&NVval, 4, 0); + _fuelgauge = NVval; + _accessed[0] = true; + DebugPort.printf("RTC_Store - read fuel gauge %.2f\r\n", _fuelgauge); + } + return _fuelgauge; +} + +void +CRTC_Store::setDesiredTemp(uint8_t val) +{ + _demandDegC = val; + _PackAndSaveByte4(); +} + +uint8_t +CRTC_Store::getDesiredTemp() +{ + _ReadAndUnpackByte4(); + return _demandDegC; +} + +bool +CRTC_Store::getCyclicEngaged() +{ + _ReadAndUnpackByte4(); + return _CyclicEngaged; +} + +void +CRTC_Store::setCyclicEngaged(bool active) +{ + _CyclicEngaged = active; + _PackAndSaveByte4(); +} + +void +CRTC_Store::setDesiredPump(uint8_t val) +{ + _demandPump = val; + _PackAndSaveByte5(); +} + +uint8_t +CRTC_Store::getDesiredPump() +{ + _ReadAndUnpackByte5(); + return _demandPump; +} + + +bool +CRTC_Store::incRunTime() +{ + _RunTime++; + _RunTime &= 0x1f; + _PackAndSaveByte6(); + return _RunTime == 0; +} + +bool +CRTC_Store::incGlowTime() +{ + _GlowTime++; + _GlowTime &= 0x07; + _PackAndSaveByte6(); + return _GlowTime == 0; +} + +int +CRTC_Store::getRunTime() +{ + _ReadAndUnpackByte6(); + return _RunTime; +} + +int +CRTC_Store::getGlowTime() +{ + _ReadAndUnpackByte6(); + return _GlowTime; +} + +void +CRTC_Store::_ReadAndUnpackByte4() +{ + if(!_accessed[1]) { + uint8_t NVval = 0; + Clock.readData((uint8_t*)&NVval, 1, 4); + _demandDegC = NVval & 0x3f; + _CyclicEngaged = (NVval & 0x80) != 0; + _bit6 = (NVval & 0x40) != 0; + _accessed[1] = true; + DebugPort.printf("RTC_Store - read byte4: degC=%d, CyclicOn=%d, bit6=%d\r\n", _demandDegC, _CyclicEngaged, _bit6); + } +} + +void +CRTC_Store::_PackAndSaveByte4() +{ + uint8_t NVval = (_CyclicEngaged ? 0x80 : 0x00) + | (_bit6 ? 0x40 : 0x00) + | (_demandDegC & 0x3f); + Clock.saveData((uint8_t*)&NVval, 1, 4); +} + +void +CRTC_Store::_ReadAndUnpackByte5() +{ + if(!_accessed[2]) { + uint8_t NVval = 0; + Clock.readData((uint8_t*)&NVval, 1, 5); + _demandPump = NVval & 0x3f; + _accessed[2] = true; + DebugPort.printf("RTC_Store - read byte5: pump=%d\r\n", _demandPump); + } +} + +void +CRTC_Store::_PackAndSaveByte5() +{ + uint8_t NVval = (_demandPump & 0x3f); + Clock.saveData((uint8_t*)&NVval, 1, 5); +} + +void +CRTC_Store::_PackAndSaveByte6() +{ + uint8_t NVval = ((_GlowTime & 0x07)<<5) | (_RunTime & 0x1f); + Clock.saveData((uint8_t*)&NVval, 1, 6); +} + +void +CRTC_Store::_ReadAndUnpackByte6() +{ + if(!_accessed[3]) { + uint8_t NVval = 0; + Clock.readData((uint8_t*)&NVval, 1, 6); + _GlowTime = (NVval >> 5) & 0x07; + _RunTime = NVval & 0x1f; + _accessed[3] = true; + DebugPort.printf("RTC_Store - read byte6: glow=%d, run=%d\r\n", _GlowTime, _RunTime); + } +} + diff --git a/src/RTC/RTCStore.h b/src/RTC/RTCStore.h new file mode 100644 index 0000000..97f0ae1 --- /dev/null +++ b/src/RTC/RTCStore.h @@ -0,0 +1,62 @@ +/* + * This file is part of the "bluetoothheater" distribution + * (https://gitlab.com/mrjones.id.au/bluetoothheater) + * + * Copyright (C) 2018 Ray Jones + * + * 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 . + * + */ + +#ifndef __BTC_RTC_STORE_H__ +#define __BTC_RTC_STORE_H__ + +#include + + +class CRTC_Store { + bool _accessed[4]; // [0] - bytes 0..3, [1] byte 4, [2] byte 5, [3] byte 6 + float _fuelgauge; + uint8_t _demandDegC; + uint8_t _demandPump; + bool _CyclicEngaged; + bool _bit6; + uint8_t _RunTime; + uint8_t _GlowTime; + void _ReadAndUnpackByte4(); + void _PackAndSaveByte4(); + void _ReadAndUnpackByte5(); + void _PackAndSaveByte5(); + void _ReadAndUnpackByte6(); + void _PackAndSaveByte6(); +public: + CRTC_Store(); + void begin(); + void setFuelGauge(float val); + void setDesiredTemp(uint8_t val); + void setDesiredPump(uint8_t val); + bool incRunTime(); + bool incGlowTime(); + void setCyclicEngaged(bool _CyclicEngaged); + float getFuelGauge(); + uint8_t getDesiredTemp(); + uint8_t getDesiredPump(); + bool getCyclicEngaged(); + int getRunTime(); + int getGlowTime(); +}; + +extern CRTC_Store RTC_Store; + +#endif // __BTC_RTC_STORE_H__ diff --git a/src/Utility/BTC_JSON.cpp b/src/Utility/BTC_JSON.cpp index cdc51af..f854403 100644 --- a/src/Utility/BTC_JSON.cpp +++ b/src/Utility/BTC_JSON.cpp @@ -23,6 +23,7 @@ #include "DebugPort.h" #include "NVStorage.h" #include "../RTC/Clock.h" +#include "../RTC/RTCStore.h" #include "../RTC/BTCDateTime.h" #include "../RTC/Timers.h" #include "../RTC/TimerManager.h" diff --git a/src/Utility/FuelGauge.cpp b/src/Utility/FuelGauge.cpp index 2d985ba..f058d1d 100644 --- a/src/Utility/FuelGauge.cpp +++ b/src/Utility/FuelGauge.cpp @@ -23,7 +23,7 @@ #include "FuelGauge.h" #include "NVStorage.h" #include "DebugPort.h" -#include "../RTC/Clock.h" +#include "../RTC/RTCStore.h" CFuelGauge::CFuelGauge() { diff --git a/src/Utility/HourMeter.cpp b/src/Utility/HourMeter.cpp new file mode 100644 index 0000000..6cd4a86 --- /dev/null +++ b/src/Utility/HourMeter.cpp @@ -0,0 +1,53 @@ +/* + * This file is part of the "bluetoothheater" distribution + * (https://gitlab.com/mrjones.id.au/bluetoothheater) + * + * Copyright (C) 2019 Ray Jones + * + * 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 . + * + */ + +#include "HourMeter.h" +#include "NVStorage.h" +#include "../RTC/RTCStore.h" +#include "../RTC/Clock.h" +#include "../Protocol/Protocol.h" + + +void +CHourMeter::powerOnInit() +{ + _RunTime = 0; + _GlowTime = 0; +} + +void +CHourMeter::monitor(const CProtocol& frame) +{ + Clock.get().secondstime(); +} + +unsigned long +CHourMeter::getRunTime() +{ + return 0; +} + +unsigned long +CHourMeter::getGlowTime() +{ + return 0; +} + diff --git a/src/Utility/HourMeter.h b/src/Utility/HourMeter.h new file mode 100644 index 0000000..c572c06 --- /dev/null +++ b/src/Utility/HourMeter.h @@ -0,0 +1,46 @@ +/* + * This file is part of the "bluetoothheater" distribution + * (https://gitlab.com/mrjones.id.au/bluetoothheater) + * + * Copyright (C) 2019 Ray Jones + * + * 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 . + * + */ + +#include +#include "../RTC/RTCStore.h" +#include "NVStorage.h" + +class CProtocol; + +class CHourMeter { + int& _RunTime; + int& _GlowTime; +public: + CHourMeter(int &runtime, int& glowtime) : + _RunTime(runtime), + _GlowTime(glowtime) + {}; + void associate(int &runtime, int& glowtime) { + _RunTime = runtime; + _GlowTime = glowtime; + }; + void powerOnInit(); + void monitor(const CProtocol& frame); + unsigned long getRunTime(); + unsigned long getGlowTime(); +}; + +extern CHourMeter* pHourMeter; \ No newline at end of file