aboutsummaryrefslogtreecommitdiff
path: root/arduino/config.cc
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2021-12-04 22:22:47 -0800
committer3gg <3gg@shellblade.net>2021-12-04 22:22:47 -0800
commite62be3b12d3c94d709a77d89f52c31f7c4ac475d (patch)
treebb351f85960443086730f0354f829958b2d03190 /arduino/config.cc
parent48481ba9032689e1bceba05c98bc69972e1a2d3b (diff)
Initial commit.
Diffstat (limited to 'arduino/config.cc')
-rwxr-xr-xarduino/config.cc61
1 files changed, 61 insertions, 0 deletions
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 @@
1#include "config.h"
2
3#include <Arduino.h>
4#include <EEPROM.h>
5
6#define ROM_INITIALISED 17
7
8#define DEFAULT_SIGNALS_PER_RPM 133
9#define DEFAULT_START_DELAY_MILLIS 1000
10#define DEFAULT_RPM_COUNT 5000
11
12static Config config;
13
14int writeLong (int address, unsigned long val)
15{
16 const char* p = (char*) &val;
17 int i = 0;
18 for (; i < sizeof(val); ++i, ++p)
19 {
20 EEPROM.write(address + i, *p);
21 }
22 return address + i;
23}
24
25int readLong (int address, unsigned long& val)
26{
27 char* p = (char*) &val;
28 int i = 0;
29 for (; i < sizeof(val); ++i, ++p)
30 {
31 *p = EEPROM.read(address + i);
32 }
33 return address + i;
34}
35
36const Config& readConfig ()
37{
38 byte initialised = EEPROM.read(0);
39 if (initialised != ROM_INITIALISED)
40 {
41 int addr = 1;
42 addr = writeLong(addr, DEFAULT_START_DELAY_MILLIS);
43 addr = writeLong(addr, DEFAULT_RPM_COUNT);
44 addr = writeLong(addr, DEFAULT_SIGNALS_PER_RPM);
45 EEPROM.write(0, ROM_INITIALISED);
46 }
47 int addr = 1;
48 addr = readLong(addr, config.startDelay);
49 addr = readLong(addr, config.rpmCount);
50 addr = readLong(addr, config.signalsPerRPM);
51 return config;
52}
53
54void writeConfig (const Config& config)
55{
56 int addr = 1;
57 addr = writeLong(addr, config.startDelay);
58 addr = writeLong(addr, config.rpmCount);
59 addr = writeLong(addr, config.signalsPerRPM);
60 EEPROM.write(0, ROM_INITIALISED);
61}