diff --git a/CYD-Klipper/src/core/data_setup.cpp b/CYD-Klipper/src/core/data_setup.cpp index 39ee59b..f89e29c 100644 --- a/CYD-Klipper/src/core/data_setup.cpp +++ b/CYD-Klipper/src/core/data_setup.cpp @@ -13,6 +13,33 @@ const char *printer_state_messages[] = { Printer printer = {0}; int klipper_request_consecutive_fail_count = 0; +char filename_buff[512] = {0}; +SemaphoreHandle_t freezeRenderThreadSemaphore, freezeRequestThreadSemaphore; +long last_data_update = 0; +const long data_update_interval = 800; + +void semaphore_init(){ + freezeRenderThreadSemaphore = xSemaphoreCreateMutex(); + freezeRequestThreadSemaphore = xSemaphoreCreateMutex(); + xSemaphoreGive(freezeRenderThreadSemaphore); + xSemaphoreGive(freezeRequestThreadSemaphore); +} + +void freeze_request_thread(){ + xSemaphoreTake(freezeRequestThreadSemaphore, portMAX_DELAY); +} + +void unfreeze_request_thread(){ + xSemaphoreGive(freezeRequestThreadSemaphore); +} + +void freeze_render_thread(){ + xSemaphoreTake(freezeRenderThreadSemaphore, portMAX_DELAY); +} + +void unfreeze_render_thread(){ + xSemaphoreGive(freezeRenderThreadSemaphore); +} void send_gcode(bool wait, const char *gcode) { @@ -36,10 +63,10 @@ void send_gcode(bool wait, const char *gcode) } } -char filename_buff[512] = {0}; - void fetch_printer_data() { + bool frozen = true; + freeze_request_thread(); char buff[256] = {}; sprintf(buff, "http://%s:%d/printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats&webhooks", global_config.klipperHost, global_config.klipperPort); HTTPClient client; @@ -55,6 +82,10 @@ void fetch_printer_data() bool emit_state_update = false; int printer_state = printer.state; + unfreeze_request_thread(); + frozen = false; + freeze_render_thread(); + if (status.containsKey("webhooks")) { const char *state = status["webhooks"]["state"]; @@ -164,23 +195,40 @@ void fetch_printer_data() klipper_request_consecutive_fail_count++; Serial.printf("Failed to fetch printer data: %d\n", httpCode); } -} -long last_data_update = 0; -const long data_update_interval = 1500; + if (frozen) + unfreeze_request_thread(); + + unfreeze_render_thread(); +} void data_loop() { - if (millis() - last_data_update < data_update_interval) - return; + // Causes other threads that are trying to lock the thread to actually lock it + unfreeze_render_thread(); + delay(1); + freeze_render_thread(); +} - fetch_printer_data(); - last_data_update = millis(); +void data_loop_background(void * param){ + while (true){ + delay(100); + if (millis() - last_data_update < data_update_interval) + continue; + + fetch_printer_data(); + last_data_update = millis(); + } } +TaskHandle_t background_loop; + void data_setup() { + semaphore_init(); printer.print_filename = filename_buff; fetch_printer_data(); macros_query_setup(); -} \ No newline at end of file + freeze_render_thread(); + xTaskCreatePinnedToCore(data_loop_background, "data_loop_background", 5000, NULL, 1, &background_loop, 0); +} diff --git a/CYD-Klipper/src/core/data_setup.h b/CYD-Klipper/src/core/data_setup.h index 3b2a151..fc0a44f 100644 --- a/CYD-Klipper/src/core/data_setup.h +++ b/CYD-Klipper/src/core/data_setup.h @@ -36,4 +36,7 @@ extern int klipper_request_consecutive_fail_count; void data_loop(); void data_setup(); -void send_gcode(bool wait, const char* gcode); \ No newline at end of file +void send_gcode(bool wait, const char* gcode); + +void freeze_request_thread(); +void unfreeze_request_thread(); \ No newline at end of file diff --git a/CYD-Klipper/src/core/files_query.cpp b/CYD-Klipper/src/core/files_query.cpp index 28abbb5..d6ebb42 100644 --- a/CYD-Klipper/src/core/files_query.cpp +++ b/CYD-Klipper/src/core/files_query.cpp @@ -1,6 +1,7 @@ #include #include "files_query.h" #include "../conf/global_config.h" +#include "data_setup.h" #include #include #include @@ -9,6 +10,8 @@ FILESYSTEM_FILE* last_query = NULL; FILESYSTEM_FILE* get_files(){ + freeze_request_thread(); + if (last_query != NULL){ FILESYSTEM_FILE* current = last_query; @@ -59,5 +62,6 @@ FILESYSTEM_FILE* get_files(){ result += 1; } + unfreeze_request_thread(); return last_query; } \ No newline at end of file diff --git a/CYD-Klipper/src/ui/ip_setup.cpp b/CYD-Klipper/src/ui/ip_setup.cpp index 04f6d9d..deb6fda 100644 --- a/CYD-Klipper/src/ui/ip_setup.cpp +++ b/CYD-Klipper/src/ui/ip_setup.cpp @@ -139,7 +139,9 @@ void ip_init(){ void ip_ok(){ if (klipper_request_consecutive_fail_count > 5){ + freeze_request_thread(); ip_init(); + unfreeze_request_thread(); klipper_request_consecutive_fail_count = 0; lv_msg_send(DATA_PRINTER_STATE, &printer); }