diff --git a/Arduino/BTCDieselHeater/src/Utility/NVCore.cpp b/Arduino/BTCDieselHeater/src/Utility/NVCore.cpp new file mode 100644 index 0000000..f7975a9 --- /dev/null +++ b/Arduino/BTCDieselHeater/src/Utility/NVCore.cpp @@ -0,0 +1,133 @@ +/* + * 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 "NVCore.h" +#include "DebugPort.h" + + +bool +CESP32_NVStorage::validatedLoad(const char* key, char* val, int maxlen, const char* defVal) +{ + char probe[128]; + bool retval = true; + strcpy(probe, "TestPresence"); + int len = preferences.getString(key, probe, 127); + if(len == 0 || strcmp(probe, "TestPresence") == 0) { + preferences.putString(key, defVal); + DebugPort.printf("CESP32HeaterStorage::validatedLoad default installed %s=%s", key, defVal); + retval = false; + } + preferences.getString(key, val, maxlen); + val[maxlen] = 0; // ensure null terminated + return retval; +} + +bool +CESP32_NVStorage::validatedLoad(const char* key, uint8_t& val, int defVal, std::function validator, int min, int max, uint8_t mask) +{ + val = preferences.getUChar(key, defVal); + if(!validator(val & mask, min, max)) { + + DebugPort.printf("CESP32HeaterStorage::validatedLoad invalid read %s=%d", key, val); + DebugPort.printf(" validator(%d,%d) reset to %d\r\n", min, max, defVal); + + val = defVal; + preferences.putUChar(key, val); + return false; + } + return true; +} + +bool +CESP32_NVStorage::validatedLoad(const char* key, int8_t& val, int defVal, std::function validator, int min, int max) +{ + val = preferences.getChar(key, defVal); + if(!validator(val, min, max)) { + + DebugPort.printf("CESP32HeaterStorage::validatedLoad invalid read %s=%d", key, val); + DebugPort.printf(" validator(%d,%d) reset to %d\r\n", min, max, defVal); + + val = defVal; + preferences.putChar(key, val); + return false; + } + return true; +} + +bool +CESP32_NVStorage::validatedLoad(const char* key, uint16_t& val, int defVal, std::function validator, int min, int max) +{ + val = preferences.getUShort(key, defVal); + if(!validator(val, min, max)) { + + DebugPort.printf("CESP32HeaterStorage::validatedLoad invalid read %s=%d", key, val); + DebugPort.printf(" validator(%d,%d) reset to %d\r\n", min, max, defVal); + + val = defVal; + preferences.putUShort(key, val); + return false; + } + return true; +} + +bool +CESP32_NVStorage::validatedLoad(const char* key, long& val, long defVal, std::function validator, long min, long max) +{ + val = preferences.getLong(key, defVal); + if(!validator(val, min, max)) { + + DebugPort.printf("CESP32HeaterStorage::validatedLoad invalid read %s=%ld", key, val); + DebugPort.printf(" validator(%ld,%ld) reset to %ld\r\n", min, max, defVal); + + val = defVal; + preferences.putLong(key, val); + return false; + } + return true; +} + +bool u8inBounds(uint8_t test, uint8_t minLim, uint8_t maxLim) +{ + return (test >= minLim) && (test <= maxLim); +} + +bool s8inBounds(int8_t test, int8_t minLim, int8_t maxLim) +{ + return (test >= minLim) && (test <= maxLim); +} + +bool u8Match2(uint8_t test, uint8_t test1, uint8_t test2) +{ + return (test == test1) || (test == test2); +} + +bool u16inBounds(uint16_t test, uint16_t minLim, uint16_t maxLim) +{ + return (test >= minLim) && (test <= maxLim); +} + +bool s32inBounds(long test, long minLim, long maxLim) +{ + return (test >= minLim) && (test <= maxLim); +} + + diff --git a/Arduino/BTCDieselHeater/src/Utility/NVCore.h b/Arduino/BTCDieselHeater/src/Utility/NVCore.h new file mode 100644 index 0000000..38d0fe6 --- /dev/null +++ b/Arduino/BTCDieselHeater/src/Utility/NVCore.h @@ -0,0 +1,58 @@ +/* + * 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_NV_CORE_H__ +#define __BTC_NV_CORE_H__ + +#include +#include + +bool u8inBounds(uint8_t test, uint8_t minLim, uint8_t maxLim); +bool s8inBounds(int8_t test, int8_t minLim, int8_t maxLim); +bool u8Match2(uint8_t test, uint8_t test1, uint8_t test2); +bool u16inBounds(uint16_t test, uint16_t minLim, uint16_t maxLim); +bool s32inBounds(long test, long minLim, long maxLim); +bool thermoMethodinBounds(uint8_t test, uint8_t minLim, uint8_t maxLim); + +class CNVStorage { + public: + CNVStorage() {}; + virtual ~CNVStorage() {}; + virtual void init() = 0; + virtual void load() = 0; + virtual void save() = 0; + virtual bool valid() = 0; +}; + +class CESP32_NVStorage { +protected: + Preferences preferences; +protected: + bool validatedLoad(const char* key, int8_t& val, int defVal, std::function validator, int min, int max); + bool validatedLoad(const char* key, uint8_t& val, int defVal, std::function validator, int min, int max, uint8_t mask=0xff); + bool validatedLoad(const char* key, uint16_t& val, int defVal, std::function validator, int min, int max); + bool validatedLoad(const char* key, long& val, long defVal, std::function validator, long min, long max); + bool validatedLoad(const char* key, char* val, int maxlen, const char* defVal); +}; + + +#endif // __BTC_NV_CORE_H__ +