From 2b62a10329befa98ed24ba169be68ad932d698fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20K=C3=A4ppler?= Date: Mon, 7 Jun 2021 10:47:16 +0200 Subject: [PATCH 1/3] led_effects: Clarify blocking nature of countdown * The 'int' return signature and the comment 'Returns the number of remaining leds' sounds like the function would return its current countdown state. However, it only returns the number of remaining leds after the button was released. Thus change the type to 'bool' and return 'true' after a successful countdown * Do not use 1 as return value for night mode, because it is ambigous. Return 'false' instead. --- ampel-firmware/ampel-firmware.ino | 2 +- ampel-firmware/led_effects.cpp | 13 ++++++++----- ampel-firmware/led_effects.h | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/ampel-firmware/ampel-firmware.ino b/ampel-firmware/ampel-firmware.ino index 39f6b0c..3fdc1d1 100644 --- a/ampel-firmware/ampel-firmware.ino +++ b/ampel-firmware/ampel-firmware.ino @@ -189,7 +189,7 @@ void checkFlashButton() { led_effects::toggleNightMode(); } else { Serial.println(F("Flash has been pressed for a long time. Keep it pressed for calibration.")); - if (led_effects::countdownToZero() < 0) { + if (led_effects::countdownToZero()) { sensor::startCalibrationProcess(); } } diff --git a/ampel-firmware/led_effects.cpp b/ampel-firmware/led_effects.cpp index f0b672c..2577161 100644 --- a/ampel-firmware/led_effects.cpp +++ b/ampel-firmware/led_effects.cpp @@ -211,14 +211,17 @@ namespace led_effects { } /** - * Displays a complete blue circle, and starts removing LEDs one by one. Returns the number of remaining LEDs. - * Can be used for calibration, e.g. when countdown is 0. Does not work in night mode. + * Displays a complete blue circle, and starts removing LEDs one by one. + * Does nothing in night mode and returns false then. Returns true if + * the countdown has finished. Can be used for calibration, e.g. when countdown is 0. + * NOTE: This function is blocking and returns only after the button has + * been released. */ - int countdownToZero() { + bool countdownToZero() { if (config::night_mode) { Serial.println(F("Night mode. Not doing anything.")); delay(1000); // Wait for a while, to avoid coming back to this function too many times when button is pressed. - return 1; + return false; } pixels.fill(color::blue); pixels.show(); @@ -229,6 +232,6 @@ namespace led_effects { Serial.println(countdown); delay(500); } - return countdown; + return true; } } diff --git a/ampel-firmware/led_effects.h b/ampel-firmware/led_effects.h index a910d30..8f04d83 100644 --- a/ampel-firmware/led_effects.h +++ b/ampel-firmware/led_effects.h @@ -26,7 +26,7 @@ namespace led_effects { void setupRing(); void redAlert(); - int countdownToZero(); + bool countdownToZero(); void showWaitingLED(uint32_t color); void showKITTWheel(uint32_t color, uint16_t duration_s = 2); void showRainbowWheel(uint16_t duration_ms = 1000); -- GitLab From 0afa64321b13265bbb00b0e2daf2696d2caf998e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20K=C3=A4ppler?= Date: Mon, 7 Jun 2021 11:17:13 +0200 Subject: [PATCH 2/3] co2_sensor: Move 'state' enum to header file This allows to access the current state from another code files. --- ampel-firmware/co2_sensor.cpp | 15 --------------- ampel-firmware/co2_sensor.h | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/ampel-firmware/co2_sensor.cpp b/ampel-firmware/co2_sensor.cpp index 6e0e01e..6fcf2f8 100644 --- a/ampel-firmware/co2_sensor.cpp +++ b/ampel-firmware/co2_sensor.cpp @@ -30,21 +30,6 @@ namespace sensor { char timestamp[23]; int16_t stable_measurements = 0; - /** - * Define sensor states - * BOOTUP -> initial state, until first >0 ppm values are returned - * READY -> sensor does output valid information (> 0 ppm) and no other condition takes place - * NEEDS_CALIBRATION -> sensor measurements are too low (< 250 ppm) - * PREPARE_CALIBRATION_UNSTABLE -> forced calibration was initiated, last measurements were too far apart - * PREPARE_CALIBRATION_STABLE -> forced calibration was initiated, last measurements were close to each others - */ - enum state { - BOOTUP, - READY, - NEEDS_CALIBRATION, - PREPARE_CALIBRATION_UNSTABLE, - PREPARE_CALIBRATION_STABLE - }; const char *state_names[] = { "BOOTUP", "READY", diff --git a/ampel-firmware/co2_sensor.h b/ampel-firmware/co2_sensor.h index d9eff87..eb2612d 100644 --- a/ampel-firmware/co2_sensor.h +++ b/ampel-firmware/co2_sensor.h @@ -24,6 +24,24 @@ namespace sensor { extern float humidity; extern char timestamp[]; + /** + * Define sensor states + * BOOTUP -> initial state, until first >0 ppm values are returned + * READY -> sensor does output valid information (> 0 ppm) and no other condition takes place + * NEEDS_CALIBRATION -> sensor measurements are too low (< 250 ppm) + * PREPARE_CALIBRATION_UNSTABLE -> forced calibration was initiated, last measurements were too far apart + * PREPARE_CALIBRATION_STABLE -> forced calibration was initiated, last measurements were close to each others + */ + enum state { + BOOTUP, + READY, + NEEDS_CALIBRATION, + PREPARE_CALIBRATION_UNSTABLE, + PREPARE_CALIBRATION_STABLE + }; + + extern state current_state; + void initialize(); bool processData(); void startCalibrationProcess(); -- GitLab From 39347f6e8cae8cf7a826a44038093e16d27e2c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20K=C3=A4ppler?= Date: Mon, 7 Jun 2021 11:18:20 +0200 Subject: [PATCH 3/3] Do not check flash button during calibration It does not make sense to toggle night mode or start another round of countdown during calibration. Closes #4. --- ampel-firmware/ampel-firmware.ino | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ampel-firmware/ampel-firmware.ino b/ampel-firmware/ampel-firmware.ino index 3fdc1d1..bf67370 100644 --- a/ampel-firmware/ampel-firmware.ino +++ b/ampel-firmware/ampel-firmware.ino @@ -141,7 +141,12 @@ void loop() { keepServicesAlive(); // Short press for night mode, Long press for calibration. - checkFlashButton(); + // Inactive during calibration. + if (!(sensor::current_state == sensor::PREPARE_CALIBRATION_STABLE || + sensor::current_state == sensor::PREPARE_CALIBRATION_UNSTABLE)) + { + checkFlashButton(); + } checkSerialInput(); -- GitLab