Adapt pull request to McLighting

This commit is contained in:
Tobias Blum 2018-01-21 00:10:20 +01:00
parent 84bf3fe255
commit e64b3991b8
3 changed files with 257 additions and 262 deletions

View file

@ -60,16 +60,7 @@ WS2812FX strip = WS2812FX(NUMLEDS, PIN, NEO_GRB + NEO_KHZ800);
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
/// Button ////
#ifdef ENABLE_BUTTON
unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;
byte longKeyPressCountMax = 80; // 80 * 25 = 2000 ms
byte mediumKeyPressCountMin = 20; // 20 * 25 = 500 ms
byte KeyPressCount = 0;
byte prevKeyState = HIGH; // button is active low
boolean buttonState = false;
#endif
// ***************************************************************************
// Load library "ticker" for blinking status led
// ***************************************************************************
@ -584,32 +575,7 @@ void setup() {
String chk = getValue(saved_state_string, '|', 0);
if (chk == "STA") {
DBG_OUTPUT_PORT.printf("Found saved state: %s\n", saved_state_string.c_str());
String str_mode = getValue(saved_state_string, '|', 1);
mode = static_cast<MODE>(str_mode.toInt());
String str_ws2812fx_mode = getValue(saved_state_string, '|', 2);
ws2812fx_mode = str_ws2812fx_mode.toInt();
String str_ws2812fx_speed = getValue(saved_state_string, '|', 3);
ws2812fx_speed = str_ws2812fx_speed.toInt();
String str_brightness = getValue(saved_state_string, '|', 4);
brightness = str_brightness.toInt();
String str_red = getValue(saved_state_string, '|', 5);
main_color.red = str_red.toInt();
String str_green = getValue(saved_state_string, '|', 6);
main_color.green = str_green.toInt();
String str_blue = getValue(saved_state_string, '|', 7);
main_color.blue = str_blue.toInt();
DBG_OUTPUT_PORT.printf("ws2812fx_mode: %d\n", ws2812fx_mode);
DBG_OUTPUT_PORT.printf("ws2812fx_speed: %d\n", ws2812fx_speed);
DBG_OUTPUT_PORT.printf("brightness: %d\n", brightness);
DBG_OUTPUT_PORT.printf("main_color.red: %d\n", main_color.red);
DBG_OUTPUT_PORT.printf("main_color.green: %d\n", main_color.green);
DBG_OUTPUT_PORT.printf("main_color.blue: %d\n", main_color.blue);
strip.setMode(ws2812fx_mode);
strip.setSpeed(convertSpeed(ws2812fx_speed));
strip.setBrightness(brightness);
strip.setColor(main_color.red, main_color.green, main_color.blue);
setModeByStateString(saved_state_string);
}
sprintf(last_state, "STA|%2d|%3d|%3d|%3d|%3d|%3d|%3d", mode, ws2812fx_mode, ws2812fx_speed, brightness, main_color.red, main_color.green, main_color.blue);
#endif

View file

@ -1,15 +1,13 @@
// Neopixel
#define PIN D2 // PIN where neopixel / WS2811 strip is attached
#define NUMLEDS 68 // Number of leds in the strip
#define BUILTIN_LED 2 // ESP-12F has the built in LED on GPIO2, see https://github.com/esp8266/Arduino/issues/2192
#define BUTTON D1 // Input pin for switching the LED strip on / off
#define PIN D1 // PIN where neopixel / WS2811 strip is attached
#define NUMLEDS 7 // Number of leds in the strip
//#define BUILTIN_LED 2 // ESP-12F has the built in LED on GPIO2, see https://github.com/esp8266/Arduino/issues/2192
#define BUTTON D2 // Input pin for switching the LED strip on / off, connect this PIN to ground to trigger button.
const char HOSTNAME[] = "McLighting"; // Friedly hostname
const char HOSTNAME[] = "ESP8266_VORONOI"; // Friedly hostname
#define ENABLE_OTA // If defined, enable Arduino OTA code.
#define ENABLE_MQTT // If defined, enable MQTT client code.
#define ENABLE_BUTTON // If defined, enable button handling code.
// parameters for automatically cycling favorite patterns
@ -25,7 +23,6 @@ uint32_t autoParams[][4] = { // color, speed, mode, duration (seconds)
#define MQTT_MAX_RECONNECT_TRIES 4
int mqtt_reconnect_retries = 0;
char mqtt_intopic[strlen(HOSTNAME) + 4]; // Topic in will be: <HOSTNAME>/in
char mqtt_outtopic[strlen(HOSTNAME) + 5]; // Topic out will be: <HOSTNAME>/out
@ -49,13 +46,13 @@ enum MODE { SET_MODE, HOLD, OFF, ALL, WIPE, RAINBOW, RAINBOWCYCLE, THEATERCHASE,
MODE mode = RAINBOW; // Standard mode that is active when software starts
int ws2812fx_speed = 196; // Global variable for storing the delay between color changes --> smaller == faster
int brightness = 255; // Global variable for storing the brightness (255 == 100%)
int brightness = 196; // Global variable for storing the brightness (255 == 100%)
int ws2812fx_mode = 0; // Helper variable to set WS2812FX modes
bool exit_func = false; // Global helper variable to get out of the color modes when mode changes
bool shouldSaveConfig = true; // For WiFiManger custom config
bool shouldSaveConfig = false; // For WiFiManger custom config
struct ledstate // Data structure to store a state of a single led
{
@ -76,3 +73,18 @@ LEDState main_color = { 255, 0, 0 }; // Store the "main color" of the strip use
int timeout_statechange_save = 5000; // Timeout in ms to wait before state is saved
bool state_save_requested = false; // State has to be saved after timeout
#endif
// Button handling
#ifdef ENABLE_BUTTON
#define BTN_MODE_SHORT "STA| 1| 0|245|196|255|255|255" // Static white
#define BTN_MODE_MEDIUM "STA| 1| 48|245|196|255|102| 0" // Fire flicker
#define BTN_MODE_LONG "STA| 1| 46|253|196|255|102| 0" // Fireworks random
unsigned long keyPrevMillis = 0;
const unsigned long keySampleIntervalMs = 25;
byte longKeyPressCountMax = 80; // 80 * 25 = 2000 ms
byte mediumKeyPressCountMin = 20; // 20 * 25 = 500 ms
byte KeyPressCount = 0;
byte prevKeyState = HIGH; // button is active low
boolean buttonState = false;
#endif

View file

@ -2,8 +2,6 @@
// Request handlers
// ***************************************************************************
////////////////////////////
void getArgs() {
if (server.arg("rgb") != "") {
uint32_t rgb = (uint32_t) strtol(server.arg("rgb").c_str(), NULL, 16);
@ -160,6 +158,35 @@ void handleRangeDifferentColors(uint8_t * mypayload) {
}
}
void setModeByStateString(String saved_state_string) {
String str_mode = getValue(saved_state_string, '|', 1);
mode = static_cast<MODE>(str_mode.toInt());
String str_ws2812fx_mode = getValue(saved_state_string, '|', 2);
ws2812fx_mode = str_ws2812fx_mode.toInt();
String str_ws2812fx_speed = getValue(saved_state_string, '|', 3);
ws2812fx_speed = str_ws2812fx_speed.toInt();
String str_brightness = getValue(saved_state_string, '|', 4);
brightness = str_brightness.toInt();
String str_red = getValue(saved_state_string, '|', 5);
main_color.red = str_red.toInt();
String str_green = getValue(saved_state_string, '|', 6);
main_color.green = str_green.toInt();
String str_blue = getValue(saved_state_string, '|', 7);
main_color.blue = str_blue.toInt();
DBG_OUTPUT_PORT.printf("ws2812fx_mode: %d\n", ws2812fx_mode);
DBG_OUTPUT_PORT.printf("ws2812fx_speed: %d\n", ws2812fx_speed);
DBG_OUTPUT_PORT.printf("brightness: %d\n", brightness);
DBG_OUTPUT_PORT.printf("main_color.red: %d\n", main_color.red);
DBG_OUTPUT_PORT.printf("main_color.green: %d\n", main_color.green);
DBG_OUTPUT_PORT.printf("main_color.blue: %d\n", main_color.blue);
strip.setMode(ws2812fx_mode);
strip.setSpeed(convertSpeed(ws2812fx_speed));
strip.setBrightness(brightness);
strip.setColor(main_color.red, main_color.green, main_color.blue);
}
void handleSetNamedMode(String str_mode) {
exit_func = true;
@ -561,15 +588,15 @@ void checkForRequests() {
}
#endif
/// Button management /////
// ***************************************************************************
// Button management
// ***************************************************************************
#ifdef ENABLE_BUTTON
void shortKeyPress() {
DBG_OUTPUT_PORT.printf("Short button press\n");
if (buttonState == false) {
main_color.red = 255;
main_color.green = 255;
main_color.blue = 255;
strip.setColor(main_color.red, main_color.green, main_color.blue);
mode = HOLD;
setModeByStateString(BTN_MODE_SHORT);
buttonState = true;
} else {
mode = OFF;
@ -577,34 +604,16 @@ void shortKeyPress() {
}
}
// called when button is kept pressed for more than 2 seconds
// called when button is kept pressed for less than 2 seconds
void mediumKeyPress() {
mode = TWINKLERANDOM;
DBG_OUTPUT_PORT.printf("Medium button press\n");
setModeByStateString(BTN_MODE_MEDIUM);
}
// called when button is kept pressed for 2 seconds or more
void longKeyPress() {
//Serial.println("hosszu lenyomas");
}
// called when key goes from not pressed to pressed
void keyPress() {
KeyPressCount = 0;
}
// called when key goes from pressed to not pressed
void keyRelease() {
if (KeyPressCount < longKeyPressCountMax && KeyPressCount >= mediumKeyPressCountMin) {
mediumKeyPress();
}
else {
if (KeyPressCount < mediumKeyPressCountMin) {
shortKeyPress();
}
}
DBG_OUTPUT_PORT.printf("Long button press\n");
setModeByStateString(BTN_MODE_LONG);
}
void button() {
@ -614,10 +623,18 @@ void button() {
byte currKeyState = digitalRead(BUTTON);
if ((prevKeyState == HIGH) && (currKeyState == LOW)) {
keyPress();
// key goes from not pressed to pressed
KeyPressCount = 0;
}
else if ((prevKeyState == LOW) && (currKeyState == HIGH)) {
keyRelease();
if (KeyPressCount < longKeyPressCountMax && KeyPressCount >= mediumKeyPressCountMin) {
mediumKeyPress();
}
else {
if (KeyPressCount < mediumKeyPressCountMin) {
shortKeyPress();
}
}
}
else if (currKeyState == LOW) {
KeyPressCount++;