-
Notifications
You must be signed in to change notification settings - Fork 6
/
EEPROMFunctions.cpp
90 lines (85 loc) · 2.75 KB
/
EEPROMFunctions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* © 2023 Peter Cole
*
* This file is part of EX-Turntable
*
* This 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.
*
* It 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 EX-Turntable. If not, see <https://www.gnu.org/licenses/>.
*/
#include "EEPROMFunctions.h"
#include <EEPROM.h>
#include "IOFunctions.h"
char eepromFlag[4] = {'T', 'T', 'E', 'X'}; // EEPROM location 0 to 3 should contain TTEX if we have stored steps.
const uint8_t eepromVersion = EEPROM_VERSION; // Version of stored EEPROM data to invalidate stored steps if config changes.
// Function to retrieve step count from EEPROM.
// Looks for identifier "TTEX" at 0 to 3.
// Looks for version in 4.
// MSB -> LSB of steps stored in 5 - 8.
long getSteps() {
char data[4];
long eepromSteps;
bool stepsSet = true;
for (uint8_t i = 0; i < 4; i ++) {
data[i] = EEPROM.read(i);
if (data[i] != eepromFlag[i]) {
stepsSet = false;
break;
}
}
uint8_t version = EEPROM.read(4);
if (version != eepromVersion) {
Serial.println(F("EEPROM version outdated, calibration required"));
stepsSet = false;
}
if (stepsSet) {
eepromSteps = ((long)EEPROM.read(5) << 24) + ((long)EEPROM.read(6) << 16) + ((long)EEPROM.read(7) << 8) + (long)EEPROM.read(8);
if (eepromSteps <= sanitySteps) {
if (debug) {
Serial.print(F("DEBUG: TTEX steps defined in EEPROM: "));
Serial.println(eepromSteps);
}
return eepromSteps;
} else {
if (debug) {
Serial.print(F("DEBUG: TTEX steps defined in EEPROM are invalid: "));
Serial.println(eepromSteps);
}
calibrating = true;
return 0;
}
} else {
if (debug) {
Serial.println(F("DEBUG: TTEX steps not defined in EEPROM"));
}
calibrating = true;
return 0;
}
}
// Function to write step count with "TTEX" identifier to EEPROM.
void writeEEPROM(long steps) {
(void) EEPROM;
for (uint8_t i = 0; i < 4; i++) {
EEPROM.write(i, eepromFlag[i]);
}
EEPROM.write(4, eepromVersion);
EEPROM.write(5, (steps >> 24) & 0xFF);
EEPROM.write(6, (steps >> 16) & 0xFF);
EEPROM.write(7, (steps >> 8) & 0xFF);
EEPROM.write(8, steps & 0xFF);
}
// Function to clear step count and identifier from EEPROM.
void clearEEPROM() {
for (uint8_t i = 0; i < 9; i++) {
EEPROM.write(i, 0);
}
}