diff --git a/src/Hanglamp.cpp b/src/Hanglamp.cpp index 96b3b4b..3fb2e90 100644 --- a/src/Hanglamp.cpp +++ b/src/Hanglamp.cpp @@ -1,345 +1,76 @@ -#define INCLUDE_LED -#ifdef INCLUDE_LED -#include "Led.h" -#endif - -#include - -#define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass -#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only - -#define ENABLE_MQTT -#define ENABLE_WIFI -// #define ENABLE_FS -// #define ENABLE_PINFINDER -#define ENABLE_COLORS -// #define ENABLE_PIN_SENSING -#define ENABLE_CONTROLS - -// #include - -#ifdef ENABLE_WIFI -#include "Wifi.h" -#endif - -#ifdef ENABLE_MQTT -#include "MQTT.h" -#endif - -#ifdef ENABLE_FS -#include "FS.h" -#include -#endif - -#ifdef ENABLE_PINFINDER -#include "PinFinder.h" -#endif - -#ifdef ENABLE_CONTROLS -#include "Controls.h" -#endif +#include "Hanglamp.hpp" + -#ifdef ENABLE_COLORS CRGB colorTable[] = { + CRGB(255, 243, 218), + CRGB(255, 250, 237), + CRGB(255, 255, 255), + CRGB(250, 233, 213), CRGB::Red, CRGB::Blue, CRGB::Green, CRGB::Purple, CRGB::Turquoise, CRGB::Yellow, - CRGB::Red, - CRGB::Blue, - CRGB::Green, - CRGB::Purple, - CRGB::Turquoise, - CRGB::Yellow, - CRGB::Red, - CRGB::Blue, - CRGB::Green, - CRGB::Purple, - CRGB::Turquoise, - CRGB::Yellow, - CRGB::Red, - CRGB::Blue, - CRGB::Green, - CRGB::Purple, - CRGB::Turquoise, - CRGB::Yellow, - CRGB::Red, - CRGB::Blue, - CRGB::Green, - CRGB::Purple, - CRGB::Turquoise, - CRGB::Yellow, - CRGB::Red, - CRGB::Blue, - CRGB::Green, - CRGB::Purple, - CRGB::Turquoise, - CRGB::Yellow, - CRGB::Red, - CRGB::Blue, - CRGB::Green, - CRGB::Purple, - CRGB::Turquoise, - CRGB::Yellow, - CRGB::Red, - CRGB::Blue, - CRGB::Green, - CRGB::Purple, - CRGB::Turquoise, - CRGB::Yellow, - }; -int8_t colorIndex = 0; -void nextColor(){ - colorIndex++; +Hanglamp::Hanglamp(){ + +} + +void Hanglamp::setup(){ + +} + +void Hanglamp::setColor(CRGB color){ + jumpTo(color); +} + +void Hanglamp::nextColor(){ + this->colorIndex++; if(colorIndex >= (sizeof(colorTable) / sizeof(CRGB))){ colorIndex = 0; } - Serial.printf("colorIndex: %d\n", colorIndex); - jumpTo(colorTable[colorIndex]); + Serial.printf("colorIndex: %d\n", this->colorIndex); + + setColor(colorTable[this->colorIndex]); } -void colors(void *pvParameters){ - while(true){ - nextColor(); - - vTaskDelay(1000 / portTICK_PERIOD_MS); - } -} -#endif - -#ifdef ENABLE_PIN_SENSING -gpio_num_t sensePins[] = { - (gpio_num_t) 0, - (gpio_num_t) 1, - (gpio_num_t) 2, - (gpio_num_t) 3, - (gpio_num_t) 4, - (gpio_num_t) 5, - (gpio_num_t) 6, - (gpio_num_t) 7, - (gpio_num_t) 8, - (gpio_num_t) 9, - (gpio_num_t) 10, - (gpio_num_t) 20, - (gpio_num_t) 21, -}; - -long prevStates[sizeof(sensePins)]; -#endif - -int16_t brightness = 128; - -#ifdef ENABLE_CONTROLS - -void newBrightness(){ - uint8_t newBrightness = +void Hanglamp::setBrightness(char brightness){ + uint8_t newBrightness = brightness < 0 ? 0 : brightness > 255 ? 255 : brightness; - Serial.printf("new brightness: %d\n", newBrightness); - delay(100); - FastLED.setBrightness(newBrightness); - FastLED.show(); - #ifdef ENABLE_MQTT - MQTT_publish("hanglamp/brightness", String(newBrightness)); - #endif + this->brightness = newBrightness; + Serial.printf("new brightness: %d\n", newBrightness); + FastLED.setBrightness(newBrightness); + FastLED.show(); + #ifdef ENABLE_MQTT + MQTT_publish("hanglamp/brightness", String(newBrightness)); + #endif } -void controlsCallback(ControlEvent event){ - Serial.printf("ControlEvent: %d\n", event); +void Hanglamp::adjustBrightness(int add){ + this->setBrightness(this->brightness + add); +} + +void Hanglamp::controlsCallback(ControlEvent event){ + Serial.printf("ControlEvent: %d\n", event); switch(event){ case Clockwise: - if(brightness < 256){ - brightness += 8; - newBrightness(); - } + adjustBrightness(1); break; case Counterclockwise: - if(brightness > 0){ - brightness -= 8 ; - newBrightness(); - } + adjustBrightness(-1); break; case Key: nextColor(); break; } } -#endif -#ifdef ENABLE_LED - -#endif - -void setup() { - - - Serial.begin(115200); - - Serial.println("Waiting..."); - delay(1500); - Serial.println("That's long enough."); - - - #ifdef ENABLE_PIN_SENSING - for(int i=0; i 10){ - Serial.printf("pin#%02d %d->%d\n", pin, prevStates[i], currentState); - // } - prevStates[i] = currentState; - } - - - for(int i=0; i= 0){ - String name = command.substring(0, index); - Serial.print("Have command name: "); - Serial.println(name); - - #ifdef INCLUDE_LED - if(name.equals("c")){ - //c:0xff4000 - //c:0xff0000 - //c:0x00ff00 - //c:0x0000ff - String color = command.substring(index+1); - if(color.startsWith("0x")){ - color = color.substring(2); - } - - int value = strtol(color.c_str(), NULL, 16); - - CRGB target; - target.r = (value >> 16) & 0xFF; - target.g = (value >> 8) & 0xFF; - target.b = (value >> 0) & 0xFF; - - jumpTo(target); - } - #endif - } - - } - -} +void Hanglamp::status(CRGB color){ + Led_status(color); +} \ No newline at end of file diff --git a/src/Hanglamp.hpp b/src/Hanglamp.hpp new file mode 100644 index 0000000..e9e98e7 --- /dev/null +++ b/src/Hanglamp.hpp @@ -0,0 +1,24 @@ +#pragma once + +#include + +#include "Led.h" +#include "Controls.h" + +class Hanglamp { + public: + Hanglamp(); + void setup(); + void setBrightness(char brightness); + void adjustBrightness(int add); + void nextColor(); + void controlsCallback(ControlEvent event); + void status(CRGB color); + void setColor(CRGB color); + + + private: + char colorIndex = 0; + int brightness = 128; + +}; \ No newline at end of file diff --git a/src/Led.cpp b/src/Led.cpp index 49eaa91..c53ff3e 100644 --- a/src/Led.cpp +++ b/src/Led.cpp @@ -123,8 +123,8 @@ void Led_setup(){ // analogWriteRange(255); #ifdef LED_FASTLED - FastLED.addLeds(strip2, NUM_LEDS); - FastLED.addLeds(strip2, NUM_LEDS); + FastLED.addLeds(strip2, NUM_LEDS); + FastLED.addLeds(strip2, NUM_LEDS); strip1[0] = CRGB::Black; strip2[0] = CRGB::Black; #endif @@ -156,4 +156,10 @@ void Led_loop(){ strip2[15] = CRGB(50, 255, 20); FastLED.show(); delay(500); +} + +void Led_status(CRGB color){ + strip1[0] = color; + strip2[0] = color; + FastLED.show(); } \ No newline at end of file diff --git a/src/Led.h b/src/Led.h index 08e542a..71cc419 100644 --- a/src/Led.h +++ b/src/Led.h @@ -35,4 +35,6 @@ void Led_loop(); void jumpTo(const CRGB & dest); void fadeTo(const CRGB & dest); -void fadeTask(); \ No newline at end of file +void fadeTask(); + +void Led_status(CRGB color); \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..5d44121 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,187 @@ +#define INCLUDE_LED +#ifdef INCLUDE_LED +#include "Led.h" +#endif + +#include + +#define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass +#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only + +#define ENABLE_MQTT +#define ENABLE_WIFI +// #define ENABLE_FS +// #define ENABLE_PINFINDER +#define ENABLE_COLORS +// #define ENABLE_PIN_SENSING +#define ENABLE_CONTROLS + +#ifdef ENABLE_WIFI +#include "Wifi.h" +#endif + +#ifdef ENABLE_MQTT +#include "MQTT.h" +#endif + +#ifdef ENABLE_FS +#include "FS.h" +#include +#endif + +#ifdef ENABLE_PINFINDER +#include "PinFinder.h" +#endif + +#ifdef ENABLE_CONTROLS +#include "Controls.h" +#endif + +#include "Hanglamp.hpp" + +Hanglamp hanglamp; + + +#ifdef ENABLE_CONTROLS + +void controlsCallback(ControlEvent event){ + hanglamp.controlsCallback(event); +} +#endif + +#ifdef ENABLE_LED + +#endif + +void setup() { + + Serial.begin(115200); + + Serial.println("Waiting..."); + delay(1500); + Serial.println("That's long enough."); + + + + #ifdef ENABLE_PIN_SENSING + for(int i=0; i= 0){ + String name = command.substring(0, index); + Serial.print("Have command name: "); + Serial.println(name); + + #ifdef INCLUDE_LED + if(name.equals("c")){ + //c:0xff4000 + //c:0xff0000 + //c:0x00ff00 + //c:0x0000ff + String color = command.substring(index+1); + if(color.startsWith("0x")){ + color = color.substring(2); + } + + int value = strtol(color.c_str(), NULL, 16); + + CRGB target; + target.r = (value >> 16) & 0xFF; + target.g = (value >> 8) & 0xFF; + target.b = (value >> 0) & 0xFF; + + hanglamp.setColor(target); + } + #endif + } + + } + +}