refactoring
This commit is contained in:
158
application.cpp
158
application.cpp
@ -30,14 +30,6 @@
|
|||||||
#include "configuration.h"
|
#include "configuration.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef WS2811
|
#ifdef WS2811
|
||||||
CRGB leds[NUM_OF_LEDs];
|
CRGB leds[NUM_OF_LEDs];
|
||||||
#endif
|
#endif
|
||||||
@ -53,6 +45,9 @@ typedef struct {
|
|||||||
Adafruit_NeoPixel pixels(NUM_OF_LEDs, PIXEL_PIN, NEO_RGB + NEO_KHZ400);
|
Adafruit_NeoPixel pixels(NUM_OF_LEDs, PIXEL_PIN, NEO_RGB + NEO_KHZ400);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
bool show = false;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static CRGB evaluationColorWord(char* cmd) {
|
static CRGB evaluationColorWord(char* cmd) {
|
||||||
uint8_t red = 0, green = 0, blue = 0;
|
uint8_t red = 0, green = 0, blue = 0;
|
||||||
@ -64,6 +59,10 @@ static CRGB evaluationColorWord(char* cmd) {
|
|||||||
red = 0;
|
red = 0;
|
||||||
green = 0;
|
green = 0;
|
||||||
blue = 0;
|
blue = 0;
|
||||||
|
} else if (! strcmp(cmd, "warmwhite")) {
|
||||||
|
red = 0;
|
||||||
|
green = 0;
|
||||||
|
blue = 0;
|
||||||
} else if (! strcmp(cmd, "red")) {
|
} else if (! strcmp(cmd, "red")) {
|
||||||
red = 255;
|
red = 255;
|
||||||
green = 0;
|
green = 0;
|
||||||
@ -101,6 +100,38 @@ void subscribeApplication() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
static void setColor(int8_t ledNumber, uint8_t red, uint8_t green, uint8_t blue) {
|
||||||
|
if (ledNumber == -1) {
|
||||||
|
for (uint8_t i = 0; i < NUM_OF_LEDs; i++) {
|
||||||
|
#ifdef WS2811
|
||||||
|
leds[i].r = red;
|
||||||
|
leds[i].g = green;
|
||||||
|
leds[i].b = blue;
|
||||||
|
#endif
|
||||||
|
#ifdef PL9823
|
||||||
|
pixels.setPixelColor(i, pixels.Color(red, green, blue));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
show = true;
|
||||||
|
} else {
|
||||||
|
if (ledNumber >= 0 && ledNumber < NUM_OF_LEDs) {
|
||||||
|
|
||||||
|
#ifdef WS2811
|
||||||
|
leds[ledNumber].r = red;
|
||||||
|
leds[ledNumber].g = green;
|
||||||
|
leds[ledNumber].b = blue;
|
||||||
|
#endif
|
||||||
|
#ifdef PL9823
|
||||||
|
pixels.setPixelColor(ledNumber, pixels.Color(red, green, blue));
|
||||||
|
#endif
|
||||||
|
show = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void callbackApplication(char *topic, uint8_t tokenCnt, char **tokens) {
|
void callbackApplication(char *topic, uint8_t tokenCnt, char **tokens) {
|
||||||
if (! strcmp(topic, configBlock.mqttTopicCommand)) {
|
if (! strcmp(topic, configBlock.mqttTopicCommand)) {
|
||||||
int32_t n, b;
|
int32_t n, b;
|
||||||
@ -109,43 +140,15 @@ void callbackApplication(char *topic, uint8_t tokenCnt, char **tokens) {
|
|||||||
case 1:
|
case 1:
|
||||||
// set brightness
|
// set brightness
|
||||||
b = strtol(tokens[0], NULL, 10);
|
b = strtol(tokens[0], NULL, 10);
|
||||||
for (uint8_t i = 0; i < NUM_OF_LEDs; i++) {
|
setColor(-1, b, b, b);
|
||||||
#ifdef WS2811
|
|
||||||
leds[i].r = b;
|
|
||||||
leds[i].g = b;
|
|
||||||
leds[i].b = b;
|
|
||||||
#endif
|
|
||||||
#ifdef PL9823
|
|
||||||
pixels.setPixelColor(i, pixels.Color(b, b, b));
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
#ifdef WS2811
|
|
||||||
FastLED.show();
|
|
||||||
#endif
|
|
||||||
#ifdef PL9823
|
|
||||||
pixels.show();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
// set brightness for one LED
|
// set brightness for one LED
|
||||||
n = strtol(tokens[0], NULL, 10);
|
n = strtol(tokens[0], NULL, 10);
|
||||||
b = strtol(tokens[1], NULL, 10);
|
b = strtol(tokens[1], NULL, 10);
|
||||||
if (n >= 0 && n < NUM_OF_LEDs) {
|
setColor(n, b, b, b);
|
||||||
#ifdef WS2811
|
|
||||||
leds[n].r = b;
|
|
||||||
leds[n].g = b;
|
|
||||||
leds[n].b = b;
|
|
||||||
FastLED.show();
|
|
||||||
#endif
|
|
||||||
#ifdef PL9823
|
|
||||||
pixels.setPixelColor(n, pixels.Color(b, b, b));
|
|
||||||
pixels.show();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (! strcmp(topic, configBlock.mqttTopicColorCommand)) {
|
} else if (! strcmp(topic, configBlock.mqttTopicColorCommand)) {
|
||||||
int32_t n, red, green, blue;
|
int32_t n, red, green, blue;
|
||||||
CRGB colors;
|
CRGB colors;
|
||||||
@ -153,85 +156,34 @@ void callbackApplication(char *topic, uint8_t tokenCnt, char **tokens) {
|
|||||||
case 1:
|
case 1:
|
||||||
// on, off, color word for all LEDs
|
// on, off, color word for all LEDs
|
||||||
colors = evaluationColorWord(tokens[0]);
|
colors = evaluationColorWord(tokens[0]);
|
||||||
for (uint8_t i = 0; i < NUM_OF_LEDs; i++) {
|
setColor(-1, colors.r, colors.g, colors.b);
|
||||||
#ifdef WS2811
|
|
||||||
leds[i] = colors;
|
|
||||||
#endif
|
|
||||||
#ifdef PL9823
|
|
||||||
pixels.setPixelColor(i, pixels.Color(colors.r, colors.g, colors.b));
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
#ifdef WS2811
|
|
||||||
FastLED.show();
|
|
||||||
#endif
|
|
||||||
#ifdef PL9823
|
|
||||||
pixels.show();
|
|
||||||
#endif
|
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
// token0 = LED number, token1 = color
|
// token0 = LED number, token1 = color
|
||||||
n = strtol(tokens[0], NULL, 10);
|
n = strtol(tokens[0], NULL, 10);
|
||||||
if (n >= 0 && n < NUM_OF_LEDs) {
|
colors = evaluationColorWord(tokens[1]);
|
||||||
colors = evaluationColorWord(tokens[1]);
|
setColor(n, colors.r, colors.g, colors.b);
|
||||||
#ifdef WS2811
|
|
||||||
leds[n] = colors;
|
|
||||||
FastLED.show();
|
|
||||||
#endif
|
|
||||||
#ifdef PL9823
|
|
||||||
pixels.setPixelColor(n, pixels.Color(colors.r, colors.g, colors.b));
|
|
||||||
pixels.show();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
// token0 = red, token1 = green, token2 = blue
|
// token0 = red, token1 = green, token2 = blue
|
||||||
red = strtol(tokens[0], NULL, 10);
|
red = strtol(tokens[0], NULL, 10);
|
||||||
green = strtol(tokens[1], NULL, 10);
|
green = strtol(tokens[1], NULL, 10);
|
||||||
blue = strtol(tokens[2], NULL, 10);
|
blue = strtol(tokens[2], NULL, 10);
|
||||||
for (uint8_t i = 0; i < NUM_OF_LEDs; i++) {
|
setColor(-1, red, green, blue);
|
||||||
#ifdef WS2811
|
|
||||||
leds[i].r = red;
|
|
||||||
leds[i].g = green;
|
|
||||||
leds[i].b = blue;
|
|
||||||
#endif
|
|
||||||
#ifdef PL9823
|
|
||||||
pixels.setPixelColor(i, pixels.Color(red, green, blue));
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
#ifdef WS2811
|
|
||||||
FastLED.show();
|
|
||||||
#endif
|
|
||||||
#ifdef PL9823
|
|
||||||
pixels.show();
|
|
||||||
#endif
|
|
||||||
break;
|
break;
|
||||||
case 4:
|
case 4:
|
||||||
// token0 = LED number, token1 = red, token2 = green, token3 = blue
|
// token0 = LED number, token1 = red, token2 = green, token3 = blue
|
||||||
n = strtol(tokens[0], NULL, 10);
|
n = strtol(tokens[0], NULL, 10);
|
||||||
if (n >= 0 && n < NUM_OF_LEDs) {
|
red = strtol(tokens[1], NULL, 10);
|
||||||
red = strtol(tokens[1], NULL, 10);
|
green = strtol(tokens[2], NULL, 10);
|
||||||
green = strtol(tokens[2], NULL, 10);
|
blue = strtol(tokens[3], NULL, 10);
|
||||||
blue = strtol(tokens[3], NULL, 10);
|
setColor(n, red, green, blue);
|
||||||
#ifdef WS2811
|
|
||||||
leds[n].r = red;
|
|
||||||
leds[n].g = green;
|
|
||||||
leds[n].b = blue;
|
|
||||||
FastLED.show();
|
|
||||||
#endif
|
|
||||||
#ifdef PL9823
|
|
||||||
pixels.setPixelColor(n, pixels.Color(red, green, blue));
|
|
||||||
pixels.show();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void setupApplication() {
|
void setupApplication() {
|
||||||
mqttSetup();
|
mqttSetup();
|
||||||
|
|
||||||
@ -251,6 +203,18 @@ void setupApplication() {
|
|||||||
|
|
||||||
void loopApplication() {
|
void loopApplication() {
|
||||||
mqttLoop();
|
mqttLoop();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (show) {
|
||||||
|
show = false;
|
||||||
|
#ifdef WS2811
|
||||||
|
FastLED.show();
|
||||||
|
#endif
|
||||||
|
#ifdef PL9823
|
||||||
|
pixels.show();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user