aboutsummaryrefslogtreecommitdiff
path: root/arduino/rpmcount/config.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'arduino/rpmcount/config.cpp')
-rwxr-xr-xarduino/rpmcount/config.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/arduino/rpmcount/config.cpp b/arduino/rpmcount/config.cpp
new file mode 100755
index 0000000..a46c663
--- /dev/null
+++ b/arduino/rpmcount/config.cpp
@@ -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}