-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.cpp
181 lines (148 loc) · 3.81 KB
/
log.cpp
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
#include "log.hpp"
#include "config.hpp"
#include "print.hpp"
#include <Arduino.h>
#include <SD.h>
#include <SPI.h>
#include <stddef.h>
#include <string.h>
namespace cvslpr {
// Preamble is used to indicate the format of the log
const char LOG_PREAMBLE[] = "Log_v2";
const char LOG_FILENAME[] = "log";
constexpr inline uint32_t ENDIANNESS_BYTES =
(uint32_t(78) << 24) | (uint32_t(185) << 16) | (uint32_t(219) << 8) |
(uint32_t(110));
// TODO: Add other sensor readouts here
const char FormattedLogEntry::HEADER[] = "datetime,temperature\n";
const int FormattedLogEntry::TEMP_WIDTH = 6;
const int FormattedLogEntry::PRECISION = 2;
FormattedLogEntry::FormattedLogEntry(const LogEntry& entry)
{
// TODO: Write other sensor readouts here
char temp_buf[TEMP_WIDTH];
snprintf(data,
sizeof(data),
"%s,%s\n",
format_time(entry.timestamp).time,
dtostrf(entry.temperature, 1, PRECISION, (char*)temp_buf));
size = strlen(data);
}
struct LogMeta
{
char preamble[sizeof(LOG_PREAMBLE)];
uint32_t endianness_bytes{ ENDIANNESS_BYTES };
uint32_t int_size{ sizeof(int) };
uint32_t double_size{ sizeof(int) };
LogMeta() { memcpy(preamble, LOG_PREAMBLE, sizeof(LOG_PREAMBLE)); }
bool operator==(const LogMeta& other) const
{
return !memcmp(this, &other, sizeof(LogMeta));
}
operator bool() const { return int_size != 0; }
} __attribute__((packed)) DEFAULT_META;
static File logfile;
[[nodiscard]] static bool
init_sd()
{
if (SD.begin(SD_CS_PIN)) {
msg_println(F("SD card module initialized."));
return true;
}
msg_println(F("Failed to initialize SD card module."));
return false;
}
[[nodiscard]] static LogMeta
get_meta(File& logfile)
{
if (logfile.available() < sizeof(LogMeta)) {
msg_println(F("Log file too short."));
return {};
}
LogMeta meta;
logfile.readBytes((char*)(&meta), sizeof(LogMeta));
return meta;
}
File
open_log()
{
File f = SD.open(LOG_FILENAME, FILE_WRITE);
f.seek(0UL);
const auto meta = get_meta(f);
if (!meta) {
msg_println(F("Failed to read log meta."));
return {};
}
if (meta != DEFAULT_META) {
msg_println(F("Loaded meta is different from expected."));
return {};
}
return f;
}
bool
init_log()
{
if (!init_sd()) {
return false;
}
bool found_prev = false;
if (SD.exists(LOG_FILENAME)) {
found_prev = true;
if constexpr (RESUME_PREVIOUS_LOG) {
msg_println(F("Previous log found, appending."));
} else {
msg_println(F("Previous log found, removing."));
SD.remove("log");
}
} else {
msg_println(F("No previous log found, creating a new one."));
}
if constexpr (RESUME_PREVIOUS_LOG) {
if (found_prev) {
logfile = open_log();
if (logfile) {
// Seek to the end of the file
logfile.seek(logfile.size());
return true;
} else {
msg_println(F("Failed to open log file for appending."));
return false;
}
}
}
logfile = SD.open(LOG_FILENAME, FILE_WRITE);
if (logfile) {
msg_println(F("Log file opened for writing."));
logfile.write((byte*)(&DEFAULT_META), sizeof(DEFAULT_META));
logfile.flush();
return true;
}
msg_println(F("Failed to open log file for writing."));
return false;
}
bool
deinit_log()
{
logfile.close();
msg_println(F("Log deinitialized."));
return true;
}
void
log(const SensorsReadout& readout, const DateTime& now)
{
const auto unixtime = now.unixtime();
const LogEntry entry(unixtime, readout.temp);
logfile.write((byte*)(&entry), sizeof(LogEntry));
logfile.flush();
msg_println(F("Measurements written to the SD card."));
}
bool
load_log_entry(File& logfile, LogEntry& entry)
{
if (logfile.available() < sizeof(LogEntry)) {
return false;
}
logfile.readBytes((char*)(&entry), sizeof(LogEntry));
return true;
}
} // namespace cvslpr