diff options
| -rw-r--r-- | test_fw/include/i2c_scan.h | 18 | ||||
| -rw-r--r-- | test_fw/include/log.h | 12 | ||||
| -rw-r--r-- | test_fw/platformio.ini | 1 | ||||
| -rw-r--r-- | test_fw/src/bringup.cpp | 7 | ||||
| -rw-r--r-- | test_fw/src/main.cpp | 82 |
5 files changed, 75 insertions, 45 deletions
diff --git a/test_fw/include/i2c_scan.h b/test_fw/include/i2c_scan.h index 1fc81c1..5588f5c 100644 --- a/test_fw/include/i2c_scan.h +++ b/test_fw/include/i2c_scan.h @@ -3,6 +3,8 @@ #include <Arduino.h> #include <Wire.h> +#include "log.h" + namespace ocularium { inline void scan(TwoWire& wire, bool post_delay = true) { @@ -13,18 +15,18 @@ namespace ocularium { if (error == 0) { - Serial.print("I2C device found at address 0x"); + LOGGER.print("I2C device found at address 0x"); if (address<16) - Serial.print("0"); - Serial.print(address,HEX); - Serial.println(); + LOGGER.print("0"); + LOGGER.print(address,HEX); + LOGGER.println(); } else if (error==4) { - Serial.print("Unknown error at address 0x"); + LOGGER.print("Unknown error at address 0x"); if (address<16) - Serial.print("0"); - Serial.println(address,HEX); + LOGGER.print("0"); + LOGGER.println(address,HEX); } } @@ -34,4 +36,4 @@ namespace ocularium { delay(10); } } -}
\ No newline at end of file +} diff --git a/test_fw/include/log.h b/test_fw/include/log.h new file mode 100644 index 0000000..18efa7a --- /dev/null +++ b/test_fw/include/log.h @@ -0,0 +1,12 @@ +#pragma once + +#define USE_RTT 1 + +#define LOGGER Serial + +#if USE_RTT +#include <RTTStream.h> + +extern RTTStream rtt; +#define LOGGER rtt +#endif diff --git a/test_fw/platformio.ini b/test_fw/platformio.ini index 6b402d8..5921fdc 100644 --- a/test_fw/platformio.ini +++ b/test_fw/platformio.ini @@ -24,6 +24,7 @@ lib_deps = arduino-libraries/SD@^1.3.0 adafruit/Adafruit LSM6DS@^4.7.3 boschsensortec/BSEC Software Library@^1.8.1492 + koendv/RTT Stream@^1.4.1 build_flags = -std=c++2a -DPIO_FRAMEWORK_ARDUINO_ENABLE_EXCEPTIONS diff --git a/test_fw/src/bringup.cpp b/test_fw/src/bringup.cpp index 07ac257..61b1cb0 100644 --- a/test_fw/src/bringup.cpp +++ b/test_fw/src/bringup.cpp @@ -1,5 +1,6 @@ #include "bringup.h" #include "board.h" +#include "log.h" #include <Wire.h> #include <SPI.h> @@ -20,13 +21,13 @@ void bringup::startup_checks(const etl::span<bringup::init_check> &checks) if (succeeded) continue; - Serial.print("checking " + name + "... "); + LOGGER.print("checking " + name + "... "); succeeded = f(); success = success && succeeded; - if (succeeded) Serial.println("ok"); - else Serial.println("bad"); + if (succeeded) LOGGER.println("ok"); + else LOGGER.println("bad"); } if (!success) diff --git a/test_fw/src/main.cpp b/test_fw/src/main.cpp index 0acdc82..8a6b3c4 100644 --- a/test_fw/src/main.cpp +++ b/test_fw/src/main.cpp @@ -16,9 +16,14 @@ #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; @@ -77,10 +82,10 @@ static bringup::init_check STARTUP_CHECKS[] = { if (lsm.begin_SPI(LSM_CS, &LSM_SPI)) { lsm.reset(); - + lsm.setAccelDataRate(LSM6DS_RATE_104_HZ); lsm.setGyroDataRate(LSM6DS_RATE_104_HZ); - + return true; }; @@ -102,31 +107,33 @@ static QueueHandle_t bme_buffer_handle; #endif void setup() { +#if !USE_RTT Serial.begin(115200); - Serial.println("boot"); +#endif + LOGGER.println("boot"); bringup::init_buses(); bringup::init_pins(); #if SCAN_I2C - Serial.println("scanning..."); + LOGGER.println("scanning..."); scan(Wire1); #endif - + bringup::startup_checks(STARTUP_CHECKS); - Serial.println("sensors initialized"); + 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(); delay(100); } @@ -137,10 +144,10 @@ void setup() { { while (true) { - Serial.println("bme run start"); + LOGGER.println("bme run start"); const auto status = bme.run(); - Serial.println("bme run end"); - + LOGGER.println("bme run end"); + if (status) { bme_data data { @@ -151,10 +158,10 @@ void setup() { .co2 = bme.co2Equivalent, .gas = bme.gasPercentage, }; - + xQueueSend(bme_buffer_handle, &data, 0); } - + delay(max(bme.nextCall - millis(), 0)); } } @@ -166,7 +173,7 @@ void loop() { // do not wait xQueueReceiveFromISR(bme_buffer_handle, &bme_data, nullptr); auto& bme_data_source = bme_data; - + #else static uint32_t bme_target_millis = 0; @@ -174,9 +181,9 @@ void loop() { { if (bme_target_millis != 0 && !bme.endReading()) { - Serial.println("bme read error!"); + LOGGER.println("bme read error!"); } - + bme_target_millis = bme.beginReading(); } @@ -188,7 +195,7 @@ void loop() { const bool card_detected = !digitalRead(CARD_DETECT); analogWrite(LED_CAPTURING, card_detected ? 24 : 0); - + static sensors_vec_t accel, gyro; if (lsm.gyroscopeAvailable()) @@ -199,7 +206,7 @@ void loop() { 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 @@ -218,14 +225,14 @@ void loop() { 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); - // Serial.printf("norm: %.2f, compensated: %.2f, pin_val: %d\n", norm, compensated, pin_val); - + // LOGGER.printf("norm: %.2f, compensated: %.2f, pin_val: %d\n", norm, compensated, pin_val); + analogWrite(LED_OTHER, pin_val); } @@ -238,14 +245,14 @@ void loop() { { sd_connected = false; } - + if (millis() - sd_last_connect > sd_period && card_detected) { sd_last_connect = millis(); - + if (!sd.begin(SD_CS)) { - Serial.println("failed connection to sd card"); + LOGGER.println("failed connection to sd card"); sd_connected = false; } else { @@ -263,7 +270,7 @@ void loop() { .name = "lux", .value = lux_value, }, - + { .name = "temp", .value = bme_data_source.temperature, @@ -308,7 +315,7 @@ void loop() { .name = "az", .value = accel.z, }, - + { .name = "gx", .value = gyro.x, @@ -323,27 +330,34 @@ void loop() { }, }; + for (const auto &[name, value] : readings) { - Serial.printf("%s:%f,", name, value); + // 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]; + auto n = snprintf(buf, sizeof(buf), "%s:%f,", name, value); + + LOGGER.printf("%s", buf); } - - Serial.println(); + + LOGGER.println(); #if ENABLE_SD if (sd_connected) { auto data = sd.open("data.csv", O_CREAT | O_WRITE | O_APPEND); - + 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(); } #endif -}
\ No newline at end of file +} |
