diff --git a/src/Controls.cpp b/src/Controls.cpp index fa0cb67..096bb42 100644 --- a/src/Controls.cpp +++ b/src/Controls.cpp @@ -8,26 +8,130 @@ uint8_t controlPins[] = { }; + +uint16_t prevState[3]; + +auto lastChange = micros(); +uint8_t pinChanges[128]; +uint8_t pinChangeIndex = 0; + +void Controls_direction_reset(){ + lastChange = 0; + pinChangeIndex = 0; +} + +auto lastKey = micros(); + +ControlsCallback handler = NULL; + +void Controls_handler(ControlsCallback newHandler){ + handler = newHandler; +} + +void Controls_send_event(ControlEvent event){ + if(handler){ + handler(event); + } +} + +void Controls_loop(void *pvParameters){ + lastKey = 0; + lastChange = 0; + + while(true){ + for(uint8_t a=0; a 3072; + auto prevValue = prevState[a]; + + // Serial.printf("Pin %d: %d->%d\n", pin, prevValue, currentValue); + + // if(abs(currentValue - prevValue) > 100){ + if(currentValue != prevValue){ + if(pin == PIN_KEY){ + if(currentValue){ + auto duration = micros() - lastKey; + if(duration > KEY_DURATION_MS * 1000){ + if(CONTROLS_DEBUG){ + Serial.printf("Key: %d\n", duration); + } + Controls_send_event(Key); + }else{ + if(CONTROLS_DEBUG){ + Serial.println("Too short"); + } + } + lastKey = 0; + } else { + lastKey = micros(); + } + + }else{ + pinChanges[pinChangeIndex++] = pin; + lastChange = micros(); + } + } + + prevState[a] = currentValue; + } + + //maybe we have a valid sequence + if(pinChangeIndex >= 4){ + // Serial.printf("Have %d changes\n", pinChangeIndex); + for(uint8_t base = 0; base + 4 <= pinChangeIndex; base += 4){ + if( + pinChanges[base + 0] == PIN_A && + pinChanges[base + 1] == PIN_B && + pinChanges[base + 2] == PIN_A && + pinChanges[base + 3] == PIN_B + ){ + + if(CONTROLS_DEBUG){ + Serial.println("Dir A"); + } + Controls_send_event(Counterclockwise); + }else if( + pinChanges[base + 0] == PIN_B && + pinChanges[base + 1] == PIN_A && + pinChanges[base + 2] == PIN_B && + pinChanges[base + 3] == PIN_A + ){ + + if(CONTROLS_DEBUG){ + Serial.println("Dir B"); + } + Controls_send_event(Clockwise); + }else{ + if(CONTROLS_DEBUG){ + Serial.println("Unknown direction"); + } + } + } + Controls_direction_reset(); + } + + if(lastChange){ + if(micros() - lastChange > 100 * 1000){ + + if(CONTROLS_DEBUG){ + Serial.println("changes expired"); + } + Controls_direction_reset(); + } + } + + vTaskDelay(1/portTICK_PERIOD_MS); + + } +} + + void Controls_setup(){ pinMode(PIN_A, INPUT_PULLDOWN); pinMode(PIN_B, INPUT_PULLDOWN); pinMode(PIN_KEY, INPUT_PULLDOWN); -} -int16_t prevState[3]; - -void Controls_loop(){ - for(uint8_t a=0; a%d\n", pin, prevValue, currentValue); - - if(abs(currentValue - prevValue) > 100){ - Serial.println("Changed!"); - } - - prevState[a] = currentValue; - } + + TaskHandle_t taskControls = NULL; + xTaskCreate(Controls_loop, "Controls_loop", 1000, NULL, tskIDLE_PRIORITY, &taskControls); } \ No newline at end of file diff --git a/src/Controls.h b/src/Controls.h index edaf7e4..a1ce696 100644 --- a/src/Controls.h +++ b/src/Controls.h @@ -6,6 +6,18 @@ #define PIN_B 3 #define PIN_KEY 4 +#define KEY_DURATION_MS 120 + +#define CONTROLS_DEBUG false + +enum ControlEvent { + Clockwise, + Counterclockwise, + Key +}; + +typedef void (* ControlsCallback)( ControlEvent ) ; void Controls_setup(); -void Controls_loop(); \ No newline at end of file + +void Controls_handler(ControlsCallback callback); \ No newline at end of file diff --git a/src/Hanglamp.cpp b/src/Hanglamp.cpp index 6388c63..9213e99 100644 --- a/src/Hanglamp.cpp +++ b/src/Hanglamp.cpp @@ -93,20 +93,23 @@ CRGB colorTable[] = { }; -uint8_t colorIndex = 0; +int8_t colorIndex = 0; + +void nextColor(){ + colorIndex++; + if(colorIndex >= (sizeof(colorTable) / sizeof(CRGB))){ + colorIndex = 0; + } + Serial.printf("colorIndex: %d\n", colorIndex); + jumpTo(colorTable[colorIndex]); +} + void colors(void *pvParameters){ while(true){ - colorIndex++; - if(colorIndex >= (sizeof(colorTable) / sizeof(CRGB))){ - colorIndex = 0; - } - Serial.printf("colorIndex: %d\n", colorIndex); - jumpTo(colorTable[colorIndex]); + nextColor(); vTaskDelay(1000 / portTICK_PERIOD_MS); } - - vTaskDelete(NULL); } #endif @@ -130,12 +133,52 @@ gpio_num_t sensePins[] = { long prevStates[sizeof(sensePins)]; #endif +int16_t brightness = 128; + +#ifdef ENABLE_CONTROLS + +void newBrightness(){ + uint8_t newBrightness = + brightness < 0 ? 0 : + brightness > 255 ? 255 : + brightness; + Serial.printf("new brightness: %d\n", newBrightness); + delay(100); + FastLED.setBrightness(newBrightness); + FastLED.show(); +} + +void controlsCallback(ControlEvent event){ + Serial.printf("ControlEvent: %d\n", event); + switch(event){ + case Clockwise: + if(brightness < 256){ + brightness += 8; + newBrightness(); + } + break; + case Counterclockwise: + if(brightness > 0){ + brightness -= 8 ; + newBrightness(); + } + break; + case Key: + nextColor(); + break; + } +} +#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 #endif -#define PIN_STRIP1 5 -#define PIN_STRIP2 6 +#define PIN_STRIP1 8 +#define PIN_STRIP2 9 #define STRIP_LENGTH 46