aboutsummaryrefslogtreecommitdiff
path: root/test_fw/src
diff options
context:
space:
mode:
Diffstat (limited to 'test_fw/src')
-rw-r--r--test_fw/src/main.cpp103
-rw-r--r--test_fw/src/veml.cpp70
2 files changed, 173 insertions, 0 deletions
diff --git a/test_fw/src/main.cpp b/test_fw/src/main.cpp
new file mode 100644
index 0000000..c6d3834
--- /dev/null
+++ b/test_fw/src/main.cpp
@@ -0,0 +1,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);
+} \ No newline at end of file
diff --git a/test_fw/src/veml.cpp b/test_fw/src/veml.cpp
new file mode 100644
index 0000000..020a380
--- /dev/null
+++ b/test_fw/src/veml.cpp
@@ -0,0 +1,70 @@
+#include "veml.h"
+
+constexpr float BASE_LUX_PER_LSB = 0.0042f;
+
+using namespace ocularium::veml;
+
+
+uint16_t Config::to_reg() const
+{
+ uint16_t result = 0;
+
+ result |= shutdown ? 1 : 0;
+ result |= interrupt ? 1 : 0;
+ result |= static_cast<uint8_t>(integration) << 6;
+ result |= static_cast<uint8_t>(gain) << 11;
+
+ return result;
+}
+
+bool VEML::init(const Config& config) {
+ if (chipid() != CHIPID) return false;
+
+ set_config(config);
+ write_reg(Reg::POWER, 0);
+
+ return true;
+}
+
+uint16_t VEML::chipid() const
+{
+ return read_reg(Reg::CHIPID);
+}
+
+float VEML::lux() const
+{
+ const auto val = read_reg(Reg::LUX) * config.gain_factor();
+
+ return static_cast<float>(val) * BASE_LUX_PER_LSB;
+}
+
+void VEML::set_config(const Config& config)
+{
+ this->config = config;
+ write_reg(Reg::CONFIG, config.to_reg());
+}
+
+void VEML::write_reg(const Reg reg, const uint16_t val) const
+{
+ wire.beginTransmission(ADDR);
+ wire.write(static_cast<uint8_t>(reg));
+ wire.write(val & 0xff);
+ wire.write(val >> 8);
+ wire.endTransmission();
+}
+
+uint16_t VEML::read_reg(const Reg reg) const
+{
+ wire.flush();
+
+ wire.beginTransmission(ADDR);
+ wire.write(static_cast<uint8_t>(reg));
+ wire.endTransmission(false);
+ wire.requestFrom(ADDR, 2);
+ wire.endTransmission(true);
+
+ auto result = static_cast<uint16_t>(wire.read());
+ result |= wire.read() << 8;
+
+ return result;
+}