-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPositions.cpp
More file actions
199 lines (156 loc) · 5.06 KB
/
Positions.cpp
File metadata and controls
199 lines (156 loc) · 5.06 KB
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
#include "Config.h"
#include "Positions.h"
#include "Gps.h"
#include "Rtc.h"
#include "Logging.h"
#if BACKUP_ENABLE_SDCARD || BACKUP_ENABLE_NETWORK
#define BACKUPS_ENABLED (BACKUP_ENABLE_SDCARD + BACKUP_ENABLE_NETWORK)
#endif
#if BACKUP_ENABLE_SDCARD
#include "SdPositionsBackup.h"
#endif
#if BACKUP_ENABLE_NETWORK
#include "NetworkPositionsBackup.h"
#endif
#define ENTRY_RESERVED_SIZE 128
#define ENTRIES_ADDR CONFIG_RESERVED_SIZE
namespace positions {
#define CURRENT_LOGGER "positions"
#if BACKUPS_ENABLED > 1
backup::PositionsBackup **_backups;
#elif BACKUPS_ENABLED == 1
backup::PositionsBackup * _backup;
#endif
namespace details {
uint16_t maxEntryIndex = 0;
uint16_t getEntryAddress(uint16_t index) {
if (index > maxEntryIndex) return -1;
return ENTRIES_ADDR + (ENTRY_RESERVED_SIZE * index);
}
}
void setup() {
details::maxEntryIndex = (E24_MAX_ADDRESS(hardware::i2c::eeprom.getSize()) - ENTRIES_ADDR) / ENTRY_RESERVED_SIZE;
#if BACKUPS_ENABLED > 0
backup::PositionsBackup * backup = NULL;
#if BACKUPS_ENABLED > 1
uint8_t backupIdx = 0;
_backups = new backup::PositionsBackup*[BACKUPS_ENABLED];
#endif //BACKUPS_ENABLED > 1
#if BACKUP_ENABLE_SDCARD
backup = new backup::sd::SdPositionsBackup();
backup->setup();
#if BACKUPS_ENABLED > 1
_backups[backupIdx] = backup;
backupIdx++;
#endif //BACKUPS_ENABLED > 1
#endif //BACKUP_ENABLE_SDCARD
#if BACKUP_ENABLE_NETWORK
backup = new backup::net::NetworkPositionsBackup();
backup->setup();
#if BACKUPS_ENABLED > 1
_backups[backupIdx] = backup;
backupIdx++;
#endif //BACKUPS_ENABLED > 1
#endif //BACKUP_ENABLE_NETWORK
#if BACKUPS_ENABLED == 1
_backup = backup;
#endif //BACKUPS_ENABLED == 1
#endif //BACKUPS_ENABLED > 0
}
bool acquire(PositionEntryMetadata &metadata) {
#define CURRENT_LOGGER_FUNCTION "acquire"
NOTICE;
timestamp_t before;
gps::powerOn();
before = rtc::getTime();
SIM808_GPS_STATUS gpsStatus = gps::acquireCurrentPosition(GPS_TOTAL_TIMEOUT_MS);
uint16_t timeToFix = rtc::getTime() - before;
SIM808ChargingStatus battery = hardware::sim808::device.getChargingState();
gps::powerOff();
bool acquired = gpsStatus >= SIM808_GPS_STATUS::FIX; //prety useless wins 14 bytes on the hex size rather than return gpStatus >= ...
NOTICE_FORMAT("Status : %d", gpsStatus);
metadata = {
battery.level,
battery.voltage,
rtc::getTemperature(),
timeToFix,
gpsStatus
};
return acquired;
}
void appendLast(const PositionEntryMetadata &metadata) {
#define CURRENT_LOGGER_FUNCTION "appendLast"
VERBOSE;
uint16_t entryIndex;
uint16_t entryAddress;
PositionEntry entry = { metadata };
strlcpy(entry.position, gps::lastPosition, POSITION_SIZE);
config_t* config = &config::main::value;
entryIndex = config->lastEntry + 1;
entryAddress = details::getEntryAddress(entryIndex);
print(entryIndex, entry);
hardware::i2c::powerOn();
hardware::i2c::eeprom.writeBlock(entryAddress, entry);
NOTICE_MSG("Saved");
config->lastEntry++;
if (config->lastEntry > details::maxEntryIndex) config->lastEntry = 0;
if (config->lastEntry == config->firstEntry) config->firstEntry++;
if (config->firstEntry > details::maxEntryIndex) config->firstEntry = 0;
config::main::save();
hardware::i2c::powerOff();
}
bool get(uint16_t index, PositionEntry &entry) {
#define CURRENT_LOGGER_FUNCTION "get"
VERBOSE;
uint16_t entryAddress = details::getEntryAddress(index);
if (entryAddress == -1) return false;
VERBOSE_FORMAT("Reading entry %d @ %X", index, entryAddress);
hardware::i2c::powerOn();
hardware::i2c::eeprom.readBlock(entryAddress, entry);
hardware::i2c::powerOff();
print(index, entry);
return true;
}
bool moveNext(uint16_t &index) {
if (index == config::main::value.lastEntry) return false;
if (index == details::maxEntryIndex) index = 0; //could use a modulo but easier to understand that way
else index++;
return true;
}
uint16_t count(uint16_t fromIndex) {
config_t *config = &config::main::value;
uint16_t lastEntry = config->lastEntry;
if (lastEntry < fromIndex) { lastEntry += details::maxEntryIndex; }
return lastEntry - fromIndex;
}
void prepareBackup(bool force) {
#if BACKUPS_ENABLED > 1
for (int i = 0; i < BACKUPS_ENABLED; i++) {
_backups[i]->prepare(force);
}
#elif BACKUPS_ENABLED == 1
_backup->prepare(force);
#endif
}
void doBackup(bool force) {
#if BACKUPS_ENABLED > 1
for (int i = 0; i < BACKUPS_ENABLED; i++) {
_backups[i]->backup(force);
}
#elif BACKUPS_ENABLED == 1
_backup->backup(force);
#endif
}
void print(uint16_t index, PositionEntry &entry) {
#define CURRENT_LOGGER_FUNCTION "print"
uint16_t address = details::getEntryAddress(index);
NOTICE_FORMAT("%X : %d,%d,%d,%d,%d,%s",
address,
entry.metadata.batteryLevel,
entry.metadata.batteryVoltage,
entry.metadata.temperature,
static_cast<uint8_t>(entry.metadata.status),
entry.metadata.timeToFix,
entry.position);
}
}