aboutsummaryrefslogtreecommitdiff
path: root/test_fw/src/main.cpp
blob: 5206c163939c3697066f77a19348632f039c1a75 (plain) (blame)
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
#include <Arduino.h>
#include <Wire.h>
#include <SPI.h>

#include <Adafruit_BME680.h>
#include <Adafruit_LSM6DS.h>
#include <Adafruit_LSM6DSL.h>

#include <hardware/pio.h>

#include "i2c_scan.h"
#include "veml.h"
#include "board.h"
#include "bringup.h"

using namespace ocularium;

static Adafruit_BME680 bme(&Wire1);
static VEML lux(Wire1);
static Adafruit_LSM6DSL lsm;

#define SCAN_I2C 0

#define SDCARD_SPI SD_SPI
#include <SD.h>
static SDClass sd;

static bringup::init_check STARTUP_CHECKS[] = {
    {
        .name = String("bme680"),
        .f = [] { return bme.begin(BME_ADDR); },
        .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_52_HZ);
                lsm.setGyroDataRate(LSM6DS_RATE_52_HZ);
                
                return true;
            };

            return false;
        },
        .succeeded = false,
    }
};

void setup() {
    Serial.begin(115200);
    Serial.println("boot");

    bringup::init_buses();
    bringup::init_pins();

#if SCAN_I2C
    Serial.println("scanning...");
    scan(Wire1);
#endif
    
    bringup::startup_checks(STARTUP_CHECKS);

    Serial.println("sensors initialized");

    bringup::boot_animation();
    delay(100);

    digitalWrite(LED_CAPTURING, HIGH);
}

void loop() {
    static uint32_t bme_target_millis = 0;

    if (millis() >= bme_target_millis)
    {
        if (bme_target_millis != 0 && !bme.endReading())
        {
            Serial.println("bme read error!");
        }
        
        bme_target_millis = bme.beginReading();
    }

    static sensors_vec_t accel, gyro;

    while (lsm.gyroscopeAvailable())
    {
        lsm.readGyroscope(gyro.x, gyro.y, gyro.z);
    }

    while (lsm.accelerationAvailable())
    {
        lsm.readAcceleration(accel.x, accel.y, accel.z);
    }

    struct
    {
        const char* name;
        float value;
    } readings[] = {
        {
            .name = "lux",
            .value = lux.lux(),
        },
        
        {
            .name = "temp",
            .value = bme.temperature,
        },
        {
            .name = "hum",
            .value = bme.humidity,
        },
        {
            .name = "pres",
            .value = static_cast<float>(bme.pressure),
        },
        {
            .name = "gas",
            .value = bme.humidity,
        },

        {
            .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,
        },
    };

    for (const auto &[name, value] : readings)
    {
        Serial.printf("%s:%f,", name, value);
    }
    
    Serial.println();
}