From 067f76f71facf7980645ad8de9df9cd8a7f13851 Mon Sep 17 00:00:00 2001 From: Daan Meijer Date: Mon, 21 Apr 2025 14:40:20 +0200 Subject: [PATCH] controls werken, wifi ook --- platformio.ini | 5 ++++- src/Controls.cpp | 32 +++++++++++++++++++++----------- src/Controls.h | 8 ++++---- src/Hanglamp.cpp | 6 ++++++ src/Led.cpp | 2 ++ src/Wifi.cpp | 5 ++++- 6 files changed, 41 insertions(+), 17 deletions(-) diff --git a/platformio.ini b/platformio.ini index bd2d295..c7c89b4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -41,7 +41,10 @@ board_build.f_cpu = 80000000L board = dfrobot_beetle_esp32c3 platform = espressif32 framework = arduino -monitor_speed = 460800 +monitor_speed = 115200 +monitor_filters = + esp32_exception_decoder + time lib_deps = fastled/FastLED@^3.7.0 knolleary/PubSubClient@^2.8 diff --git a/src/Controls.cpp b/src/Controls.cpp index 57cba0f..e559317 100644 --- a/src/Controls.cpp +++ b/src/Controls.cpp @@ -7,10 +7,16 @@ uint8_t controlPins[] = { PIN_KEY, }; - +uint16_t thresholds[] = { + 2048, + 1000, + 3072, +}; uint16_t prevState[3]; +uint8_t controlPinCount = sizeof(controlPins) / sizeof(controlPins[0]); + auto lastChange = micros(); uint8_t pinChanges[128]; uint8_t pinChangeIndex = 0; @@ -39,28 +45,31 @@ void Controls_loop(void *pvParameters){ lastChange = 0; while(true){ - for(uint8_t a=0; a 3072; + auto currentValue = currentValueAnalog > thresholds[a]; auto prevValue = prevState[a]; - if(CONTROLS_DEBUG){ + if(CONTROLS_DEBUG > 200){ Serial.printf("Pin %d: %d->%d (%d)\n", pin, prevValue, currentValue, currentValueAnalog); } // if(abs(currentValue - prevValue) > 100){ if(currentValue != prevValue){ + if(CONTROLS_DEBUG > 100){ + Serial.printf("Change on pin %d: %d -> %d (%d)\n", pin, prevValue, currentValue, currentValueAnalog); + } if(pin == PIN_KEY){ if(currentValue){ auto duration = micros() - lastKey; if(duration > KEY_DURATION_MS * 1000){ - if(CONTROLS_DEBUG){ + if(CONTROLS_DEBUG > 10){ Serial.printf("Key: %d\n", duration); } Controls_send_event({Key, (int)duration}); }else{ - if(CONTROLS_DEBUG){ + if(CONTROLS_DEBUG > 20){ Serial.println("Too short"); } } @@ -89,7 +98,7 @@ void Controls_loop(void *pvParameters){ pinChanges[base + 3] == PIN_B ){ - if(CONTROLS_DEBUG){ + if(CONTROLS_DEBUG > 10){ Serial.println("Dir A"); } Controls_send_event({Counterclockwise, NULL}); @@ -100,13 +109,14 @@ void Controls_loop(void *pvParameters){ pinChanges[base + 3] == PIN_A ){ - if(CONTROLS_DEBUG){ + if(CONTROLS_DEBUG > 10){ Serial.println("Dir B"); } Controls_send_event({Clockwise, NULL}); }else{ - if(CONTROLS_DEBUG){ + if(CONTROLS_DEBUG > 20){ Serial.println("Unknown direction"); + Serial.printf("[%d, %d, %d, %d]\n", pinChanges[base + 0], pinChanges[base + 1], pinChanges[base + 2], pinChanges[base + 3]); } } } @@ -116,14 +126,14 @@ void Controls_loop(void *pvParameters){ if(lastChange){ if(micros() - lastChange > 100 * 1000){ - if(CONTROLS_DEBUG){ + if(CONTROLS_DEBUG > 100){ Serial.println("changes expired"); } Controls_direction_reset(); } } - vTaskDelay(1/portTICK_PERIOD_MS); + vTaskDelay(2/portTICK_PERIOD_MS); } } diff --git a/src/Controls.h b/src/Controls.h index e0d0a98..19f7277 100644 --- a/src/Controls.h +++ b/src/Controls.h @@ -2,13 +2,13 @@ #include -#define PIN_A 2 -#define PIN_B 1 -#define PIN_KEY 0 +#define PIN_A 1 +#define PIN_B 0 +#define PIN_KEY 2 #define KEY_DURATION_MS 150 -#define CONTROLS_DEBUG false +#define CONTROLS_DEBUG 1 enum ControlEventType { diff --git a/src/Hanglamp.cpp b/src/Hanglamp.cpp index 6a2571b..899d958 100644 --- a/src/Hanglamp.cpp +++ b/src/Hanglamp.cpp @@ -8,6 +8,12 @@ CRGB colorTable[] = { // CRGB(255, 100, 32), // CRGB(255, 128, 32), CRGB::White, + CRGB::Red, + CRGB::Green, + CRGB::Blue, + CRGB::Yellow, + CRGB::Turquoise, + CRGB::Purple, CRGB(255, 142, 32), // "warm wit" }; diff --git a/src/Led.cpp b/src/Led.cpp index 5c61293..c3411ce 100644 --- a/src/Led.cpp +++ b/src/Led.cpp @@ -127,6 +127,8 @@ void Led_setup(){ FastLED.addLeds(strip2, NUM_LEDS).setRgbw(RgbwDefault()); strip1[0] = CRGB::Black; strip2[0] = CRGB::Black; + + FastLED.show(); #endif #ifdef LED_NEOPIXEL_BUS diff --git a/src/Wifi.cpp b/src/Wifi.cpp index 5672a22..13af6f1 100644 --- a/src/Wifi.cpp +++ b/src/Wifi.cpp @@ -30,8 +30,11 @@ void Wifi_loop(void* params){ { while (WiFi.status() != WL_CONNECTED) { - vTaskDelay(500 / portTICK_PERIOD_MS); Serial.println("Wifi waiting..."); + vTaskDelay(5000 / portTICK_PERIOD_MS); + if(WiFi.status() != WL_CONNECTED){ + WiFi.reconnect(); + } } Serial.println("");