From 42d9030948c45d38a5e09030b27ad615df8dca78 Mon Sep 17 00:00:00 2001
From: Eric Duminil <eric.duminil@gmail.com>
Date: Thu, 15 Apr 2021 14:03:25 +0200
Subject: [PATCH] LED Effects : small refactor, to allow breathing to be
 disabled (remove MIN_BRIGHTNESS or set it to MAX_BRIGHTNESS).

---
 ampel-firmware/co2_sensor.cpp  |  4 +++-
 ampel-firmware/config.public.h |  5 +++--
 ampel-firmware/led_effects.cpp | 32 +++++++++++++++-----------------
 3 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/ampel-firmware/co2_sensor.cpp b/ampel-firmware/co2_sensor.cpp
index 7110776..1ac7f9b 100644
--- a/ampel-firmware/co2_sensor.cpp
+++ b/ampel-firmware/co2_sensor.cpp
@@ -133,11 +133,13 @@ namespace sensor {
     }
     /**
      * Display data, even if it's "old" (with breathing).
-     * Those effects include a short delay.
+     * A short delay is required in order to not overwhelm controller.
+     * see https://github.com/esp8266/Arduino/issues/3241#issuecomment-301290392
      */
     if (co2 < 2000) {
       led_effects::displayCO2color(co2);
       led_effects::breathe(co2);
+      delay(100);
     } else {
       // >= 2000: entire ring blinks red
       led_effects::redAlert();
diff --git a/ampel-firmware/config.public.h b/ampel-firmware/config.public.h
index 0033935..2fa008f 100644
--- a/ampel-firmware/config.public.h
+++ b/ampel-firmware/config.public.h
@@ -59,9 +59,10 @@
  */
 
 // LED brightness, which can vary between min and max brightness ("LED breathing")
-// max_brightness should be between 0 and 255.
-// min_brightness should be between 0 and max_brightness
+// MAX_BRIGHTNESS should be between 0 and 255.
 #  define MAX_BRIGHTNESS 255
+// MIN_BRIGHTNESS should be between 0 and MAX_BRIGHTNESS - 1
+// If MIN_BRIGHTNESS is not set, or set to MAX_BRIGHTNESS, breathing is disabled.
 #  define MIN_BRIGHTNESS 60
 
 /**
diff --git a/ampel-firmware/led_effects.cpp b/ampel-firmware/led_effects.cpp
index f654e77..4f84b3b 100644
--- a/ampel-firmware/led_effects.cpp
+++ b/ampel-firmware/led_effects.cpp
@@ -4,16 +4,14 @@
  *****************************************************************/
 namespace config {
   const uint8_t max_brightness = MAX_BRIGHTNESS;
-  const uint8_t min_brightness = MIN_BRIGHTNESS;
-  const int kitt_tail = 3; // How many dimmer LEDs follow in K.I.T.T. wheel
-}
-
-/*****************************************************************
- * Configuration  (calculated from above values)                 *
- *****************************************************************/
-namespace config //NOTE: Use a class instead? NightMode could then be another state.
-{
+  #if defined(MIN_BRIGHTNESS)
+    const uint8_t min_brightness = MIN_BRIGHTNESS;
+  #else
+    const uint8_t min_brightness = MAX_BRIGHTNESS;
+  #endif
   const uint8_t brightness_amplitude = config::max_brightness - config::min_brightness;
+  const int kitt_tail = 3; // How many dimmer LEDs follow in K.I.T.T. wheel
+  //NOTE: Use a class instead? NightMode could then be another state.
   bool night_mode = false;
 }
 
@@ -180,15 +178,15 @@ namespace led_effects {
   }
 
   void breathe(int16_t co2) {
-    if (!config::night_mode) {
-      static uint16_t breathing_offset = 0;
-      uint16_t brightness = config::min_brightness
-          + pixels.sine8(breathing_offset) * config::brightness_amplitude / 255;
-      pixels.setBrightness(brightness);
-      pixels.show();
-      breathing_offset += 3; // breathing speed. +3 looks like slow human breathing.
+    if (config::night_mode || config::brightness_amplitude == 0) {
+      return;
     }
-    delay(co2 > 1600 ? 50 : 100); // faster breathing for higher CO2 values
+    static uint16_t breathing_offset = 0;
+    uint16_t brightness = config::min_brightness
+        + pixels.sine8(breathing_offset) * config::brightness_amplitude / 255;
+    pixels.setBrightness(brightness);
+    pixels.show();
+    breathing_offset += co2 > 1600 ? 6 : 3; // breathing speed. +3 looks like slow human breathing.
   }
 
   /**
-- 
GitLab