-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataLog.hpp
172 lines (158 loc) · 4.87 KB
/
DataLog.hpp
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
#include "DataLog.h"
template <typename LogEntry>
int DataLog<LogEntry>::wipeLog(){
IEEPROM.begin(EEPROM_MAX_SIZE);
for(int i=LOG_START_ADDRESS; i<LOG_END_ADDRESS; i++){
IEEPROM.write(i, 0xFF);
}
IEEPROM.commit();
IEEPROM.end();
return 0;
}
template <typename LogEntry>
int DataLog<LogEntry>::addEntry(LogEntry *entry){
int address;
uint16_t index;
getAddressToOverwrite(address, index);
return writeEntryToEEPROM(address, index, entry);
}
template <typename LogEntry>
int DataLog<LogEntry>::readEntry(int number, LogEntry* entry){
if(abs(number) > maxNumOfEntries()) return -1;
int address;
uint16_t index;
if(number < 0){
getAddressToOverwrite(address, index);
for(int i=number; i<0; i++){
address -= sizeof(LogEntry)+sizeof(uint16_t);
if(address < LOG_START_ADDRESS) address = LOG_START_ADDRESS + ((maxNumOfEntries() -1) * sizeof(LogEntry)+sizeof(uint16_t));
}
}else{
address = getOldestEntryAddress();
for(int i=0; i<number; i++){
address += sizeof(LogEntry)+sizeof(uint16_t);
if(address > (int)(LOG_END_ADDRESS - sizeof(LogEntry)+sizeof(uint16_t))) address = LOG_START_ADDRESS;
}
}
return readEntryFromEEPROM(address, entry);
}
template <typename LogEntry>
int DataLog<LogEntry>::numOfEntries(){
int address;
uint16_t index;
getAddressToOverwrite(address, index);
LogEntry checkentry;
if(readEntryFromEEPROM(address, &checkentry) < 0) return (address - LOG_START_ADDRESS) / (sizeof(LogEntry)+sizeof(uint16_t));
return maxNumOfEntries();
}
template <typename LogEntry>
int DataLog<LogEntry>::maxNumOfEntries(){
return floor((LOG_END_ADDRESS - LOG_START_ADDRESS) / (sizeof(LogEntry)+sizeof(uint16_t)));
}
template <typename LogEntry>
int DataLog<LogEntry>::getHighestIndex(){
int address;
uint16_t index;
getAddressToOverwrite(address, index);
return index-1;
}
template <typename LogEntry>
int DataLog<LogEntry>::writeEntryToEEPROM(int address, uint16_t index, LogEntry* entry){
if(address > (int)(LOG_END_ADDRESS-(sizeof(LogEntry)+sizeof(uint16_t)))) return -1;
IEEPROM.begin(EEPROM_MAX_SIZE);
IEEPROM.put(address, index);
IEEPROM.put(address+sizeof(index), *entry);
IEEPROM.commit();
uint16_t checkindex = 0;
IEEPROM.get(address, checkindex);
IEEPROM.end();
if(checkindex != index) return -1;
return 0;
}
template <typename LogEntry>
int DataLog<LogEntry>::readEntryFromEEPROM(int address, LogEntry* entry){
if(address > (int)(LOG_END_ADDRESS-(sizeof(LogEntry)+sizeof(uint16_t)))) return -1;
IEEPROM.begin(EEPROM_MAX_SIZE);
uint16_t index;
IEEPROM.get(address, index);
IEEPROM.get(address+sizeof(index), *entry);
IEEPROM.end();
if(index == 0xFFFF || index == 0x0 ) return -1;
return index;
}
template <typename LogEntry>
void DataLog<LogEntry>::getAddressToOverwrite(int &address, uint16_t &index){
address = LOG_START_ADDRESS;
IEEPROM.begin(EEPROM_MAX_SIZE);
IEEPROM.get(address, index);
uint16_t lastindex = index;
//find unwritten memory or oldest index
while((index == lastindex+1 || index == lastindex) && index != 0xFFFF && index != 0x0){
lastindex = index;
address += (sizeof(LogEntry)+sizeof(index));
if(address > (int)(LOG_END_ADDRESS - (sizeof(LogEntry)+sizeof(index)))) address = LOG_START_ADDRESS;
IEEPROM.get(address, index);
}
IEEPROM.end();
if(lastindex == 0x0 || lastindex == 0xFFFF) index = 1;
else index = lastindex + 1;
}
template <typename LogEntry>
int DataLog<LogEntry>::getOldestEntryAddress(){
int address;
uint16_t index;
getAddressToOverwrite(address, index);
LogEntry checkentry;
if(readEntryFromEEPROM(address, &checkentry) < 0){
if(index == 1) return -1;
else return LOG_START_ADDRESS;
}
return address;
}
template <typename LogEntry>
void DataLog<LogEntry>::binaryToString(uint8_t status, String& binstatus){
binstatus = "";
for(int i=7; i>=0; i--){
if((status>>i) & 1 ) binstatus += "1";
else binstatus += "0";
}
}
template <typename LogEntry>
void DataLog<LogEntry>::wakeupReasonToString(uint8_t code, String& reason){
switch(code){
#if defined(ESP32) || defined(ESP8266)
case ESP_SLEEP_WAKEUP_UNDEFINED:
reason = "Reset";
break;
case ESP_SLEEP_WAKEUP_EXT0:
reason = "GPIO Single Signal";
break;
case ESP_SLEEP_WAKEUP_EXT1:
reason = "GPIO Multi Signal";
break;
case ESP_SLEEP_WAKEUP_TIMER:
reason = "Timer";
break;
case ESP_SLEEP_WAKEUP_TOUCHPAD:
reason = "Touch";
break;
#endif
default:
reason = "Unknown";
}
}
template <typename LogEntry>
void DataLog<LogEntry>::timeToString(uint64_t timeInS, String& time){
int seconds = timeInS % 60;
int minutes = (timeInS / 60) % 60;
int hours = (timeInS / 3600) % 24;
int days = (timeInS / 86400);
time = String(days);
time += "d:";
time += String(hours);
time += "h:";
time += String(minutes);
time += "m:";
time += String(seconds);
time += "s";
}