-
Notifications
You must be signed in to change notification settings - Fork 13
/
c_ee.h
226 lines (161 loc) · 5.51 KB
/
c_ee.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/***************************************************
Copyright (C) 2016 Steffen Ochs
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
HISTORY: Please refer Github History
****************************************************/
// HELP: https://github.com/bblanchon/ArduinoJson
// Quelle: externer I2C EEPROM
// https://github.com/CombiesGit/I2C_EEPROM/blob/master/inc/eephandler.h
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Extern EEPROM
class M24C02 {
private:
bool _exist;
byte _address;
void (*onwaiting)() = 0; // onWaiting Callback nullptr
// prüft, ob Baustein unter der Adresse beschäftigt ist
bool busy() {
Wire.beginTransmission(_address);
return (bool) Wire.endTransmission();
}
// aktives Warten, Rechenzeit wird abgegeben
void wait() {
while (busy())
if (onwaiting) {
onwaiting(); // Callback aufrufen
}
}
void byteRead(uint8_t address, uint8_t * value) {
wait();
Wire.beginTransmission(_address);
Wire.write(address);
Wire.endTransmission();
Wire.requestFrom((int)_address,1);
* value = Wire.read();
}
void byteWrite(uint8_t address, uint8_t * value) {
wait();
Wire.beginTransmission(_address);
Wire.write(address);
Wire.write(*value);
Wire.endTransmission();
}
public:
bool exist() {
return _exist;
}
boolean begin(void) {
for(byte address = 80; address < 85; address++ ) {
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
if (error == 0) {
_exist = 1;
_address = address;
break;
}
}
return _exist;
}
int getadress() {
if (_exist) return _address;
else return 0;
}
// einzelnes Byte schreiben
void write(uint8_t address,uint8_t value) {
byteWrite(address, &value);
}
// einzelnes Byte lesen
uint8_t read(uint8_t address) {
uint8_t result = 0;
byteRead(address, &result);
return result;
}
// lesen beliebiger Datentypen
template< typename T >
void get(uint8_t address, T &customvar) {
uint8_t *ptr = (uint8_t*) &customvar;
for(int i = 0; i < sizeof(T); i++)
*ptr++ = read(address + i);
}
template< typename T >
void put(uint16_t address,T &customvar) {
uint8_t *ptr = (uint8_t*) &customvar;
for(int i = 0; i < sizeof(T); i++)
write(address + i,*ptr++);
}
};
M24C02 m24;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Initalize EEPROM
void setEE() {
// EEPROM Sector: 0xFB, ab Sector 0xFC liegen System Parameter
IPRINTP("EEPROM: 0x"); // letzter Sector von APP2
DPRINT((((uint32_t)&_SPIFFS_end - 0x40200000) / SPI_FLASH_SEC_SIZE),HEX);
DPRINTP(" (");
DPRINT(EEPROM_SIZE, DEC); // ESP.getFreeSketchSpace()
DPRINTPLN("B)");
EEPROM.begin(EEPROM_SIZE);
// WIFI SETTINGS: 0 - 300
// SYSTEM SETTINGS: 300 - 680
// CHANNEL SETTINGS: 680 - 1180
// THINGSPEAK SETTINGS: 1180 - 1600
// PITMASTER SETTINGS: 1600 - 2300
// PUSH xxx
m24.begin();
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Write to EEPROM
void writeEE(const char* json, int len, int startP) {
IPRINTP("wEE: (");
DPRINT(len);
DPRINTP(") ");
DPRINTLN(json);
for (int i = startP; i < (startP+len); ++i) {
EEPROM.write(i, json[i-startP]);
}
EEPROM.commit();
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Read from EEPROM
void readEE(char *buffer, int len, int startP) {
for (int i = startP; i < (startP+len); ++i) {
buffer[i-startP] = char(EEPROM.read(i));
}
IPRINTP("rEE: ");
DPRINTLN(buffer);
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Clear EEPROM
void clearEE(int len, int startP) {
IPRINTF("cEE: %u to: %u\r\n", startP, startP+len-1);
for (int i = startP; i < (startP+len); ++i) {
EEPROM.write(i, 0);
}
EEPROM.commit();
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Check Memory Sectors
void check_sector() {
// https://github.com/esp8266/Arduino/blob/master/cores/esp8266/Esp.cpp
//uint32_t freeSpaceStart = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1)); // First Sector of OTA
//uint32_t freeSpaceEnd = (uint32_t)&_SPIFFS_start - 0x40200000 - FLASH_SECTOR_SIZE; // Last Sector+1 of OTA
IPRINTP("FV: ");
DPRINTLN(FIRMWAREVERSION);
IPRINTP("SKETCH: 0x01 (");
DPRINT((ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1)/1024);
DPRINTPLN("K)");
//DPRINTP(" (");
//DPRINT((freeSpaceEnd - freeSpaceStart)/1024, DEC); // ESP.getFreeSketchSpace()
//DPRINTPLN("K)");
//DPRINTLN(ESP.getFlashChipRealSize()/1024);
//DPRINTLN(ESP.getFlashChipSize() - 0x4000);
}