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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LSM6DS.h>
#include <Adafruit_LSM6DSL.h>
#include <FreeRTOS.h>
#include <task.h>
#include <message_buffer.h>
#include <queue.h>
#include <hardware/pio.h>
#include "i2c_scan.h"
#include "veml.h"
#include "board.h"
#include "bringup.h"
#include "log.h"
#define USE_BSEC 0
#if USE_RTT
RTTStream rtt;
#endif
#if USE_BSEC
#include <bsec.h>
static Bsec bme;
#else
#include <Adafruit_BME680.h>
static Adafruit_BME680 bme(&Wire1);
#endif
using namespace ocularium;
static VEML lux(Wire1);
static Adafruit_LSM6DSL lsm;
#define SCAN_I2C 0
#define ENABLE_SD 1
#define DEBUG_SD 0
#define SDCARD_SPI SD_SPI
#include <SD.h>
static SDClass sd;
static bringup::init_check STARTUP_CHECKS[] = {
{
.name = String("bme680"),
.f = []
{
#if USE_BSEC
bme.begin(BME_ADDR, Wire1);
bsec_virtual_sensor_t sensors[] = {
BSEC_OUTPUT_IAQ,
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
BSEC_OUTPUT_RAW_PRESSURE,
BSEC_OUTPUT_GAS_PERCENTAGE,
BSEC_OUTPUT_CO2_EQUIVALENT,
};
bme.updateSubscription(sensors, ETL_ARRAY_SIZE(sensors), BSEC_SAMPLE_RATE_CONT);
return true;
#else
return bme.begin(BME_ADDR);
#endif
},
.succeeded = false,
},
{
.name = String("veml7700"),
.f = []{ return lux.init(veml::Config {
.gain = veml::AmbientLightGain::Quarter,
}); },
.succeeded = false,
},
{
.name = String("lsm6dsm"),
.f = []
{
if (lsm.begin_SPI(LSM_CS, &LSM_SPI))
{
lsm.reset();
lsm.setAccelDataRate(LSM6DS_RATE_104_HZ);
lsm.setGyroDataRate(LSM6DS_RATE_104_HZ);
return true;
};
return false;
},
.succeeded = false,
},
};
#if USE_BSEC
struct bme_data
{
float temperature, humidity, pressure, iaq, co2, gas;
};
static uint8_t bme_storage[2 * sizeof(bme_data)];
static StaticQueue_t bme_buffer;
static QueueHandle_t bme_buffer_handle;
#endif
void setup() {
#if USE_RTT
rtt.trimDownBufferFull();
rtt.trimUpBufferFull();
#endif
LOGGER.begin(115200);
LOGGER.println("boot");
bringup::init_buses();
bringup::init_pins();
#if SCAN_I2C
LOGGER.println("scanning...");
scan(Wire1);
#endif
bringup::startup_checks(STARTUP_CHECKS);
LOGGER.println("sensors initialized");
#if USE_BSEC
[[noreturn]] void bme_task(void* params);
bme_buffer_handle = xQueueCreateStatic(2, sizeof(bme_data), bme_storage, &bme_buffer);
static StaticTask_t bsec_task_buffer;
static StackType_t bsec_stack[2048];
xTaskCreateStatic(bme_task, "bme", 2048, nullptr, tskIDLE_PRIORITY, bsec_stack, &bsec_task_buffer);
#endif
bringup::boot_animation();
}
#if USE_BSEC
[[noreturn]] void bme_task(void* params)
{
while (true)
{
LOGGER.println("bme run start");
const auto status = bme.run();
LOGGER.println("bme run end");
if (status)
{
bme_data data {
.temperature = bme.temperature,
.humidity = bme.humidity,
.pressure = bme.pressure,
.iaq = bme.iaq,
.co2 = bme.co2Equivalent,
.gas = bme.gasPercentage,
};
xQueueSend(bme_buffer_handle, &data, 0);
}
delay(max(bme.nextCall - millis(), 0));
}
}
#endif
void loop() {
#if USE_BSEC
static bme_data bme_data;
// do not wait
xQueueReceiveFromISR(bme_buffer_handle, &bme_data, nullptr);
auto& bme_data_source = bme_data;
#else
static uint32_t bme_target_millis = 0;
if (millis() >= bme_target_millis)
{
if (bme_target_millis != 0 && !bme.endReading())
{
LOGGER.println("bme read error!");
}
bme_target_millis = bme.beginReading();
}
auto& bme_data_source = bme;
#endif
const auto lux_value = lux.lux();
analogWrite(LED_STORAGE, map(static_cast<int>(lux_value), 200, 1000, 0, 255));
const bool card_detected = !digitalRead(CARD_DETECT);
analogWrite(LED_CAPTURING, card_detected ? 24 : 0);
static sensors_vec_t accel, gyro;
if (lsm.gyroscopeAvailable())
{
lsm.readGyroscope(gyro.x, gyro.y, gyro.z);
}
if (lsm.accelerationAvailable())
{
lsm.readAcceleration(accel.x, accel.y, accel.z);
const auto norm = sqrtf(accel.x * accel.x + accel.y * accel.y + accel.z * accel.z);
// small rolling average to reduce the effects of noise
constexpr auto WINDOW = 10;
static float norm_hist[WINDOW] = {0.0};
for (int i = 0; i < WINDOW - 1; i++)
norm_hist[i] = norm_hist[i + 1];
norm_hist[WINDOW - 1] = norm;
auto norm_avg = 0.f;
for (const float i : norm_hist)
{
norm_avg += i;
}
norm_avg /= static_cast<float>(WINDOW);
constexpr auto NEUTRAL = 1.98f;
constexpr auto RANGE = 8.f;
auto compensated = abs(norm_avg - NEUTRAL) / RANGE;
compensated *= 5; // more sensitivity (heuristic)
const auto pin_val = etl::clamp(static_cast<int>(compensated * 255), 0, 255);
// LOGGER.printf("norm: %.2f, compensated: %.2f, pin_val: %d\n", norm, compensated, pin_val);
analogWrite(LED_OTHER, pin_val);
}
#if ENABLE_SD
constexpr auto sd_period = 50;
static unsigned int sd_last_connect = 0;
static bool sd_connected = false;
if (!card_detected)
{
sd_connected = false;
}
if (millis() - sd_last_connect > sd_period && card_detected && !sd_connected)
{
sd_last_connect = millis();
#if 0
Sd2Card card;
auto result = card.init(3, SD_CS);
if (!result)
{
LOGGER.printf("failed to connect to sd card, code: %d, data: %d", card.errorCode(), card.errorData());
}
#else
if (!sd.begin(SD_CS))
{
LOGGER.println("failed connection to sd card");
sd_connected = false;
} else
{
sd_connected = true;
}
#endif
}
#endif
struct
{
const char* name;
float value;
} readings[] = {
{
.name = "lux",
.value = lux_value,
},
{
.name = "temp",
.value = bme_data_source.temperature,
},
{
.name = "hum",
.value = bme_data_source.humidity,
},
{
.name = "pres",
.value = static_cast<float>(bme_data_source.pressure),
},
#if USE_BSEC
{
.name = "iaq",
.value = bme_data_source.iaq,
},
{
.name = "co2",
.value = bme_data_source.co2,
},
{
.name = "gas",
.value = bme_data_source.gas,
},
#else
{
.name = "gas",
.value = static_cast<float>(bme.gas_resistance),
},
#endif
{
.name = "ax",
.value = accel.x,
},
{
.name = "ay",
.value = accel.y,
},
{
.name = "az",
.value = accel.z,
},
{
.name = "gx",
.value = gyro.x,
},
{
.name = "gy",
.value = gyro.y,
},
{
.name = "gz",
.value = gyro.z,
},
};
#if !DEBUG_SD
if (LOGGER)
{
for (const auto &[name, value] : readings)
{
// This awkwardness is to support the fact that the RTT implementation
// doesn't support formatting floats and instead silently fails -- manually
// format first, then printf the string.
char buf[32];
snprintf(buf, sizeof(buf), "%s:%f,", name, value);
LOGGER.printf("%s", buf);
}
LOGGER.println();
}
#endif
#if ENABLE_SD
if (sd_connected) {
auto data = sd.open("data.csv", O_CREAT | O_WRITE | O_APPEND);
if (data.size() == 0)
{
data.write("uptime_ms,");
for (const auto &[name, _] : readings)
{
data.write(name);
data.write(",");
}
data.write("\n");
}
data.write(String(millis()).c_str());
for (const auto &[name, value] : readings)
{
data.write(String(value).c_str());
data.write(",");
}
data.write("\n");
data.close();
#if DEBUG_SD
LOGGER.println("wrote entry to sd");
#endif
}
#endif
}
|