Files
H1602_WSEN_platformio/src/h1602_eeprom.cpp
2021-02-15 13:42:09 +01:00

199 lines
4.1 KiB
C++

#include <ESP8266WiFi.h>
#include <EEPROM.h>
#include "h1602_eeprom.h"
eepromStruct_t eepromStruct;
//Read eeprom check for empty eeprom
void checkEeprom()
{
EEPROM.get(EEPROMSTRUCT_OFFSET, eepromStruct);
boolean writeCheck = false;
//volatile stops optimization
volatile uint8_t boolvalue = (uint8_t)eepromStruct.apMode;
if (boolvalue >= 3)
{
eepromStruct.apMode = false;
writeCheck = true;
}
if (eepromStruct.password[0] == 255)
{
strcpy(eepromStruct.ssid, "SDNmtr\0");
strcpy(eepromStruct.password, "LockedSDN\0");
writeCheck = true;
}
if (eepromStruct.noderedHost[0] == 255)
{
strcpy(eepromStruct.noderedHost, "http://nodered.sdnsupport.nl:1880\0");
writeCheck = true;
}
if (eepromStruct.noderedUrl[0] == 255)
{
strcpy(eepromStruct.noderedUrl, "Default\0");
writeCheck = true;
}
if (eepromStruct.device[0] == 255)
{
strcpy(eepromStruct.device, "WSEN0000\0");
writeCheck = true;
}
boolvalue = (uint8_t)eepromStruct.ssdp;
if (boolvalue >= 3)
{
eepromStruct.ssdp = true;
writeCheck = true;
}
if (eepromStruct.updateTime == 0xffffffff)
{
eepromStruct.updateTime = 900;
writeCheck = true;
}
if (eepromStruct.channel[1].channel[0] == 255)
{
strcpy(eepromStruct.channel[1].channel, "channel1\0");
eepromStruct.channel[1].enable = false;
writeCheck = true;
}
if (eepromStruct.channel[2].channel[0] == 255)
{
strcpy(eepromStruct.channel[2].channel, "channel2\0");
eepromStruct.channel[2].enable = false;
writeCheck = true;
}
if (eepromStruct.channel[3].channel[0] == 255)
{
strcpy(eepromStruct.channel[3].channel, "channel3\0");
eepromStruct.channel[3].enable = false;
writeCheck = true;
}
if (eepromStruct.channel[4].channel[0] == 255)
{
strcpy(eepromStruct.channel[4].channel, "channel4\0");
eepromStruct.channel[4].enable = false;
writeCheck = true;
}
if (eepromStruct.www_username[0] == 255)
{
strcpy(eepromStruct.www_username, "admin\0");
writeCheck = true;
}
if (eepromStruct.www_password[0] == 255)
{
strcpy(eepromStruct.www_password, "password\0");
writeCheck = true;
}
boolvalue = (uint8_t)eepromStruct.use_prtg;
if (boolvalue >= 3)
{
eepromStruct.use_prtg = false;
writeCheck = true;
}
boolvalue = (uint8_t)eepromStruct.use_nodered;
if (boolvalue >= 3)
{
eepromStruct.use_nodered = false;
writeCheck = true;
}
if (eepromStruct.prtgHost[0] == 255)
{
strcpy(eepromStruct.prtgHost, "prtg.sdnsupport.nl\0");
writeCheck = true;
}
if (eepromStruct.prtgHttpPort == 0xffffffff)
{
eepromStruct.prtgHttpPort = 5050;
writeCheck = true;
}
if (eepromStruct.serialnumber[0] == 255)
{
strcpy(eepromStruct.serialnumber, "XXXXXXX\0");
writeCheck = true;
}
boolvalue = (uint8_t)eepromStruct.isSerialSet;
if (boolvalue >= 3)
{
eepromStruct.isSerialSet = false;
writeCheck = true;
}
boolvalue = (uint8_t)eepromStruct.use_mdns;
if (boolvalue >= 3)
{
eepromStruct.use_mdns = true;
writeCheck = true;
}
boolvalue = (uint8_t)eepromStruct.use_llmnr;
if (boolvalue >= 3)
{
eepromStruct.use_llmnr = true;
writeCheck = true;
}
yield();
if (writeCheck == true)
{
EEPROM.put(EEPROMSTRUCT_OFFSET, eepromStruct);
EEPROM.commit();
}
}
void startEeprom()
{
EEPROM.begin(512);
checkEeprom();
}
void storeApMode(bool state)
{
//AP mode activation
eepromStruct.apMode = state;
EEPROM.put(EEPROMSTRUCT_OFFSET, eepromStruct);
EEPROM.commit();
}
eepromStruct_t *getEeprom()
{
return &eepromStruct;
}
channel_t *getEepromChannel(int channel)
{
return &eepromStruct.channel[channel];
}
bool anyChannelEnabled()
{
if (eepromStruct.channel[1].enable == true || eepromStruct.channel[2].enable == true || eepromStruct.channel[3].enable == true || eepromStruct.channel[4].enable == true)
{
return true;
}
return false;
}
bool saveEeprom()
{
EEPROM.put(EEPROMSTRUCT_OFFSET, eepromStruct);
return EEPROM.commit();
}
void clearEeprom()
{
// write a 0 to all 512 bytes of the EEPROM
for (int i = 0; i < 512; i++)
{
EEPROM.write(i, 0xFF);
yield();
}
EEPROM.commit();
}