From 23ddc2e86faad90b3f1cfb2807fbe9a570582e14 Mon Sep 17 00:00:00 2001 From: BPoH_Voodoo Date: Sat, 3 Mar 2018 10:13:58 +0100 Subject: [PATCH] Intergated changes of original projects --- Arduino/McLighting/WS2812FX.cpp | 32 ++++++++++++++++++++++----- Arduino/McLighting/WS2812FX.h | 11 +++++++-- Arduino/McLighting/request_handlers.h | 14 ++++++++++++ README.md | 4 ++++ 4 files changed, 54 insertions(+), 7 deletions(-) diff --git a/Arduino/McLighting/WS2812FX.cpp b/Arduino/McLighting/WS2812FX.cpp index eb234f8..5dfe03c 100644 --- a/Arduino/McLighting/WS2812FX.cpp +++ b/Arduino/McLighting/WS2812FX.cpp @@ -49,6 +49,7 @@ 2017-02-02 removed "blackout" on mode, speed or color-change 2017-09-26 implemented segment and reverse features 2017-11-16 changed speed calc, reduced memory footprint + 2018-02-24 added hooks for user created custom effects */ #include "WS2812FX.h" @@ -223,7 +224,7 @@ const __FlashStringHelper* WS2812FX::getModeName(uint8_t m) { } void WS2812FX::setSegment(uint8_t n, uint16_t start, uint16_t stop, uint8_t mode, uint32_t color, uint16_t speed, bool reverse) { - if(n < MAX_NUM_SEGMENTS) { + if(n < (sizeof(_segments) / sizeof(_segments[0]))) { if(n + 1 > _num_segments) _num_segments = n + 1; _segments[n].start = start; _segments[n].stop = stop; @@ -235,7 +236,7 @@ void WS2812FX::setSegment(uint8_t n, uint16_t start, uint16_t stop, uint8_t mode } void WS2812FX::setSegment(uint8_t n, uint16_t start, uint16_t stop, uint8_t mode, const uint32_t colors[], uint16_t speed, bool reverse) { - if(n < MAX_NUM_SEGMENTS) { + if(n < (sizeof(_segments) / sizeof(_segments[0]))) { if(n + 1 > _num_segments) _num_segments = n + 1; _segments[n].start = start; _segments[n].stop = stop; @@ -252,9 +253,10 @@ void WS2812FX::setSegment(uint8_t n, uint16_t start, uint16_t stop, uint8_t mode void WS2812FX::resetSegments() { memset(_segments, 0, sizeof(_segments)); memset(_segment_runtimes, 0, sizeof(_segment_runtimes)); - _segment_index = 0; + + _segment_index = 0; _num_segments = 1; - setSegment(0, 0, 7, FX_MODE_STATIC, {DEFAULT_COLOR}, DEFAULT_SPEED, false); + setSegment(0, 0, 7, FX_MODE_STATIC, DEFAULT_COLOR, DEFAULT_SPEED, false); } /* ##################################################### @@ -618,7 +620,7 @@ uint16_t WS2812FX::mode_rainbow(void) { */ uint16_t WS2812FX::mode_rainbow_cycle(void) { for(uint16_t i=0; i < SEGMENT_LENGTH; i++) { - uint32_t color = color_wheel(((i * 256 / SEGMENT_LENGTH) + SEGMENT_RUNTIME.counter_mode_step) & 0xFF); + uint32_t color = color_wheel(((i * 256 / SEGMENT_LENGTH) + SEGMENT_RUNTIME.counter_mode_step) & 0xFF); Adafruit_NeoPixel::setPixelColor(SEGMENT.start + i, color); } @@ -1353,3 +1355,23 @@ uint16_t WS2812FX::mode_icu() { return (SEGMENT.speed / SEGMENT_LENGTH); } + +/* + * Custom mode + */ +uint16_t (*customMode)(void) = NULL; +uint16_t WS2812FX::mode_custom() { + if(customMode == NULL) { + return 1000; // if custom mode not set, do nothing + } else { + return customMode(); + } +} + +/* + * Custom mode helper + */ +void WS2812FX::setCustomMode(uint16_t (*p)()) { + setMode(FX_MODE_CUSTOM); + customMode = p; +} \ No newline at end of file diff --git a/Arduino/McLighting/WS2812FX.h b/Arduino/McLighting/WS2812FX.h index a3867a1..089f70e 100644 --- a/Arduino/McLighting/WS2812FX.h +++ b/Arduino/McLighting/WS2812FX.h @@ -50,6 +50,8 @@ #define BRIGHTNESS_MIN 0 #define BRIGHTNESS_MAX 255 + + #define MAX_NUM_SEGMENTS 10 #define NUM_COLORS 4 /* number of colors per segment */ #define SEGMENT _segments[_segment_index] @@ -70,7 +72,7 @@ #define ORANGE 0x00FF3000 #define ULTRAWHITE 0xFFFFFFFF -#define MODE_COUNT 56 +#define MODE_COUNT 57 #define FX_MODE_STATIC 0 #define FX_MODE_BLINK 1 @@ -128,6 +130,7 @@ #define FX_MODE_BICOLOR_CHASE 53 #define FX_MODE_TRICOLOR_CHASE 54 #define FX_MODE_ICU 55 +#define FX_MODE_CUSTOM 56 class WS2812FX : public Adafruit_NeoPixel { @@ -219,6 +222,7 @@ class WS2812FX : public Adafruit_NeoPixel { _mode[FX_MODE_RUNNING_LIGHTS] = &WS2812FX::mode_running_lights; _mode[FX_MODE_ICU] = &WS2812FX::mode_icu; #endif + _mode[FX_MODE_CUSTOM] = &WS2812FX::mode_custom; _name[FX_MODE_STATIC] = F("Static"); _name[FX_MODE_BLINK] = F("Blink"); @@ -276,6 +280,7 @@ class WS2812FX : public Adafruit_NeoPixel { _name[FX_MODE_BICOLOR_CHASE] = F("Bicolor Chase"); _name[FX_MODE_TRICOLOR_CHASE] = F("Tricolor Chase"); _name[FX_MODE_ICU] = F("ICU"); + _name[FX_MODE_CUSTOM] = F("Custom"); _brightness = DEFAULT_BRIGHTNESS; _running = false; @@ -294,6 +299,7 @@ class WS2812FX : public Adafruit_NeoPixel { start(void), stop(void), setMode(uint8_t m), + setCustomMode(uint16_t (*p)()), setSpeed(uint16_t s), increaseSpeed(uint8_t s), decreaseSpeed(uint8_t s), @@ -405,7 +411,8 @@ class WS2812FX : public Adafruit_NeoPixel { tricolor_chase(uint32_t, uint32_t, uint32_t), mode_bicolor_chase(void), mode_tricolor_chase(void), - mode_icu(void); + mode_icu(void), + mode_custom(void); boolean _running, diff --git a/Arduino/McLighting/request_handlers.h b/Arduino/McLighting/request_handlers.h index 21fe77f..23dddaa 100644 --- a/Arduino/McLighting/request_handlers.h +++ b/Arduino/McLighting/request_handlers.h @@ -611,9 +611,15 @@ void shortKeyPress() { if (buttonState == false) { setModeByStateString(BTN_MODE_SHORT); buttonState = true; + #ifdef ENABLE_MQTT + mqtt_client.publish(mqtt_outtopic, String("OK =static white").c_str()); + #endif } else { mode = OFF; buttonState = false; + #ifdef ENABLE_MQTT + mqtt_client.publish(mqtt_outtopic, String("OK =off").c_str()); + #endif } } @@ -621,12 +627,20 @@ void shortKeyPress() { void mediumKeyPress() { DBG_OUTPUT_PORT.printf("Medium button press\n"); setModeByStateString(BTN_MODE_MEDIUM); + buttonState = true; + #ifdef ENABLE_MQTT + mqtt_client.publish(mqtt_outtopic, String("OK =fire flicker").c_str()); + #endif } // called when button is kept pressed for 2 seconds or more void longKeyPress() { DBG_OUTPUT_PORT.printf("Long button press\n"); setModeByStateString(BTN_MODE_LONG); + buttonState = true; + #ifdef ENABLE_MQTT + mqtt_client.publish(mqtt_outtopic, String("OK =fireworks random").c_str()); + #endif } void button() { diff --git a/README.md b/README.md index cc32356..eb65132 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,10 @@ > Because of it's open architecture and APIs it's easy to build new clients for different platforms (iOS, Android, Windows Universal Apps, Siri/Cortana integration, ...). ___ +Update 17.02.2018: +User @debsahu contributed code for integration with homeassistant. It's currently in a separate branch (https://github.com/toblum/McLighting/tree/feature/ha_integration). If you're using Homeassistant, please try it out and give feedback. +A thank you goes to all contributors. + Update 31.01.2018: User @codmpm did a very professional McLighting installation and even designed his own PCBs. He has a great writeup for his project at: https://allgeek.de/2018/01/29/esp8266-neopixel-controller/ (in german).