From d21632da8d573729be78d74329e3b743865f2e53 Mon Sep 17 00:00:00 2001 From: pulipakaa24 Date: Mon, 12 Jan 2026 16:26:25 -0600 Subject: [PATCH] ensure we don't keep queueing http requests when we're not in the looping state. --- include/mainEventLoop.cpp | 4 ++++ include/servo.cpp | 8 ++++---- include/socketIO.cpp | 4 ++++ include/socketIO.hpp | 2 ++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/mainEventLoop.cpp b/include/mainEventLoop.cpp index ecbc808..56d1343 100644 --- a/include/mainEventLoop.cpp +++ b/include/mainEventLoop.cpp @@ -6,12 +6,16 @@ #include "cJSON.h" #include "encoder.hpp" #include "WiFi.hpp" +#include "socketIO.hpp" TaskHandle_t wakeTaskHandle = NULL; void wakeTimer(void* pvParameters) { while (1) { vTaskDelay(pdMS_TO_TICKS(60000)); + // avoid accumulating events during re-setup or calibration + if (setupTaskHandle != NULL || socketIOactive + || uxQueueMessagesWaiting(main_event_queue) > 2) continue; main_event_type_t evt = EVENT_REQUEST_POS; xQueueSend(main_event_queue, &evt, portMAX_DELAY); } diff --git a/include/servo.cpp b/include/servo.cpp index 4f37fe0..82a8604 100644 --- a/include/servo.cpp +++ b/include/servo.cpp @@ -142,10 +142,6 @@ void initMainLoop() { void IRAM_ATTR watchdogCallback(void* arg) { if (runningManual || runningServer) { - BaseType_t xHigherPriorityTaskWoken = pdFALSE; - main_event_type_t evt = EVENT_CLEAR_CALIB; - BaseType_t result = xQueueSendFromISR(main_event_queue, &evt, &xHigherPriorityTaskWoken); - if (result == pdPASS) portYIELD_FROM_ISR(xHigherPriorityTaskWoken); topEnc->pauseWatchdog(); // get ready for recalibration by clearing all these listeners @@ -153,6 +149,10 @@ void IRAM_ATTR watchdogCallback(void* arg) { topEnc->wandListen.store(false, std::memory_order_release); topEnc->serverListen.store(false, std::memory_order_release); servoOff(); + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + main_event_type_t evt = EVENT_CLEAR_CALIB; + BaseType_t result = xQueueSendFromISR(main_event_queue, &evt, &xHigherPriorityTaskWoken); + if (result == pdPASS) portYIELD_FROM_ISR(xHigherPriorityTaskWoken); } else { // if no movement is running, we're fine diff --git a/include/socketIO.cpp b/include/socketIO.cpp index feda7f3..8773280 100644 --- a/include/socketIO.cpp +++ b/include/socketIO.cpp @@ -12,6 +12,7 @@ static esp_socketio_client_handle_t io_client; static esp_socketio_packet_handle_t tx_packet = NULL; static bool stopSocketFlag = false; +std::atomic socketIOactive{false}; // Event handler for Socket.IO events static void socketio_event_handler(void *handler_args, esp_event_base_t base, @@ -300,6 +301,8 @@ void initSocketIO() { esp_socketio_register_events(io_client, SOCKETIO_EVENT_ANY, socketio_event_handler, NULL); esp_socketio_client_start(io_client); + + socketIOactive = true; } void stopSocketIO() { @@ -310,6 +313,7 @@ void stopSocketIO() { io_client = NULL; tx_packet = NULL; } + socketIOactive = false; } // Helper function to emit Socket.IO event with data diff --git a/include/socketIO.hpp b/include/socketIO.hpp index 86c9d70..989ed93 100644 --- a/include/socketIO.hpp +++ b/include/socketIO.hpp @@ -2,6 +2,8 @@ #define SOCKETIO_HPP #include +extern std::atomic socketIOactive; + // Initialize Socket.IO client and connect to server void initSocketIO();