From d4a8e93d825a9faf80a15422cad65141eacf5009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20K=C3=A4ppler?= <michael_kaeppler@web.de> Date: Mon, 7 Jun 2021 10:47:16 +0200 Subject: [PATCH] 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..84cd7aa 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 or after every LED has been turned off. */ - 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 countdown < 0; } } 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