diff --git a/ampel-firmware/co2_sensor.cpp b/ampel-firmware/co2_sensor.cpp index 7110776f7ab9362e431917afa8ebc84cd8bcb653..0420890c1deedc6f0c31b3b36d2b75bf5f39a9c1 100644 --- a/ampel-firmware/co2_sensor.cpp +++ b/ampel-firmware/co2_sensor.cpp @@ -133,11 +133,12 @@ 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 let background tasks run on the ESP8266. + * 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 00339350cf751165bbd1c1ac46f78ef859e46ed5..feb7f4e612f0fecb0b268f58b7b8fbdee04cb6ff 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 must be defined, and should be between 0 and 255. # define MAX_BRIGHTNESS 255 +// MIN_BRIGHTNESS, if defined, should be between 0 and MAX_BRIGHTNESS - 1 +// If MIN_BRIGHTNESS is not set, or if it is 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 f654e77e581ec88bfce7c3dd8be9b648740e9583..51ba98b96c3e19df4737c0ef77900fa450e6e231 100644 --- a/ampel-firmware/led_effects.cpp +++ b/ampel-firmware/led_effects.cpp @@ -4,16 +4,15 @@ *****************************************************************/ namespace config { const uint8_t max_brightness = MAX_BRIGHTNESS; +#if defined(MIN_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. -{ +#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 + const uint16_t poor_air_quality_ppm = 1600; // Above this threshold, LED breathing effect is faster. + //NOTE: Use a class instead? NightMode could then be another state. bool night_mode = false; } @@ -131,6 +130,17 @@ namespace led_effects { } } + /** + * If enabled, slowly varies the brightness between MAX_BRIGHTNESS & MIN_BRIGHTNESS. + */ + void breathe(int16_t co2) { + static uint8_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 > config::poor_air_quality_ppm ? 6 : 3; // breathing speed. +3 looks like slow human breathing. + } + /** * Fills the whole ring with green, yellow, orange or black, depending on co2 input and CO2_TICKS. */ @@ -144,6 +154,9 @@ namespace led_effects { pixels.setPixelColor(ledId, pixels.ColorHSV(LED_HUES[ledId], 255, brightness)); } pixels.show(); + if (config::brightness_amplitude > 0) { + breathe(co2); + } } void showRainbowWheel(uint16_t duration_ms, uint16_t hue_increment) { @@ -179,18 +192,6 @@ 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. - } - delay(co2 > 1600 ? 50 : 100); // faster breathing for higher CO2 values - } - /** * 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. diff --git a/ampel-firmware/led_effects.h b/ampel-firmware/led_effects.h index d34a9bfb7d42eeca5fd8cf0ca55b037dec604482..3deb809940e61c9d042ddc52614f7b0ec16a08ca 100644 --- a/ampel-firmware/led_effects.h +++ b/ampel-firmware/led_effects.h @@ -25,7 +25,6 @@ namespace led_effects { void setupRing(); void redAlert(); - void breathe(int16_t co2); int countdownToZero(); void showWaitingLED(uint32_t color); void showKITTWheel(uint32_t color, uint16_t duration_s = 2);