-
Notifications
You must be signed in to change notification settings - Fork 1
/
Memory.h
204 lines (185 loc) · 4.72 KB
/
Memory.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
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
#include <EEPROM.h> //permanent memory
class Memory {
private:
int memAlok;
int memUsed = 180 + 50;
void report(String msg, bool offset = false) {
if(offset) {Serial.println("");}
Serial.println(msg);
}
int findFreeMem() {
//check the occupied memory
int start, end;
String text;
start = 0;
for (int i = 1; i < 50 + 1; i++) {
int one = EEPROM.read(i);
if (one == 255) {
break;
} else {
if (start == 0) {start = i;}
end = i;
text += String(char(one));
}
}
report ("memOccupied: "+ text, true);
report ("start: " + String(start) + "; end: " + String(end));
if (end == 50) {end = 0;}
return end + 1;
}
public:
Memory() {}
void begin() {
EEPROM.begin(memUsed);
report("EEPROM reading", true);
//alokace paměti
memAlok = EEPROM.read(0);
if (memAlok > 50) {
memAlok = 1;
} else if (memAlok == 255) {
memAlok = findFreeMem();
}
report("memAlokFrom: " + String(memAlok));
if (EEPROM.read(0) != memAlok) {
clear();
EEPROM.write(0, memAlok);
EEPROM.commit();
}
}
void clear() {
bool change;
for (int i = memAlok; i <= memUsed; i++) {
if (EEPROM.read(i) != 255) {
EEPROM.write(i, 255);
change = true;
}
}
EEPROM.write(0, 255);
if(change) {EEPROM.commit();}
report("EEPROM cleared");
}
int read(int pos) {
int value = EEPROM.read(pos + memAlok);
if (value == 255) value = 0;
return value;
}
String read(int start, int end) {
start = start + memAlok;
end = end + memAlok;
String text;
for (int i = start; i < end + 1; i++) {
int one = EEPROM.read(i);
if (one == 255) {
break;
} else {
text += String(char(one));
}
}
return text;
}
int readAndCheck(int def, int pos, String text, int min, int max) {
pos += memAlok;
int value = EEPROM.read(pos);
if (value != 255) report(text + ": " + String(value));
if (max > 254) value = round(value * 4.03);
if (value >= min and value <= max) {
return value;
} else {
return def;
}
}
int readAndCheck(int def, int posLow, int posHigh, String text) {
posLow += memAlok;
posHigh += memAlok;
int valLow = EEPROM.read(posLow);
if (valLow == 255) valLow = 0;
int valHigh = EEPROM.read(posHigh);
if (valHigh == 255) valHigh = 0;
int value = (valHigh * 254) + valLow;
report(text + ": " + String(value));
if (valLow == 0 and valHigh == 0) {
return def;
} else {
return value;
}
}
String readAndCheck(String def, String text, int start, int end, bool password) {
String value = read(start, end);
if (value != "") {
if (password == true) {
report(text + ": *");
} else {
report(text + ": " + value);
}
return value;
} else {
return def;
}
}
float readAndCheck(float def, int pos1, String text) {
pos1 += memAlok;
union {
float floats;
unsigned char bytes[4];
} convert;
for (int i=0; i<4; i++) {
convert.bytes[i] = EEPROM.read(pos1 + i);
}
if (isnan(convert.floats)) {
return def;
} else {
report(text + ": " + String(convert.floats));
return convert.floats;
}
}
void write(float value, int pos) {
pos += memAlok;
union {
float floats;
unsigned char bytes[4];
} convert;
convert.floats = value;
for (int i=0; i<4; i++) {
if (EEPROM.read(pos + i) != convert.bytes[i]) {
EEPROM.write(pos + i, convert.bytes[i]);
}
}
EEPROM.commit();
}
void write(int value, int pos) {
pos += memAlok;
if (value > 254) value = round(value / 4.03);
if (EEPROM.read(pos) != value) {
EEPROM.write(pos, value);
EEPROM.commit();
}
}
void write(int value, int posLow, int posHigh) {
posLow += memAlok;
posHigh += memAlok;
int valLow = value % 254;
int valHigh = floor(value / 254.0);
if (EEPROM.read(posLow) != valLow or EEPROM.read(posHigh) != valHigh) {
EEPROM.write(posLow, valLow);
EEPROM.write(posHigh, valHigh);
EEPROM.commit();
}
}
void write(String text, int start, int end) {
start += memAlok;
end += memAlok;
for (int i = 0; i < end - start + 1; i++) {
byte one;
if (i < text.length()) {
one = byte(text.charAt(i));
} else {
one = 255;
}
if (EEPROM.read(i + start) != one) {
EEPROM.write(i + start, one);
}
}
EEPROM.commit();
report("EEPROM_" + String(start) + "-" + String(end) + "(" + String(start - memAlok) + "-" + String(end - memAlok) + "): " + text);
}
};