Commit 9143422f authored by Eric Duminil's avatar Eric Duminil
Browse files

LED count as a parameter. Not elegant

parent cdfe3b70
Pipeline #5763 passed with stage
in 2 minutes and 4 seconds
#include "led_effects.h"
#include "config.h"
#include "web_config.h"
#include "sensor_console.h"
......@@ -16,58 +15,11 @@ namespace config {
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.
bool display_led = true; // Will be set to false during "night mode".
#if !defined(LED_COUNT)
# define LED_COUNT 12
#endif
const uint16_t led_count = LED_COUNT;
#if LED_COUNT == 12
//NOTE: One value has been prepended, to make calculations easier and avoid out of bounds index.
const uint16_t co2_ticks[led_count + 1] = { 0, 500, 600, 700, 800, 900, 1000, 1200, 1400, 1600, 1800, 2000, 2200 }; // [ppm]
uint16_t co2_ticks[16 + 1] = { 0, 500, 600, 700, 800, 900, 1000 }; // rest will be filled later
// For a given LED, which color should be displayed? First LED will be pure green (hue angle 120°),
// LEDs >= 1600ppm will be pure red (hue angle 0°), LEDs in-between will be yellowish.
const uint16_t led_hues[led_count] = { 21845U, 19114U, 16383U, 13653U, 10922U, 8191U, 5461U, 2730U, 0, 0, 0, 0 }; // [hue angle]
#elif LED_COUNT == 16
const uint16_t co2_ticks[led_count + 1] = {
0,
500,
600,
700,
800,
900,
1000,
1100,
1200,
1300,
1400,
1500,
1600,
1700,
1800,
2000,
2200 }; // [ppm]
const uint16_t led_hues[led_count] = {
21845U,
19859U,
17873U,
15887U,
13901U,
11915U,
9929U,
7943U,
5957U,
3971U,
1985U,
0,
0,
0,
0,
0 }; // [hue angle]
#else
# error "Only 12 and 16 LEDs rings are currently supported."
#endif
uint16_t led_hues[16];
}
#if defined(ESP8266)
......@@ -78,7 +30,8 @@ const int NEOPIXELS_PIN = 5;
const int NEOPIXELS_PIN = 23;
#endif
Adafruit_NeoPixel pixels(config::led_count, NEOPIXELS_PIN, NEO_GRB + NEO_KHZ800);
// config::led_count is not yet known
Adafruit_NeoPixel pixels(0, NEOPIXELS_PIN, NEO_GRB + NEO_KHZ800);
namespace led_effects {
//On-board LED on D4, aka GPIO02
......@@ -119,6 +72,59 @@ namespace led_effects {
}
void setupRing() {
pixels.updateLength(*config::led_count);
if (*config::led_count == 12) {
config::co2_ticks[7] = 1200;
config::co2_ticks[8] = 1400;
config::co2_ticks[9] = 1600;
config::co2_ticks[10] = 1800;
config::co2_ticks[11] = 2000;
config::co2_ticks[12] = 2200;
config::led_hues[0] = 21845U;
config::led_hues[1] = 19114U;
config::led_hues[2] = 16383U;
config::led_hues[3] = 13653U;
config::led_hues[4] = 10922U;
config::led_hues[5] = 8191U;
config::led_hues[6] = 5461U;
config::led_hues[7] = 2730U;
config::led_hues[8] = 0;
config::led_hues[9] = 0;
config::led_hues[10] = 0;
config::led_hues[11] = 0;
} else if (*config::led_count == 16) {
config::co2_ticks[7] = 1100;
config::co2_ticks[8] = 1200;
config::co2_ticks[9] = 1300;
config::co2_ticks[10] = 1400;
config::co2_ticks[11] = 1500;
config::co2_ticks[12] = 1600;
config::co2_ticks[13] = 1700;
config::co2_ticks[14] = 1800;
config::co2_ticks[15] = 2000;
config::co2_ticks[16] = 2200;
config::led_hues[0] = 21845U;
config::led_hues[1] = 19859U;
config::led_hues[2] = 17873U;
config::led_hues[3] = 15887U;
config::led_hues[4] = 13901U;
config::led_hues[5] = 11915U;
config::led_hues[6] = 9929U;
config::led_hues[7] = 7943U;
config::led_hues[8] = 5957U;
config::led_hues[9] = 3971U;
config::led_hues[10] = 1985U;
config::led_hues[11] = 0;
config::led_hues[12] = 0;
config::led_hues[13] = 0;
config::led_hues[14] = 0;
config::led_hues[15] = 0;
} else {
// "Only 12 and 16 LEDs rings are currently supported."
}
pixels.begin();
pixels.setBrightness(*config::max_brightness);
LEDsOff();
......@@ -140,7 +146,7 @@ namespace led_effects {
}
}
//NOTE: basically one iteration of KITT wheel
//NOTE: basically one iteration of KITT wheel
void showWaitingLED(uint32_t color) {
using namespace config;
delay(80);
......@@ -150,19 +156,19 @@ namespace led_effects {
static uint16_t kitt_offset = 0;
pixels.clear();
for (int j = kitt_tail; j >= 0; j--) {
int ledNumber = abs((kitt_offset - j + led_count) % (2 * led_count) - led_count) % led_count; // Triangular function
int ledNumber = abs((kitt_offset - j + *led_count) % (2 * *led_count) - *led_count) % *led_count; // Triangular function
pixels.setPixelColor(ledNumber, color * pixels.gamma8(255 - j * 76) / 255);
}
pixels.show();
kitt_offset++;
}
// Start K.I.T.T. led effect. Red color as default.
// Simulate a moving LED with tail. First LED starts at 0, and moves along a triangular function. The tail follows, with decreasing brightness.
// Takes approximately 1s for each direction.
// Start K.I.T.T. led effect. Red color as default.
// Simulate a moving LED with tail. First LED starts at 0, and moves along a triangular function. The tail follows, with decreasing brightness.
// Takes approximately 1s for each direction.
void showKITTWheel(uint32_t color, uint16_t duration_s) {
pixels.setBrightness(*config::max_brightness);
for (int i = 0; i < duration_s * config::led_count; ++i) {
for (int i = 0; i < duration_s * *config::led_count; ++i) {
showWaitingLED(color);
}
}
......@@ -205,7 +211,7 @@ namespace led_effects {
return;
}
pixels.setBrightness(*config::max_brightness);
for (int ledId = 0; ledId < config::led_count; ++ledId) {
for (int ledId = 0; ledId < *config::led_count; ++ledId) {
uint8_t brightness = getLedBrightness(co2, ledId);
pixels.setPixelColor(ledId, pixels.ColorHSV(config::led_hues[ledId], 255, brightness));
}
......@@ -224,8 +230,8 @@ namespace led_effects {
unsigned long t0 = millis();
pixels.setBrightness(*config::max_brightness);
while (millis() - t0 < duration_ms) {
for (int i = 0; i < config::led_count; i++) {
pixels.setPixelColor(i, pixels.ColorHSV(i * 65535 / config::led_count + wheel_offset));
for (int i = 0; i < *config::led_count; i++) {
pixels.setPixelColor(i, pixels.ColorHSV(i * 65535 / *config::led_count + wheel_offset));
wheel_offset += (pixels.sine8(sine_offset++ / 50) - 127) / 2;
}
pixels.show();
......@@ -265,7 +271,7 @@ namespace led_effects {
pixels.fill(color::blue);
pixels.show();
int countdown;
for (countdown = config::led_count; countdown >= 0 && !digitalRead(0); countdown--) {
for (countdown = *config::led_count; countdown >= 0 && !digitalRead(0); countdown--) {
pixels.setPixelColor(countdown, color::black);
pixels.show();
Serial.println(countdown);
......
......@@ -30,7 +30,7 @@ namespace web_config {
IotWebConf *iotWebConf;
const char config_version[IOTWEBCONF_CONFIG_VERSION_LENGTH] = "a06"; // -- Configuration specific key. The value should be modified if config structure was changed.
const char config_version[IOTWEBCONF_CONFIG_VERSION_LENGTH] = "a07"; // -- Configuration specific key. The value should be modified if config structure was changed.
using namespace iotwebconf;
/**
......@@ -77,7 +77,7 @@ namespace web_config {
IntTParameter<uint8_t> minBrightnessParam =
Builder<IntTParameter<uint8_t>>("min_brightness").label("Min Brightness").defaultValue(MIN_BRIGHTNESS).min(0).max(
255).build();
IntTParameter<uint8_t> ledCountParam = Builder<IntTParameter<uint8_t>>("led_count").label("LED ring").defaultValue(
IntTParameter<uint16_t> ledCountParam = Builder<IntTParameter<uint16_t>>("led_count").label("LED ring").defaultValue(
LED_COUNT).min(12).max(16).step(4).build();
// # define HTTP_USER "co2ampel"
......@@ -282,4 +282,5 @@ namespace config {
uint8_t *max_brightness = &web_config::maxBrightnessParam.value();
uint8_t *min_brightness = &web_config::minBrightnessParam.value();
uint16_t *led_count = &web_config::ledCountParam.value();
}
......@@ -22,6 +22,7 @@ namespace config {
// LED
extern uint8_t *max_brightness;
extern uint8_t *min_brightness;
extern uint16_t *led_count;
}
namespace web_config {
......
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