diff --git a/co2_sensor.cpp b/co2_sensor.cpp index 15b6ee49b13399a17f38891edad7fcb6d10a268d..af4807e478918a597222cd06149cd7b67f35c46f 100644 --- a/co2_sensor.cpp +++ b/co2_sensor.cpp @@ -70,7 +70,7 @@ namespace sensor { Serial.println(config::auto_calibrate_sensor ? "ON." : "OFF."); } - //NOTE: should time offset be used to reset measurement_timestep? + //NOTE: should time offset be used to adjust measurement_timestep? void checkTimerDeviation() { static int32_t previous_measurement_at = 0; int32_t now = millis(); @@ -124,11 +124,7 @@ namespace sensor { scd30.setForcedRecalibrationFactor(config::co2_calibration_level); Serial.println(F(" Done!")); Serial.println(F("Sensor calibrated.")); - Serial.println(F("Sensor will now restart.")); - LedEffects::showKITTWheel(color::green, 5); - //TODO: Add LEDs off and move to util::reset() - FS_LIB.end(); - ESP.restart(); + resetAmpel(); } void logToSerial() { diff --git a/co2_sensor.h b/co2_sensor.h index 25f79a79628cb895dc7eb2a7b085910d80fd308a..624caa138f2a849d0254782bed8c0a6b35ba0258 100644 --- a/co2_sensor.h +++ b/co2_sensor.h @@ -6,6 +6,7 @@ #include "src/lib/SparkFun_SCD30_Arduino_Library/src/SparkFun_SCD30_Arduino_Library.h" // From: http://librarymanager/All#SparkFun_SCD30 #include "config.h" #include "led_effects.h" +#include "util.h" #include "csv_writer.h" // To close filesystem before restart. #include <Wire.h> diff --git a/led_effects.cpp b/led_effects.cpp index b2f51bfe5935107bce838b1d9f67e63fd1fe764c..71d9b75b4d3dfbbedeffd89fd2530e9e2ce14f7d 100644 --- a/led_effects.cpp +++ b/led_effects.cpp @@ -11,7 +11,7 @@ namespace config { /***************************************************************** * Configuration (calculated from above values) * *****************************************************************/ -namespace config //TODO: Use a class instead. NightMode could then be another state. +namespace config //TODO: Use a class instead. NightMode could then be another state. { const float average_brightness = 0.5 * (config::max_brightness + config::min_brightness); const float brightness_amplitude = 0.5 * (config::max_brightness - config::min_brightness); @@ -38,7 +38,7 @@ namespace counter { uint16_t wheel_offset = 0; uint16_t kitt_offset = 0; uint16_t breathing_offset = 0; -} // namespace counter +} namespace LedEffects { //On-board LED on D4, aka GPIO02 @@ -56,18 +56,23 @@ namespace LedEffects { digitalWrite(ONBOARD_LED_PIN, LOW); } + void LEDsOff() { + pixels.clear(); + pixels.show(); + onBoardLEDOff(); + } + void setupRing() { pixels.begin(); pixels.setBrightness(config::max_brightness); - pixels.clear(); + LEDsOff(); } void toggleNightMode() { config::night_mode = !config::night_mode; if (config::night_mode) { Serial.println(F("NIGHT MODE!")); - pixels.clear(); - pixels.show(); + LEDsOff(); } else { Serial.println(F("DAY MODE!")); } @@ -172,7 +177,7 @@ namespace LedEffects { pixels.show(); counter::breathing_offset += 1; } - delay(co2 > 1600 ? 50 : 100); // faster breathing for higher CO2 values + delay(co2 > 1600 ? 50 : 100); // faster breathing for higher CO2 values } /** diff --git a/led_effects.h b/led_effects.h index a8d5b590dbe2bfb8312d01982114bee7c641854f..31368a2dea2842c4f2d3f884f71a2533d9e9a251 100644 --- a/led_effects.h +++ b/led_effects.h @@ -22,6 +22,7 @@ namespace LedEffects { void onBoardLEDOff(); void onBoardLEDOn(); void toggleNightMode(); + void LEDsOff(); void setupRing(); void redAlert(); diff --git a/mqtt.cpp b/mqtt.cpp index 46955483c1f556cf594ec80e93938d4595ca424c..ff8808b19277b8473738e9a087223180132a5ea2 100644 --- a/mqtt.cpp +++ b/mqtt.cpp @@ -162,8 +162,7 @@ namespace mqtt { } else if (messageString == "local_ip") { sendInfoAboutLocalNetwork(); } else if (messageString == "reset") { - FS_LIB.end(); - ESP.restart(); + resetAmpel(); } else { LedEffects::showKITTWheel(color::red, 1); Serial.println(F("Message not supported. Doing nothing.")); diff --git a/util.cpp b/util.cpp index 31df19dd856e5c3f17b5eb929979660eb27db715..cd7c53bd1423d8ae3056711b9474f4cb1f65c823 100644 --- a/util.cpp +++ b/util.cpp @@ -38,6 +38,14 @@ namespace ntp { } } +void resetAmpel() { + Serial.print("Resetting"); + FS_LIB.end(); + LedEffects::LEDsOff(); + delay(1000); + ESP.restart(); +} + uint32_t max_loop_duration = 0; //FIXME: Remove every instance of Strings, to avoid heap fragmentation problems. (Start: "Free heap space : 17104 bytes") diff --git a/util.h b/util.h index 140ff672a6093985a246d64c7c2f1195aee1a288..4e0b64eebd70fba2af14d65ad867d52501826d61 100644 --- a/util.h +++ b/util.h @@ -3,6 +3,7 @@ #include <Arduino.h> #include "config.h" #include "wifi_util.h" // To get MAC +#include "csv_writer.h" // To close filesystem before reset #include <WiFiUdp.h> //required for NTP #include "src/lib/NTPClient-master/NTPClient.h" // NTP @@ -27,4 +28,6 @@ namespace ntp { extern uint32_t max_loop_duration; const extern String SENSOR_ID; +void resetAmpel(); + #endif