Commit 4af8ff0e authored by Eric Duminil's avatar Eric Duminil
Browse files

Merge branch 'refactor/breathing' into develop

parents b513c251 52307d98
......@@ -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();
......
......@@ -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
/**
......
......@@ -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.
......
......@@ -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);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment