rgbled/sketch/application.cpp

239 lines
4.2 KiB
C++
Raw Permalink Normal View History

2019-04-05 19:59:07 +02:00
/*
* productionMode.cpp
*
* Created on: Apr 05, 2019
* Author: wn
*/
2019-04-19 23:35:34 +02:00
2019-04-05 19:59:07 +02:00
#include "defines.h"
#include <stdio.h>
#include <stdint.h>
2019-04-06 22:49:23 +02:00
#include <string.h>
2019-04-05 19:59:07 +02:00
#include <stdbool.h>
2019-04-26 13:46:33 +02:00
#include <mqttHandling.h>
2019-04-05 19:59:07 +02:00
2019-04-19 23:48:58 +02:00
#ifdef WS2811
2019-04-05 21:40:26 +02:00
#define FASTLED_ESP8266_NODEMCU_PIN_ORDER
#include <FastLED.h>
2019-04-19 23:35:34 +02:00
#endif
2019-04-19 23:48:58 +02:00
#ifdef PL9823
2019-04-19 23:35:34 +02:00
#include <Adafruit_NeoPixel.h>
#endif
2019-04-05 21:40:26 +02:00
2019-04-05 19:59:07 +02:00
#include "configuration.h"
2019-04-19 23:48:58 +02:00
#ifdef WS2811
2019-04-05 21:40:26 +02:00
CRGB leds[NUM_OF_LEDs];
2019-04-19 23:35:34 +02:00
#endif
2019-04-05 21:40:26 +02:00
2019-04-19 23:48:58 +02:00
#ifdef PL9823
2019-04-19 23:35:34 +02:00
typedef struct {
uint8_t r;
uint8_t g;
uint8_t b;
} CRGB;
// Adafruit_NeoPixel pixels(NUM_OF_LEDs, PIXEL_PIN, NEO_RGB + NEO_KHZ400);
Adafruit_NeoPixel pixels(NUM_OF_LEDs, PIXEL_PIN, NEO_RGB + NEO_KHZ800);
2019-04-19 23:35:34 +02:00
#endif
2019-04-05 21:40:26 +02:00
2019-04-26 15:52:43 +02:00
bool show = false;
2019-04-05 19:59:07 +02:00
2019-04-06 22:49:23 +02:00
static CRGB evaluationColorWord(char* cmd) {
uint8_t red = 0, green = 0, blue = 0;
if ((! strcmp(cmd, "on")) || (! strcmp(cmd, "white"))) {
2019-04-06 22:49:23 +02:00
red = 255;
green = 255;
blue = 255;
} else if (! strcmp(cmd, "off")) {
red = 0;
green = 0;
blue = 0;
2019-04-26 15:52:43 +02:00
} else if (! strcmp(cmd, "warmwhite")) {
red = 0;
green = 0;
blue = 0;
2019-04-06 22:49:23 +02:00
} else if (! strcmp(cmd, "red")) {
red = 255;
green = 0;
blue = 0;
} else if (! strcmp(cmd, "green")) {
red = 0;
green = 255;
blue = 0;
} else if (! strcmp(cmd, "blue")) {
red = 0;
green = 0;
blue = 255;
} else if (! strcmp(cmd, "purple")) {
red = 255;
green = 0;
blue = 255;
} else if (! strcmp(cmd, "yellow")) {
red = 255;
green = 255;
blue = 0;
}
CRGB res;
res.r = red;
res.g = green;
res.b = blue;
return res;
}
2019-04-05 19:59:07 +02:00
2019-04-26 13:46:33 +02:00
void subscribeApplication() {
mqttClient.subscribe(configBlock.mqttTopicColorCommand);
mqttClient.subscribe(configBlock.mqttTopicCommand);
}
2019-04-10 20:24:57 +02:00
2019-04-10 11:15:12 +02:00
2019-04-26 15:52:43 +02:00
static void setColor(int16_t ledNumber, uint8_t x, uint8_t y, uint8_t z) {
uint8_t red, green, blue;
switch (configBlock.colorPattern) {
case 1:
red = y;
green = x;
blue = z;
break;
case 0:
default:
red = x;
green = y;
blue = z;
break;
}
2019-04-26 15:52:43 +02:00
if (ledNumber == -1) {
2019-11-24 00:15:27 +01:00
for (uint16_t i = 0; i < NUM_OF_LEDs; i++) {
2019-04-19 23:48:58 +02:00
#ifdef WS2811
2019-04-26 15:52:43 +02:00
leds[i].r = red;
leds[i].g = green;
leds[i].b = blue;
2019-04-19 23:35:34 +02:00
#endif
2019-04-19 23:48:58 +02:00
#ifdef PL9823
2019-04-26 15:52:43 +02:00
pixels.setPixelColor(i, pixels.Color(red, green, blue));
2019-04-19 23:35:34 +02:00
#endif
2019-04-26 15:52:43 +02:00
}
show = true;
} else {
if (ledNumber >= 0 && ledNumber < NUM_OF_LEDs) {
2019-04-19 23:48:58 +02:00
#ifdef WS2811
2019-04-26 15:52:43 +02:00
leds[ledNumber].r = red;
leds[ledNumber].g = green;
leds[ledNumber].b = blue;
2019-04-19 23:35:34 +02:00
#endif
2019-04-19 23:48:58 +02:00
#ifdef PL9823
2019-04-26 15:52:43 +02:00
pixels.setPixelColor(ledNumber, pixels.Color(red, green, blue));
2019-04-19 23:35:34 +02:00
#endif
2019-04-26 15:52:43 +02:00
show = true;
}
}
}
2019-04-19 23:35:34 +02:00
2019-04-26 15:52:43 +02:00
void callbackApplication(char *topic, uint8_t tokenCnt, char **tokens) {
if (! strcmp(topic, configBlock.mqttTopicCommand)) {
int32_t n, b;
CRGB pseudoColors;
switch (tokenCnt) {
case 1:
// set brightness
b = strtol(tokens[0], NULL, 10);
setColor(-1, b, b, b);
2019-04-26 13:46:33 +02:00
break;
case 2:
// set brightness for one LED
n = strtol(tokens[0], NULL, 10);
b = strtol(tokens[1], NULL, 10);
2019-04-26 15:52:43 +02:00
setColor(n, b, b, b);
2019-04-26 13:46:33 +02:00
break;
}
} else if (! strcmp(topic, configBlock.mqttTopicColorCommand)) {
int32_t n, red, green, blue;
CRGB colors;
switch (tokenCnt) {
case 1:
// on, off, color word for all LEDs
colors = evaluationColorWord(tokens[0]);
2019-04-26 15:52:43 +02:00
setColor(-1, colors.r, colors.g, colors.b);
2019-04-26 13:46:33 +02:00
break;
case 2:
// token0 = LED number, token1 = color
n = strtol(tokens[0], NULL, 10);
2019-04-26 15:52:43 +02:00
colors = evaluationColorWord(tokens[1]);
setColor(n, colors.r, colors.g, colors.b);
2019-04-26 13:46:33 +02:00
break;
case 3:
// token0 = red, token1 = green, token2 = blue
red = strtol(tokens[0], NULL, 10);
green = strtol(tokens[1], NULL, 10);
blue = strtol(tokens[2], NULL, 10);
2019-04-26 15:52:43 +02:00
setColor(-1, red, green, blue);
2019-04-26 13:46:33 +02:00
break;
case 4:
// token0 = LED number, token1 = red, token2 = green, token3 = blue
n = strtol(tokens[0], NULL, 10);
2019-04-26 15:52:43 +02:00
red = strtol(tokens[1], NULL, 10);
green = strtol(tokens[2], NULL, 10);
blue = strtol(tokens[3], NULL, 10);
setColor(n, red, green, blue);
2019-04-26 13:46:33 +02:00
break;
}
}
2019-04-05 19:59:07 +02:00
}
2019-04-25 16:47:53 +02:00
void setupApplication() {
2019-04-26 13:46:33 +02:00
mqttSetup();
2019-04-05 21:40:26 +02:00
2019-04-19 23:48:58 +02:00
#ifdef WS2811
2019-04-26 13:46:33 +02:00
FastLED.addLeds<NEOPIXEL, PIXEL_PIN>(leds, NUM_OF_LEDs);
2019-04-19 23:35:34 +02:00
#endif
2019-04-19 23:48:58 +02:00
#ifdef PL9823
2019-04-19 23:35:34 +02:00
pixels.begin();
2019-04-20 00:18:52 +02:00
2019-11-24 00:15:27 +01:00
for (uint16_t i = 0; i < NUM_OF_LEDs; i++) {
2019-04-26 13:46:33 +02:00
pixels.setPixelColor(i, pixels.Color(0,0,0));
2019-04-20 00:18:52 +02:00
}
2019-04-26 13:46:33 +02:00
pixels.show();
2019-04-19 23:35:34 +02:00
#endif
2019-04-05 19:59:07 +02:00
}
2019-04-25 16:47:53 +02:00
void loopApplication() {
2019-04-26 13:46:33 +02:00
mqttLoop();
2019-04-26 15:52:43 +02:00
if (show) {
show = false;
#ifdef WS2811
FastLED.show();
#endif
#ifdef PL9823
pixels.show();
#endif
}
2019-04-05 19:59:07 +02:00
}