mirror of
https://github.com/suchmememanyskill/CYD-Klipper.git
synced 2026-03-21 13:43:25 +00:00
@@ -12,6 +12,34 @@ const char *printer_state_messages[] = {
|
|||||||
"Printing"};
|
"Printing"};
|
||||||
|
|
||||||
Printer printer = {0};
|
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)
|
void send_gcode(bool wait, const char *gcode)
|
||||||
{
|
{
|
||||||
@@ -35,10 +63,10 @@ void send_gcode(bool wait, const char *gcode)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
char filename_buff[512] = {0};
|
|
||||||
|
|
||||||
void fetch_printer_data()
|
void fetch_printer_data()
|
||||||
{
|
{
|
||||||
|
bool frozen = true;
|
||||||
|
freeze_request_thread();
|
||||||
char buff[256] = {};
|
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);
|
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;
|
HTTPClient client;
|
||||||
@@ -46,6 +74,7 @@ void fetch_printer_data()
|
|||||||
int httpCode = client.GET();
|
int httpCode = client.GET();
|
||||||
if (httpCode == 200)
|
if (httpCode == 200)
|
||||||
{
|
{
|
||||||
|
klipper_request_consecutive_fail_count = 0;
|
||||||
String payload = client.getString();
|
String payload = client.getString();
|
||||||
DynamicJsonDocument doc(4096);
|
DynamicJsonDocument doc(4096);
|
||||||
deserializeJson(doc, payload);
|
deserializeJson(doc, payload);
|
||||||
@@ -53,6 +82,10 @@ void fetch_printer_data()
|
|||||||
bool emit_state_update = false;
|
bool emit_state_update = false;
|
||||||
int printer_state = printer.state;
|
int printer_state = printer.state;
|
||||||
|
|
||||||
|
unfreeze_request_thread();
|
||||||
|
frozen = false;
|
||||||
|
freeze_render_thread();
|
||||||
|
|
||||||
if (status.containsKey("webhooks"))
|
if (status.containsKey("webhooks"))
|
||||||
{
|
{
|
||||||
const char *state = status["webhooks"]["state"];
|
const char *state = status["webhooks"]["state"];
|
||||||
@@ -156,29 +189,46 @@ void fetch_printer_data()
|
|||||||
printer.state = printer_state;
|
printer.state = printer_state;
|
||||||
lv_msg_send(DATA_PRINTER_STATE, &printer);
|
lv_msg_send(DATA_PRINTER_STATE, &printer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unfreeze_render_thread();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
klipper_request_consecutive_fail_count++;
|
||||||
Serial.printf("Failed to fetch printer data: %d\n", httpCode);
|
Serial.printf("Failed to fetch printer data: %d\n", httpCode);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
long last_data_update = 0;
|
if (frozen)
|
||||||
const long data_update_interval = 1500;
|
unfreeze_request_thread();
|
||||||
|
}
|
||||||
|
|
||||||
void data_loop()
|
void data_loop()
|
||||||
{
|
{
|
||||||
if (millis() - last_data_update < data_update_interval)
|
// Causes other threads that are trying to lock the thread to actually lock it
|
||||||
return;
|
unfreeze_render_thread();
|
||||||
|
delay(1);
|
||||||
|
freeze_render_thread();
|
||||||
|
}
|
||||||
|
|
||||||
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();
|
fetch_printer_data();
|
||||||
|
last_data_update = millis();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TaskHandle_t background_loop;
|
||||||
|
|
||||||
void data_setup()
|
void data_setup()
|
||||||
{
|
{
|
||||||
|
semaphore_init();
|
||||||
printer.print_filename = filename_buff;
|
printer.print_filename = filename_buff;
|
||||||
fetch_printer_data();
|
fetch_printer_data();
|
||||||
macros_query_setup();
|
macros_query_setup();
|
||||||
|
freeze_render_thread();
|
||||||
|
xTaskCreatePinnedToCore(data_loop_background, "data_loop_background", 5000, NULL, 1, &background_loop, 0);
|
||||||
}
|
}
|
||||||
@@ -28,6 +28,7 @@ typedef struct _Printer {
|
|||||||
} Printer;
|
} Printer;
|
||||||
|
|
||||||
extern Printer printer;
|
extern Printer printer;
|
||||||
|
extern int klipper_request_consecutive_fail_count;
|
||||||
|
|
||||||
#define DATA_PRINTER_STATE 1
|
#define DATA_PRINTER_STATE 1
|
||||||
#define DATA_PRINTER_DATA 2
|
#define DATA_PRINTER_DATA 2
|
||||||
@@ -36,3 +37,6 @@ extern Printer printer;
|
|||||||
void data_loop();
|
void data_loop();
|
||||||
void data_setup();
|
void data_setup();
|
||||||
void send_gcode(bool wait, const char* gcode);
|
void send_gcode(bool wait, const char* gcode);
|
||||||
|
|
||||||
|
void freeze_request_thread();
|
||||||
|
void unfreeze_request_thread();
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <list>
|
#include <list>
|
||||||
#include "files_query.h"
|
#include "files_query.h"
|
||||||
#include "../conf/global_config.h"
|
#include "../conf/global_config.h"
|
||||||
|
#include "data_setup.h"
|
||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
#include <HardwareSerial.h>
|
#include <HardwareSerial.h>
|
||||||
@@ -9,6 +10,8 @@
|
|||||||
FILESYSTEM_FILE* last_query = NULL;
|
FILESYSTEM_FILE* last_query = NULL;
|
||||||
|
|
||||||
FILESYSTEM_FILE* get_files(){
|
FILESYSTEM_FILE* get_files(){
|
||||||
|
freeze_request_thread();
|
||||||
|
|
||||||
if (last_query != NULL){
|
if (last_query != NULL){
|
||||||
FILESYSTEM_FILE* current = last_query;
|
FILESYSTEM_FILE* current = last_query;
|
||||||
|
|
||||||
@@ -59,5 +62,6 @@ FILESYSTEM_FILE* get_files(){
|
|||||||
result += 1;
|
result += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unfreeze_request_thread();
|
||||||
return last_query;
|
return last_query;
|
||||||
}
|
}
|
||||||
@@ -34,6 +34,7 @@ void setup() {
|
|||||||
|
|
||||||
void loop(){
|
void loop(){
|
||||||
wifi_ok();
|
wifi_ok();
|
||||||
|
ip_ok();
|
||||||
data_loop();
|
data_loop();
|
||||||
lv_timer_handler();
|
lv_timer_handler();
|
||||||
lv_task_handler();
|
lv_task_handler();
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include "lvgl.h"
|
#include "lvgl.h"
|
||||||
#include <TFT_eSPI.h>
|
#include <TFT_eSPI.h>
|
||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
|
#include "core/data_setup.h"
|
||||||
|
|
||||||
bool connect_ok = false;
|
bool connect_ok = false;
|
||||||
lv_obj_t * ipEntry;
|
lv_obj_t * ipEntry;
|
||||||
@@ -117,6 +118,7 @@ int retry_count = 0;
|
|||||||
|
|
||||||
void ip_init(){
|
void ip_init(){
|
||||||
connect_ok = false;
|
connect_ok = false;
|
||||||
|
retry_count = 0;
|
||||||
|
|
||||||
ip_init_inner();
|
ip_init_inner();
|
||||||
|
|
||||||
@@ -134,3 +136,13 @@ 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +1,2 @@
|
|||||||
void ip_init();
|
void ip_init();
|
||||||
|
void ip_ok();
|
||||||
Reference in New Issue
Block a user