From e62be3b12d3c94d709a77d89f52c31f7c4ac475d Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 4 Dec 2021 22:22:47 -0800 Subject: Initial commit. --- arduino/config.cc | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100755 arduino/config.cc (limited to 'arduino/config.cc') diff --git a/arduino/config.cc b/arduino/config.cc new file mode 100755 index 0000000..a46c663 --- /dev/null +++ b/arduino/config.cc @@ -0,0 +1,61 @@ +#include "config.h" + +#include +#include + +#define ROM_INITIALISED 17 + +#define DEFAULT_SIGNALS_PER_RPM 133 +#define DEFAULT_START_DELAY_MILLIS 1000 +#define DEFAULT_RPM_COUNT 5000 + +static Config config; + +int writeLong (int address, unsigned long val) +{ + const char* p = (char*) &val; + int i = 0; + for (; i < sizeof(val); ++i, ++p) + { + EEPROM.write(address + i, *p); + } + return address + i; +} + +int readLong (int address, unsigned long& val) +{ + char* p = (char*) &val; + int i = 0; + for (; i < sizeof(val); ++i, ++p) + { + *p = EEPROM.read(address + i); + } + return address + i; +} + +const Config& readConfig () +{ + byte initialised = EEPROM.read(0); + if (initialised != ROM_INITIALISED) + { + int addr = 1; + addr = writeLong(addr, DEFAULT_START_DELAY_MILLIS); + addr = writeLong(addr, DEFAULT_RPM_COUNT); + addr = writeLong(addr, DEFAULT_SIGNALS_PER_RPM); + EEPROM.write(0, ROM_INITIALISED); + } + int addr = 1; + addr = readLong(addr, config.startDelay); + addr = readLong(addr, config.rpmCount); + addr = readLong(addr, config.signalsPerRPM); + return config; +} + +void writeConfig (const Config& config) +{ + int addr = 1; + addr = writeLong(addr, config.startDelay); + addr = writeLong(addr, config.rpmCount); + addr = writeLong(addr, config.signalsPerRPM); + EEPROM.write(0, ROM_INITIALISED); +} -- cgit v1.2.3