-
Notifications
You must be signed in to change notification settings - Fork 3
/
eeprom.h
83 lines (68 loc) · 2.1 KB
/
eeprom.h
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
#ifndef eeprom_h
#define eeprom_h
/*
* light-duino v2
* MQTT <-> DMX controller with hw switches, based on ESP8266
* See attached Readme.md for details
*
* This is based on the work of Jorgen (aka Juergen Skrotzky, [email protected]), buy him a beer. ;-)
* Rest if not noted otherwise by Peter Froehlich, [email protected] - Munich Maker Lab e.V. (January 2016)
*
* Published under Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
* You'll find a copy of the licence text in this repo.
*/
// EEPROM methods
void forceEEPROMWrite() {
triggedChange = millis() - maximumWaitBetweenSaves;
dmxChangedStates = 1;
}
void saveDMXState() {
if (dmxChangedStates) {
if (millis() >= triggedChange + maximumWaitBetweenSaves || millis() >= lastChange + minimumWaitBetweenSaves ) {
// calculate checksum
int checksum = 0;
for( int i = 0; i < intMaxChannel; i++ ) {
checksum += dmxChannels[i];
}
checksum = checksum / intMaxChannel;
EEPROM.write(0, checksum);
for( int i = 0; i < intMaxChannel; i++ ) {
EEPROM.write(i + 1, dmxChannels[i]);
}
EEPROM.commit();
dmxChangedStates = 0;
DEBUG_PRINT("Saved channel state, checksum: ");
DEBUG_PRINTLN(checksum);
}
}
}
void recallState() {
int saved_checksum = EEPROM.read(0);
int checksum = 0;
int dmxValue;
for( int i = 0; i < intMaxChannel; i++ ) {
dmxValue = EEPROM.read(i + 1);
channelValue(i, dmxValue);
checksum += dmxValue;
}
checksum = checksum / intMaxChannel; // not perfect, better then nothing.
if (saved_checksum == checksum) {
dmxApplyChanges();
dmxChangedStates = 0;
} else {
DEBUG_PRINT("Checksum verification failed! Saved/Calculated: ");
DEBUG_PRINT(saved_checksum);
DEBUG_PRINT("/");
DEBUG_PRINTLN(checksum);
allChannelsOff();
forceEEPROMWrite();
}
}
// setup functions
void setupEEPROM() {
// init ESP EEPROM
EEPROM.begin(intMaxChannel + 1); // +1 for checksum
DEBUG_PRINTLN("Recalling saved channel state:");
recallState();
}
#endif