mirror of
https://github.com/suchmememanyskill/CYD-Klipper.git
synced 2026-03-21 05:33:24 +00:00
Rework network loop to make one request
This commit is contained in:
@@ -8,99 +8,95 @@
|
|||||||
const char *printer_state_messages[] = {
|
const char *printer_state_messages[] = {
|
||||||
"Error",
|
"Error",
|
||||||
"Idle",
|
"Idle",
|
||||||
"Printing"
|
"Printing"};
|
||||||
};
|
|
||||||
|
|
||||||
Printer printer = {0};
|
Printer printer = {0};
|
||||||
|
|
||||||
void send_gcode(bool wait, const char* gcode){
|
void send_gcode(bool wait, const char *gcode)
|
||||||
|
{
|
||||||
char buff[256] = {};
|
char buff[256] = {};
|
||||||
sprintf(buff, "http://%s:%d/printer/gcode/script?script=%s", global_config.klipperHost, global_config.klipperPort, gcode);
|
sprintf(buff, "http://%s:%d/printer/gcode/script?script=%s", global_config.klipperHost, global_config.klipperPort, gcode);
|
||||||
HTTPClient client;
|
HTTPClient client;
|
||||||
client.begin(buff);
|
client.begin(buff);
|
||||||
|
|
||||||
if (!wait){
|
if (!wait)
|
||||||
|
{
|
||||||
client.setTimeout(1000);
|
client.setTimeout(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
client.GET();
|
client.GET();
|
||||||
}
|
}
|
||||||
catch (...){
|
catch (...)
|
||||||
|
{
|
||||||
Serial.println("Failed to send gcode");
|
Serial.println("Failed to send gcode");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Merge with other request, see https://moonraker.readthedocs.io/en/latest/printer_objects/#webhooks
|
|
||||||
void fetch_printer_state(){
|
|
||||||
char buff[91] = {};
|
|
||||||
sprintf(buff, "http://%s:%d/printer/info", global_config.klipperHost, global_config.klipperPort);
|
|
||||||
HTTPClient client;
|
|
||||||
client.begin(buff);
|
|
||||||
int httpCode = client.GET();
|
|
||||||
if (httpCode == 200) {
|
|
||||||
String payload = client.getString();
|
|
||||||
DynamicJsonDocument doc(1024);
|
|
||||||
deserializeJson(doc, payload);
|
|
||||||
auto result = doc["result"];
|
|
||||||
|
|
||||||
bool update = false;
|
|
||||||
const char* state = result["state"];
|
|
||||||
|
|
||||||
if (strcmp(state, "ready") == 0 && printer.state == PRINTER_STATE_ERROR){
|
|
||||||
printer.state = PRINTER_STATE_IDLE;
|
|
||||||
update = true;
|
|
||||||
}
|
|
||||||
else if (strcmp(state, "shutdown") == 0 && printer.state != PRINTER_STATE_ERROR) {
|
|
||||||
printer.state = PRINTER_STATE_ERROR;
|
|
||||||
update = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const char* message = result["state_message"];
|
|
||||||
if (printer.state_message == NULL || strcmp(printer.state_message, message)) {
|
|
||||||
if (printer.state_message != NULL){
|
|
||||||
free(printer.state_message);
|
|
||||||
}
|
|
||||||
|
|
||||||
printer.state_message = (char*)malloc(strlen(message) + 1);
|
|
||||||
strcpy(printer.state_message, message);
|
|
||||||
update = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (update)
|
|
||||||
lv_msg_send(DATA_PRINTER_STATE, &printer);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
Serial.printf("Failed to fetch printer state: %d\n", httpCode);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
char filename_buff[512] = {0};
|
char filename_buff[512] = {0};
|
||||||
|
|
||||||
void fetch_printer_data(){
|
void fetch_printer_data()
|
||||||
|
{
|
||||||
char buff[256] = {};
|
char buff[256] = {};
|
||||||
sprintf(buff, "http://%s:%d/printer/objects/query?extruder&heater_bed&toolhead&gcode_move&virtual_sdcard&print_stats", 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;
|
||||||
client.begin(buff);
|
client.begin(buff);
|
||||||
int httpCode = client.GET();
|
int httpCode = client.GET();
|
||||||
if (httpCode == 200) {
|
if (httpCode == 200)
|
||||||
|
{
|
||||||
String payload = client.getString();
|
String payload = client.getString();
|
||||||
DynamicJsonDocument doc(4096);
|
DynamicJsonDocument doc(4096);
|
||||||
deserializeJson(doc, payload);
|
deserializeJson(doc, payload);
|
||||||
auto status = doc["result"]["status"];
|
auto status = doc["result"]["status"];
|
||||||
if (status.containsKey("extruder")){
|
bool emit_state_update = false;
|
||||||
|
int printer_state = printer.state;
|
||||||
|
|
||||||
|
if (status.containsKey("webhooks"))
|
||||||
|
{
|
||||||
|
const char *state = status["webhooks"]["state"];
|
||||||
|
const char *message = status["webhooks"]["state_message"];
|
||||||
|
|
||||||
|
if (strcmp(state, "ready") == 0 && printer.state == PRINTER_STATE_ERROR)
|
||||||
|
{
|
||||||
|
printer_state = PRINTER_STATE_IDLE;
|
||||||
|
}
|
||||||
|
else if (strcmp(state, "shutdown") == 0 && printer.state != PRINTER_STATE_ERROR)
|
||||||
|
{
|
||||||
|
printer_state = PRINTER_STATE_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (printer.state_message == NULL || strcmp(printer.state_message, message))
|
||||||
|
{
|
||||||
|
if (printer.state_message != NULL)
|
||||||
|
{
|
||||||
|
free(printer.state_message);
|
||||||
|
}
|
||||||
|
|
||||||
|
printer.state_message = (char *)malloc(strlen(message) + 1);
|
||||||
|
strcpy(printer.state_message, message);
|
||||||
|
emit_state_update = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (printer_state != PRINTER_STATE_ERROR)
|
||||||
|
{
|
||||||
|
if (status.containsKey("extruder"))
|
||||||
|
{
|
||||||
printer.extruder_temp = status["extruder"]["temperature"];
|
printer.extruder_temp = status["extruder"]["temperature"];
|
||||||
printer.extruder_target_temp = status["extruder"]["target"];
|
printer.extruder_target_temp = status["extruder"]["target"];
|
||||||
bool can_extrude = status["extruder"]["can_extrude"];
|
bool can_extrude = status["extruder"]["can_extrude"];
|
||||||
printer.can_extrude = can_extrude == true;
|
printer.can_extrude = can_extrude == true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status.containsKey("heater_bed")){
|
if (status.containsKey("heater_bed"))
|
||||||
|
{
|
||||||
printer.bed_temp = status["heater_bed"]["temperature"];
|
printer.bed_temp = status["heater_bed"]["temperature"];
|
||||||
printer.bed_target_temp = status["heater_bed"]["target"];
|
printer.bed_target_temp = status["heater_bed"]["target"];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status.containsKey("toolhead")){
|
if (status.containsKey("toolhead"))
|
||||||
|
{
|
||||||
printer.position[0] = status["toolhead"]["position"][0];
|
printer.position[0] = status["toolhead"]["position"][0];
|
||||||
printer.position[1] = status["toolhead"]["position"][1];
|
printer.position[1] = status["toolhead"]["position"][1];
|
||||||
printer.position[2] = status["toolhead"]["position"][2];
|
printer.position[2] = status["toolhead"]["position"][2];
|
||||||
@@ -108,18 +104,19 @@ void fetch_printer_data(){
|
|||||||
printer.homed_axis = strcmp(homed_axis, "xyz") == 0;
|
printer.homed_axis = strcmp(homed_axis, "xyz") == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status.containsKey("gcode_move")){
|
if (status.containsKey("gcode_move"))
|
||||||
|
{
|
||||||
bool absolute_coords = status["gcode_move"]["absolute_coordinates"];
|
bool absolute_coords = status["gcode_move"]["absolute_coordinates"];
|
||||||
printer.absolute_coords = absolute_coords == true;
|
printer.absolute_coords = absolute_coords == true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (status.containsKey("virtual_sdcard")){
|
if (status.containsKey("virtual_sdcard"))
|
||||||
|
{
|
||||||
printer.print_progress = status["virtual_sdcard"]["progress"];
|
printer.print_progress = status["virtual_sdcard"]["progress"];
|
||||||
}
|
}
|
||||||
|
|
||||||
int printer_state = printer.state;
|
if (status.containsKey("print_stats"))
|
||||||
|
{
|
||||||
if (status.containsKey("print_stats")){
|
|
||||||
const char *filename = status["print_stats"]["filename"];
|
const char *filename = status["print_stats"]["filename"];
|
||||||
strcpy(filename_buff, filename);
|
strcpy(filename_buff, filename);
|
||||||
printer.print_filename = filename_buff;
|
printer.print_filename = filename_buff;
|
||||||
@@ -128,13 +125,16 @@ void fetch_printer_data(){
|
|||||||
|
|
||||||
const char *state = status["print_stats"]["state"];
|
const char *state = status["print_stats"]["state"];
|
||||||
|
|
||||||
if (strcmp(state, "printing") == 0){
|
if (strcmp(state, "printing") == 0)
|
||||||
|
{
|
||||||
printer_state = PRINTER_STATE_PRINTING;
|
printer_state = PRINTER_STATE_PRINTING;
|
||||||
}
|
}
|
||||||
else if (strcmp(state, "paused") == 0){
|
else if (strcmp(state, "paused") == 0)
|
||||||
|
{
|
||||||
printer_state = PRINTER_STATE_PAUSED;
|
printer_state = PRINTER_STATE_PAUSED;
|
||||||
}
|
}
|
||||||
else if (strcmp(state, "complete") == 0 || strcmp(state, "cancelled") == 0 || strcmp(state, "standby") == 0){
|
else if (strcmp(state, "complete") == 0 || strcmp(state, "cancelled") == 0 || strcmp(state, "standby") == 0)
|
||||||
|
{
|
||||||
printer_state = PRINTER_STATE_IDLE;
|
printer_state = PRINTER_STATE_IDLE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -142,18 +142,22 @@ void fetch_printer_data(){
|
|||||||
// TODO: make a call to /server/files/metadata to get more accurate time estimates
|
// TODO: make a call to /server/files/metadata to get more accurate time estimates
|
||||||
// https://moonraker.readthedocs.io/en/latest/web_api/#server-administration
|
// https://moonraker.readthedocs.io/en/latest/web_api/#server-administration
|
||||||
|
|
||||||
if (printer.state == PRINTER_STATE_PRINTING && printer.print_progress > 0){
|
if (printer.state == PRINTER_STATE_PRINTING && printer.print_progress > 0)
|
||||||
|
{
|
||||||
printer.remaining_time_s = (printer.elapsed_time_s / printer.print_progress) - printer.elapsed_time_s;
|
printer.remaining_time_s = (printer.elapsed_time_s / printer.print_progress) - printer.elapsed_time_s;
|
||||||
}
|
}
|
||||||
|
|
||||||
lv_msg_send(DATA_PRINTER_DATA, &printer);
|
lv_msg_send(DATA_PRINTER_DATA, &printer);
|
||||||
|
}
|
||||||
|
|
||||||
if (printer.state != printer_state){
|
if (printer.state != printer_state || emit_state_update)
|
||||||
|
{
|
||||||
printer.state = printer_state;
|
printer.state = printer_state;
|
||||||
lv_msg_send(DATA_PRINTER_STATE, &printer);
|
lv_msg_send(DATA_PRINTER_STATE, &printer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
Serial.printf("Failed to fetch printer data: %d\n", httpCode);
|
Serial.printf("Failed to fetch printer data: %d\n", httpCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -161,19 +165,18 @@ void fetch_printer_data(){
|
|||||||
long last_data_update = 0;
|
long last_data_update = 0;
|
||||||
const long data_update_interval = 1500;
|
const long data_update_interval = 1500;
|
||||||
|
|
||||||
void data_loop(){
|
void data_loop()
|
||||||
|
{
|
||||||
if (millis() - last_data_update < data_update_interval)
|
if (millis() - last_data_update < data_update_interval)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
last_data_update = millis();
|
last_data_update = millis();
|
||||||
|
|
||||||
fetch_printer_state();
|
|
||||||
if (printer.state != PRINTER_STATE_ERROR)
|
|
||||||
fetch_printer_data();
|
fetch_printer_data();
|
||||||
}
|
}
|
||||||
|
|
||||||
void data_setup(){
|
void data_setup()
|
||||||
|
{
|
||||||
printer.print_filename = filename_buff;
|
printer.print_filename = filename_buff;
|
||||||
fetch_printer_state();
|
|
||||||
fetch_printer_data();
|
fetch_printer_data();
|
||||||
}
|
}
|
||||||
@@ -44,19 +44,15 @@ static void update_printer_data_time(lv_event_t * e){
|
|||||||
unsigned long minutes = (time % 3600) / 60;
|
unsigned long minutes = (time % 3600) / 60;
|
||||||
unsigned long seconds = (time % 3600) % 60;
|
unsigned long seconds = (time % 3600) % 60;
|
||||||
|
|
||||||
if (hours > 99){
|
if (hours >= 10){
|
||||||
lv_label_set_text(label, ">99h");
|
sprintf(time_buffer, "%luh", hours);
|
||||||
return;
|
} else if (hours >= 1){
|
||||||
}
|
sprintf(time_buffer, "%luh%02lum", hours, minutes);
|
||||||
|
|
||||||
if (hours >= 1){
|
|
||||||
sprintf(time_buffer, "%02luh%02lum", hours, minutes);
|
|
||||||
} else {
|
} else {
|
||||||
sprintf(time_buffer, "%02lum%02lus", minutes, seconds);
|
sprintf(time_buffer, "%02lum", minutes);
|
||||||
}
|
}
|
||||||
|
|
||||||
lv_label_set_text(label, time_buffer);
|
lv_label_set_text(label, time_buffer);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void btn_click_files(lv_event_t * e){
|
static void btn_click_files(lv_event_t * e){
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ static void stepper_state_update(lv_event_t * e){
|
|||||||
lv_label_set_text(label, printer.homed_axis ? LV_SYMBOL_HOME " Steppers locked" : LV_SYMBOL_EYE_CLOSE " Steppers unlocked");
|
lv_label_set_text(label, printer.homed_axis ? LV_SYMBOL_HOME " Steppers locked" : LV_SYMBOL_EYE_CLOSE " Steppers unlocked");
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_panel_data(lv_obj_t* panel){
|
void move_panel_init(lv_obj_t* panel){
|
||||||
lv_obj_clear_flag(panel, LV_OBJ_FLAG_SCROLLABLE);
|
lv_obj_clear_flag(panel, LV_OBJ_FLAG_SCROLLABLE);
|
||||||
const int button_size = 40;
|
const int button_size = 40;
|
||||||
const int button_size_vertical = 40;
|
const int button_size_vertical = 40;
|
||||||
@@ -205,11 +205,5 @@ void move_panel_data(lv_obj_t* panel){
|
|||||||
y_pos += 60;
|
y_pos += 60;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lv_msg_send(DATA_PRINTER_DATA, &printer);
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void move_panel_init(lv_obj_t* panel){
|
|
||||||
move_panel_data(panel);
|
|
||||||
}
|
}
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
#include "../../core/data_setup.h"
|
#include "../../core/data_setup.h"
|
||||||
|
|
||||||
void print_panel_init(lv_obj_t* panel){
|
void print_panel_init(lv_obj_t* panel){
|
||||||
if (printer.state == PRINTER_STATE_PRINTING || printer.state == PRINTER_STATE_PAUSED || true){
|
if (printer.state == PRINTER_STATE_PRINTING || printer.state == PRINTER_STATE_PAUSED){
|
||||||
progress_panel_init(panel);
|
progress_panel_init(panel);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,15 +35,15 @@ static void update_printer_data_percentage(lv_event_t * e){
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void btn_click_stop(lv_event_t * e){
|
static void btn_click_stop(lv_event_t * e){
|
||||||
// TODO: Implement
|
send_gcode(true, "CANCEL_PRINT");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void btn_click_pause(lv_event_t * e){
|
static void btn_click_pause(lv_event_t * e){
|
||||||
// TODO: Implement
|
send_gcode(true, "PAUSE");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void btn_click_resume(lv_event_t * e){
|
static void btn_click_resume(lv_event_t * e){
|
||||||
// TODO: Implement
|
send_gcode(true, "RESUME");
|
||||||
}
|
}
|
||||||
|
|
||||||
void progress_panel_init(lv_obj_t* panel){
|
void progress_panel_init(lv_obj_t* panel){
|
||||||
@@ -117,4 +117,6 @@ void progress_panel_init(lv_obj_t* panel){
|
|||||||
lv_label_set_text(label, LV_SYMBOL_PAUSE);
|
lv_label_set_text(label, LV_SYMBOL_PAUSE);
|
||||||
lv_obj_center(label);
|
lv_obj_center(label);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lv_msg_send(DATA_PRINTER_DATA, &printer);
|
||||||
}
|
}
|
||||||
@@ -79,16 +79,28 @@ static void show_keyboard_with_bed(lv_event_t * e){
|
|||||||
}
|
}
|
||||||
|
|
||||||
static void cooldown_temp(lv_event_t * e){
|
static void cooldown_temp(lv_event_t * e){
|
||||||
|
if (printer.state == PRINTER_STATE_PRINTING){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
send_gcode(true, "M104%20S0");
|
send_gcode(true, "M104%20S0");
|
||||||
send_gcode(true, "M140%20S0");
|
send_gcode(true, "M140%20S0");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void btn_extrude(lv_event_t * e){
|
static void btn_extrude(lv_event_t * e){
|
||||||
|
if (printer.state == PRINTER_STATE_PRINTING){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
send_gcode(true, "M83");
|
send_gcode(true, "M83");
|
||||||
send_gcode(true, "G1%20E25%20F300");
|
send_gcode(true, "G1%20E25%20F300");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void btn_retract(lv_event_t * e){
|
static void btn_retract(lv_event_t * e){
|
||||||
|
if (printer.state == PRINTER_STATE_PRINTING){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
send_gcode(true, "M83");
|
send_gcode(true, "M83");
|
||||||
send_gcode(true, "G1%20E-25%20F300");
|
send_gcode(true, "G1%20E-25%20F300");
|
||||||
}
|
}
|
||||||
@@ -148,4 +160,6 @@ void temp_panel_init(lv_obj_t* panel){
|
|||||||
label = lv_label_create(btn);
|
label = lv_label_create(btn);
|
||||||
lv_label_set_text(label, LV_SYMBOL_UP " Retract");
|
lv_label_set_text(label, LV_SYMBOL_UP " Retract");
|
||||||
lv_obj_center(label);
|
lv_obj_center(label);
|
||||||
|
|
||||||
|
lv_msg_send(DATA_PRINTER_DATA, &printer);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user