diff --git a/src/Controls.cpp b/src/Controls.cpp index 78774e7..57cba0f 100644 --- a/src/Controls.cpp +++ b/src/Controls.cpp @@ -58,7 +58,7 @@ void Controls_loop(void *pvParameters){ if(CONTROLS_DEBUG){ Serial.printf("Key: %d\n", duration); } - Controls_send_event(Key); + Controls_send_event({Key, (int)duration}); }else{ if(CONTROLS_DEBUG){ Serial.println("Too short"); @@ -92,7 +92,7 @@ void Controls_loop(void *pvParameters){ if(CONTROLS_DEBUG){ Serial.println("Dir A"); } - Controls_send_event(Counterclockwise); + Controls_send_event({Counterclockwise, NULL}); }else if( pinChanges[base + 0] == PIN_B && pinChanges[base + 1] == PIN_A && @@ -103,7 +103,7 @@ void Controls_loop(void *pvParameters){ if(CONTROLS_DEBUG){ Serial.println("Dir B"); } - Controls_send_event(Clockwise); + Controls_send_event({Clockwise, NULL}); }else{ if(CONTROLS_DEBUG){ Serial.println("Unknown direction"); @@ -136,5 +136,5 @@ void Controls_setup(){ TaskHandle_t taskControls = NULL; - xTaskCreate(Controls_loop, "Controls_loop", 1000, NULL, tskIDLE_PRIORITY, &taskControls); + xTaskCreate(Controls_loop, "Controls_loop", 2048, NULL, tskIDLE_PRIORITY, &taskControls); } \ No newline at end of file diff --git a/src/Controls.h b/src/Controls.h index a1ce696..b4b6838 100644 --- a/src/Controls.h +++ b/src/Controls.h @@ -6,16 +6,22 @@ #define PIN_B 3 #define PIN_KEY 4 -#define KEY_DURATION_MS 120 +#define KEY_DURATION_MS 150 #define CONTROLS_DEBUG false -enum ControlEvent { + +enum ControlEventType { Clockwise, Counterclockwise, Key }; +struct ControlEvent { + ControlEventType type; + int data; +}; + typedef void (* ControlsCallback)( ControlEvent ) ; void Controls_setup(); diff --git a/src/Hanglamp.cpp b/src/Hanglamp.cpp index c2b623c..6a2571b 100644 --- a/src/Hanglamp.cpp +++ b/src/Hanglamp.cpp @@ -3,21 +3,17 @@ 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(255, 100, 4), + // CRGB(255, 128, 4), + // CRGB(255, 100, 32), + // CRGB(255, 128, 32), + CRGB::White, + CRGB(255, 142, 32), // "warm wit" }; Hanglamp::Hanglamp(){ - + turnOn(); } void Hanglamp::setup(){ @@ -57,8 +53,8 @@ void Hanglamp::adjustBrightness(int add){ } void Hanglamp::controlsCallback(ControlEvent event){ - Serial.printf("ControlEvent: %d\n", event); - switch(event){ + Serial.printf("ControlEventType: %d\n", event); + switch(event.type){ case Clockwise: adjustBrightness(4); break; @@ -66,11 +62,51 @@ void Hanglamp::controlsCallback(ControlEvent event){ adjustBrightness(-4); break; case Key: - nextColor(); + Serial.printf("key press %d\n", event.data); + + if(!isOn()){ + turnOn(); + }else{ + auto msDuration = event.data/1000; + + Serial.printf("msDuration %d\n", msDuration); + if(msDuration > 800){ + turnOff(); + }else{ + nextColor(); + } + } + break; } } void Hanglamp::status(CRGB color){ Led_status(color); -} \ No newline at end of file +} + +void Hanglamp::setOn(bool on){ + Serial.printf("Hanglamp::setOn(%d -> %d)\n", this->on, on); + this->on = on; +} + +bool Hanglamp::isOn(){ + return this->on; +} + +void Hanglamp::turnOn(){ + if(!this->on){ + FastLED.setBrightness(this->brightness); + FastLED.show(); + this->setOn(true); + } +} + +void Hanglamp::turnOff(){ + if(this->on){ + FastLED.setBrightness(0); + FastLED.show(); + this->setOn(false); + } +} + diff --git a/src/Hanglamp.hpp b/src/Hanglamp.hpp index 964fc2f..53c0b94 100644 --- a/src/Hanglamp.hpp +++ b/src/Hanglamp.hpp @@ -16,9 +16,15 @@ class Hanglamp { void status(CRGB color); void setColor(CRGB color); + bool isOn(); + void turnOn(); + void turnOff(); + private: char colorIndex = 0; int brightness = 128; + bool on = false; + void setOn(bool on); }; \ No newline at end of file diff --git a/src/MQTT.h b/src/MQTT.h index 1c3c740..39c1d2a 100644 --- a/src/MQTT.h +++ b/src/MQTT.h @@ -1,3 +1,4 @@ +#pragma once #include "settings.h" @@ -5,6 +6,8 @@ #include +#include + using namespace std;