aboutsummaryrefslogtreecommitdiff
path: root/test_fw/src/main.cpp
blob: c6d383429a44d8dfa6621b3f9aa284385bd73fb5 (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
#include <Arduino.h>
#include <Wire.h>

#include <Adafruit_BME680.h>

#include <hardware/pio.h>

#include "i2c_scan.h"
#include "veml.h"

constexpr auto SDA = 6;
constexpr auto SCL = 23;

constexpr auto BME_ADDR = 0x76;

static TwoWire wire(SDA, SCL);

static Adafruit_BME680 bme(&wire);
static ocularium::VEML veml(wire);

struct init_check
{
    const String name;
    bool (*f)();
    bool succeeded;
};

static init_check STARTUP_CHECKS[] = {
    {
        .name = String("bme680"),
        .f = [] { return bme.begin(BME_ADDR); },
        .succeeded = false,
    },
    {
        .name = String("veml7700"),
        .f = []{ return veml.init(ocularium::veml::Config {
            .gain = ocularium::veml::AmbientLightGain::Quarter,
        }); },
        .succeeded = false,
    }
};

void startup_checks()
{
    auto success = true;
    
    do
    {
        success = true;
        
        for (auto & check : STARTUP_CHECKS)
        {
            if (check.succeeded)
                continue;

            Serial.print("checking " + check.name + "... ");

            check.succeeded = check.f();
            success = success && check.succeeded;

            if (check.succeeded) Serial.println("ok");
            else Serial.println("bad");
        }

        if (!success) delay(10);
    } while (!success);
}

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

    ocularium::scan(wire);
    startup_checks();

    Serial.println("sensors initialized");
}

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();
    }
    
    Serial.print("lux: " + String(veml.lux()));
    Serial.print(", temp: " + String(bme.temperature));
    Serial.print(", hum: " + String(bme.humidity));
    Serial.print(", pres: " + String(bme.pressure));
    Serial.print(", gas: " + String(bme.gas_resistance));
    Serial.println();

    delay(100);
}