-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheeprom.ino
213 lines (183 loc) · 4.63 KB
/
eeprom.ino
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
Modul EEPROM for ESP8266
part of Arduino Mega Server project
EEPROM.commit();
*/
#include <EEPROM.h>
#define EEPROM_LENGTH 2048
int eeVolume = EEPROM_LENGTH;
byte eeAddress;
#define EE_ADR_ADDRESS 100
#define EE_ID_ADR 102
#define EE_ID_LEN 15
#define EE_ID_CHECK_ADR 117
#define EE_ID_CHECK_VAL 231
#define EE_NAME_ADR 118
#define EE_NAME_LEN 15
// EEPROM init
void eepromInit() {
initStart_("EEPROM");
if (!eeCheckName()) {
eeSetName(SELF_NAME);
}
eeAddress = eeGetAddress();
if (eeAddress == 0 || eeAddress == 255) {
//eeSetAddress(random(1, 255)); // seed problem
//eeAddress = eeGetAddress();
}
eeCheckId(); // seed problem
#ifdef SERIAL_PRINT
printEeInfo();
#endif
initDone_();
}
// Address
void eeSetAddress(byte addr) {
EEPROM.begin(EEPROM_LENGTH);
EEPROM.write(EE_ADR_ADDRESS, addr);
EEPROM.end();
}
byte eeGetAddress() {
EEPROM.begin(EEPROM_LENGTH);
return EEPROM.read(EE_ADR_ADDRESS);
EEPROM.end();
}
// Name
boolean eeCheckName() {
EEPROM.begin(EEPROM_LENGTH);
for (int i = EE_NAME_ADR; i < EE_NAME_ADR + EE_NAME_LEN; i++) {
if (EEPROM.read(i) != 0 && EEPROM.read(i) != 255) {
EEPROM.end();
return true;
}
}
EEPROM.end();
return false;
}
void eeSetName(String s) {
EEPROM.begin(EEPROM_LENGTH);
for (int i = EE_NAME_ADR; i < EE_NAME_ADR + EE_NAME_LEN; i++) {
if (i - EE_NAME_ADR > s.length()) {
EEPROM.write(i, 0);
} else {
EEPROM.write(i, s[i - EE_NAME_ADR]);
}
}
EEPROM.end();
}
#ifdef SERIAL_PRINT
void eePrintName(String prefix, String suffix) {
EEPROM.begin(EEPROM_LENGTH);
Serial.print(prefix);
for (int i = EE_NAME_ADR; i < EE_NAME_ADR + EE_NAME_LEN; i++) {
Serial.print(char(EEPROM.read(i)));
}
Serial.print(suffix);
EEPROM.end();
}
#endif
String stringName() {
String s = "";
EEPROM.begin(EEPROM_LENGTH);
for (byte i = EE_NAME_ADR; i < EE_NAME_ADR + EE_NAME_LEN; i++) {
if (EEPROM.read(i) == 0 || EEPROM.read(i) == 255) {
break;
}
s += char(EEPROM.read(i));
}
EEPROM.end();
return s;
}
// id
void eeClearId() {
EEPROM.begin(EEPROM_LENGTH);
for (int i = EE_ID_ADR; i < EE_ID_ADR + EE_ID_LEN; i++) {EEPROM.write(i, 0);}
EEPROM.write(EE_ID_CHECK_ADR, 0);
EEPROM.end();
}
void eeSetId() {
EEPROM.begin(EEPROM_LENGTH);
for (int i = EE_ID_ADR; i < EE_ID_ADR + EE_ID_LEN; i++) {
EEPROM.write(i, buffId[i - EE_ID_ADR]);
}
EEPROM.write(EE_ID_CHECK_ADR, EE_ID_CHECK_VAL);
EEPROM.end();
}
boolean eeCorrectId() {
byte current, back;
byte match = 0;
byte increase = 0;
byte decrease = 0;
EEPROM.begin(EEPROM_LENGTH);
for (int i = EE_ID_ADR; i < EE_ID_ADR + EE_ID_LEN; i++) {
current = EEPROM.read(i);
if (!validChar(current)) {EEPROM.end(); return false;}
if (current == back) {match++; if (match > 5) {EEPROM.end(); return false;}}
if (current == back + 1) {increase++; if (increase > 5) {EEPROM.end(); return false;}}
if (current == back - 1) {decrease++; if (decrease > 5) {EEPROM.end(); return false;}}
back = current;
}
if (EEPROM.read(EE_ID_CHECK_ADR) != EE_ID_CHECK_VAL) {EEPROM.end(); return false;}
EEPROM.end();
return true;
}
#ifdef SERIAL_PRINT
void eePrintId(String prefix, String suffix) {
EEPROM.begin(EEPROM_LENGTH);
Serial.print(prefix);
for (int i = EE_ID_ADR; i < EE_ID_ADR + EE_ID_LEN; i++) {
Serial.print(char(EEPROM.read(i)));
}
Serial.print(suffix);
EEPROM.end();
}
#endif
void eeSetBuffId() {
EEPROM.begin(EEPROM_LENGTH);
for (int i = EE_ID_ADR; i < EE_ID_ADR + EE_ID_LEN; i++) {
buffId[i - EE_ID_ADR] = EEPROM.read(i);
}
EEPROM.end();
}
void eeCheckId() {
if (!eeCorrectId()) {
generateId();
eeSetId();
} else {
eeSetBuffId();
}
}
// EEPROM Info
#ifdef SERIAL_PRINT
void printEeInfo() {
eePrintName(F("Name: "), "\n");
//eePrintId(F("ID: "), "\n"); // seed problem
//Serial.print(F("Address: "));
//Serial.print(eeGetAddress()); // seed problem
//Serial.println();
}
#endif
/*
void clearEeprom() {
EEPROM.begin(EEPROM_LENGTH);
for (int i = 0; i < eeVolume; i++) {
EEPROM.write(i, 0);
}
EEPROM.end();
}
#ifdef SERIAL_PRINT
void printEeprom(int start, int finish) {
EEPROM.begin(EEPROM_LENGTH);
for (int i = start; i < finish; i++) {
byte val = EEPROM.read(i);
Serial.print(i);
Serial.print(F("\t"));
Serial.print(val);
Serial.print(F("\t"));
Serial.print(char(val));
Serial.println();
}
EEPROM.end();
}
#endif
*/