ESP32_ChinaDieselHeater_Con.../Arduino/BTCDieselHeater/NVStorage.cpp

204 lines
4.2 KiB
C++
Raw Normal View History

2018-11-26 11:58:15 +00:00
/*
* This file is part of the "bluetoothheater" distribution
* (https://gitlab.com/mrjones.id.au/bluetoothheater)
*
* Copyright (C) 2018 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 "Arduino.h"
#include "NVStorage.h"
#include "DebugPort.h"
2018-12-07 11:16:04 +00:00
bool
sNVStore::valid()
{
bool retval = true;
for(int i=0; i<2; i++) {
retval &= (timer[i].start.hour >= 0 && timer[i].start.hour < 24);
retval &= (timer[i].start.min >= 0 && timer[i].start.min < 60);
retval &= (timer[i].stop.hour >= 0 && timer[i].stop.hour < 24);
retval &= (timer[i].stop.min >= 0 && timer[i].stop.min < 60);
retval &= timer[i].repeat < 2;
}
retval &= Heater.Pmin < 100;
retval &= Heater.Pmax < 150;
retval &= Heater.Fmin < 5000;
retval &= Heater.Fmax < 6000;
retval &= Heater.ThermostatMode < 2;
retval &= Heater.setTemperature < 40;
return retval;
}
void
sNVStore::init()
{
for(int i=0; i<2; i++) {
timer[i].start.hour = 0;
timer[i].start.min = 0;
timer[i].stop.hour = 0;
timer[i].stop.min = 0;
timer[i].enabled = 0;
timer[i].repeat = 0;
}
Heater.Pmin = 14;
Heater.Pmax = 45;
Heater.Fmin = 1500;
Heater.Fmax = 4500;
Heater.ThermostatMode = 1;
Heater.setTemperature = 23;
}
CHeaterStorage::CHeaterStorage()
{
2018-12-07 11:16:04 +00:00
_calValues.Heater.Pmin = 14;
_calValues.Heater.Pmax = 40;
_calValues.Heater.Fmin = 1500;
_calValues.Heater.Fmax = 4500;
_calValues.Heater.ThermostatMode = 1;
_calValues.Heater.setTemperature = 22;
}
float
CHeaterStorage::getPmin()
{
2018-12-07 11:16:04 +00:00
return float(_calValues.Heater.Pmin) * 0.1f;
}
float
CHeaterStorage::getPmax()
{
2018-12-07 11:16:04 +00:00
return float(_calValues.Heater.Pmax) * 0.1f;
}
unsigned short
CHeaterStorage::getFmin()
{
2018-12-07 11:16:04 +00:00
return _calValues.Heater.Fmin;
}
unsigned short
CHeaterStorage::getFmax()
{
2018-12-07 11:16:04 +00:00
return _calValues.Heater.Fmax;
}
unsigned char
CHeaterStorage::getDesiredTemperature()
{
2018-12-07 11:16:04 +00:00
return _calValues.Heater.setTemperature;
}
unsigned char
CHeaterStorage::getThermostatMode()
{
2018-12-07 11:16:04 +00:00
return _calValues.Heater.ThermostatMode;
}
void
CHeaterStorage::setPmin(float val)
{
uint8_t cVal = (uint8_t)(val * 10.f + 0.5f);
2018-12-07 11:16:04 +00:00
_calValues.Heater.Pmin = cVal;
}
void
CHeaterStorage::setPmax(float val)
{
uint8_t cVal = (uint8_t)(val * 10.f + 0.5f);
2018-12-07 11:16:04 +00:00
_calValues.Heater.Pmax = cVal;
}
void
CHeaterStorage::setFmin(unsigned short val)
{
2018-12-07 11:16:04 +00:00
_calValues.Heater.Fmin = val;
}
void
CHeaterStorage::setFmax(unsigned short val)
{
2018-12-07 11:16:04 +00:00
_calValues.Heater.Fmax = val;
}
void
CHeaterStorage::setDesiredTemperature(unsigned char val)
{
2018-12-07 11:16:04 +00:00
_calValues.Heater.setTemperature = val;
}
void
CHeaterStorage::setThermostatMode(unsigned char val)
{
2018-12-07 11:16:04 +00:00
_calValues.Heater.ThermostatMode = val;
}
2018-12-07 04:18:24 +00:00
void
CHeaterStorage::getTimerInfo(int idx, sTimer& timerInfo)
{
if(idx >= 0 && idx <=1) {
2018-12-07 11:16:04 +00:00
timerInfo = _calValues.timer[idx];
2018-12-07 04:18:24 +00:00
}
}
void
CHeaterStorage::setTimerInfo(int idx, const sTimer& timerInfo)
{
if(idx >= 0 && idx <=1) {
2018-12-07 11:16:04 +00:00
_calValues.timer[idx] = timerInfo;
2018-12-07 04:18:24 +00:00
}
}
///////////////////////////////////////////////////////////////////////////////////////
// ESP32
//
#ifdef ESP32
CESP32HeaterStorage::CESP32HeaterStorage()
{
}
CESP32HeaterStorage::~CESP32HeaterStorage()
{
preferences.end();
}
void
CESP32HeaterStorage::init()
{
preferences.begin("dieselheater", false);
}
void CESP32HeaterStorage::load()
{
DebugPort.println("Reading from NV storage");
2018-12-07 11:16:04 +00:00
preferences.getBytes("calValues", &_calValues, sizeof(sNVStore));
if(!_calValues.valid()) {
DebugPort.println("Invalid NV storage, initialising");
2018-12-07 11:16:04 +00:00
_calValues.init();
save();
}
}
void CESP32HeaterStorage::save()
{
DebugPort.println("Saving to NV storage");
2018-12-07 11:16:04 +00:00
preferences.putBytes("calValues", &_calValues, sizeof(sNVStore));
}
#endif // ESP32