Implement printer switching in ip connect/error screen

This commit is contained in:
suchmememanyskill
2024-03-09 20:47:45 +01:00
parent f98c2eff78
commit 870d109c92
10 changed files with 118 additions and 14 deletions

View File

@@ -313,7 +313,7 @@ void fetch_printer_data_minimal()
delay(10); delay(10);
HTTPClient client; HTTPClient client;
configure_http_client(client, "http://" + String(config->klipper_host) + ":" + String(config->klipper_port) + "/printer/objects/query?webhooks&print_stats&virtual_sdcard", true, 1000); configure_http_client(client, get_full_url("/printer/objects/query?webhooks&print_stats&virtual_sdcard", config), true, 1000);
freeze_request_thread(); freeze_request_thread();
int httpCode = client.GET(); int httpCode = client.GET();
@@ -370,8 +370,6 @@ void fetch_printer_data_minimal()
} }
} }
} }
Serial.printf("Record: %d %f %d\n", data[i].online, data[i].print_progress, data[i].state);
} }
else else
{ {

View File

@@ -1,5 +1,9 @@
#include "http_client.h" #include "http_client.h"
#include "../conf/global_config.h"
String get_full_url(String url_part, PRINTER_CONFIG * config)
{
return "http://" + String(config->klipper_host) + ":" + String(config->klipper_port) + url_part;
}
String get_full_url(String url_part) String get_full_url(String url_part)
{ {

View File

@@ -1,8 +1,10 @@
#pragma once #pragma once
#include <HTTPClient.h> #include <HTTPClient.h>
#include "../conf/global_config.h"
String get_full_url(String url_part); String get_full_url(String url_part);
String get_full_url(String url_part, PRINTER_CONFIG * config);
void configure_http_client(HTTPClient &client, String url, bool stream = true, int timeout = 1000); void configure_http_client(HTTPClient &client, String url, bool stream = true, int timeout = 1000);

View File

@@ -7,6 +7,7 @@
#include "../core/macros_query.h" #include "../core/macros_query.h"
#include "panels/panel.h" #include "panels/panel.h"
#include "../core/http_client.h" #include "../core/http_client.h"
#include "switch_printer.h"
bool connect_ok = false; bool connect_ok = false;
lv_obj_t * hostEntry; lv_obj_t * hostEntry;
@@ -161,6 +162,8 @@ void redraw_connect_screen(){
lv_label_set_text(btn_label, "Power Devices"); lv_label_set_text(btn_label, "Power Devices");
lv_obj_center(btn_label); lv_obj_center(btn_label);
} }
draw_switch_printer_button();
} }
static bool auth_entry_done = false; static bool auth_entry_done = false;

View File

@@ -8,6 +8,7 @@
#include "panels/panel.h" #include "panels/panel.h"
#include "../core/macros_query.h" #include "../core/macros_query.h"
#include "../core/lv_setup.h" #include "../core/lv_setup.h"
#include "switch_printer.h"
char extruder_temp_buff[20]; char extruder_temp_buff[20];
char bed_temp_buff[20]; char bed_temp_buff[20];
@@ -90,6 +91,8 @@ void error_ui(){
lv_label_set_text(label, "Devices"); lv_label_set_text(label, "Devices");
lv_obj_center(label); lv_obj_center(label);
} }
draw_switch_printer_button();
} }
void check_if_screen_needs_to_be_disabled(){ void check_if_screen_needs_to_be_disabled(){

View File

@@ -173,25 +173,19 @@ void settings_panel_init(lv_obj_t* panel){
lv_create_custom_menu_switch("Multi Printer Mode", panel, multi_printer_switch, global_config.multi_printer_mode); lv_create_custom_menu_switch("Multi Printer Mode", panel, multi_printer_switch, global_config.multi_printer_mode);
lv_create_custom_menu_switch("Auto Update", panel, auto_ota_update_switch, global_config.auto_ota_update); lv_create_custom_menu_switch("Auto Update", panel, auto_ota_update_switch, global_config.auto_ota_update);
lv_create_custom_menu_label("Version", panel, REPO_VERSION " ");
lv_obj_t *label = lv_label_create(panel);
lv_label_set_text(label, REPO_VERSION " ");
lv_create_custom_menu_entry("Version", label, panel, false);
if (ota_has_update()){ if (ota_has_update()){
lv_obj_t *btn = lv_btn_create(panel); lv_obj_t *btn = lv_btn_create(panel);
lv_obj_add_event_cb(btn, btn_ota_do_update, LV_EVENT_CLICKED, NULL); lv_obj_add_event_cb(btn, btn_ota_do_update, LV_EVENT_CLICKED, NULL);
label = lv_label_create(btn); lv_obj_t *label = lv_label_create(btn);
lv_label_set_text_fmt(label, "Update to %s", ota_new_version_name().c_str()); lv_label_set_text_fmt(label, "Update to %s", ota_new_version_name().c_str());
lv_obj_center(label); lv_obj_center(label);
lv_create_custom_menu_entry("Device", btn, panel); lv_create_custom_menu_entry("Device", btn, panel);
} }
else { else {
label = lv_label_create(panel); lv_create_custom_menu_label("Device", panel, ARDUINO_BOARD " ");
lv_label_set_text(label, ARDUINO_BOARD " ");
lv_create_custom_menu_entry("Device", label, panel, false);
} }
} }

View File

@@ -0,0 +1,88 @@
#include "switch_printer.h"
#include "lvgl.h"
#include "../conf/global_config.h"
#include "ui_utils.h"
#include "../core/http_client.h"
#include "../core/lv_setup.h"
#include "../core/macros_query.h"
static void btn_switch_printer(lv_event_t *e){
lv_obj_t *btn = lv_event_get_target(e);
PRINTER_CONFIG * config = (PRINTER_CONFIG*)lv_event_get_user_data(e);
int index = config - global_config.printer_config;
set_printer_config_index(index);
set_color_scheme();
_macros_query_internal();
_power_devices_query_internal();
lv_obj_del(lv_obj_get_parent(lv_obj_get_parent(btn)));
}
void switch_printer_init() {
lv_obj_t * parent = lv_create_empty_panel(lv_scr_act());
lv_obj_set_style_bg_opa(parent, LV_OPA_100, 0);
lv_obj_align(parent, LV_ALIGN_TOP_RIGHT, 0, 0);
lv_obj_set_size(parent, CYD_SCREEN_WIDTH_PX, CYD_SCREEN_HEIGHT_PX);
lv_layout_flex_column(parent);
lv_obj_set_size(lv_create_empty_panel(parent), 0, 0);
auto width = CYD_SCREEN_WIDTH_PX - CYD_SCREEN_GAP_PX * 2;
lv_obj_t * btn = lv_btn_create(parent);
lv_obj_set_size(btn, width, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX);
lv_obj_add_event_cb(btn, destroy_event_user_data, LV_EVENT_CLICKED, parent);
lv_obj_t * label = lv_label_create(btn);
lv_label_set_text(label, LV_SYMBOL_CLOSE " Close");
lv_obj_center(label);
for (int i = 0; i < PRINTER_CONFIG_COUNT; i++){
PRINTER_CONFIG * config = &global_config.printer_config[i];
const char* printer_name = (config->printer_name[0] == 0) ? config->klipper_host : config->printer_name;
if (config == get_current_printer_config())
{
lv_create_custom_menu_label(printer_name, parent, "Active");
continue;
}
if (config->ip_configured) {
HTTPClient client;
configure_http_client(client, get_full_url("/printer/objects/query?webhooks&print_stats&virtual_sdcard", config), true, 1000);
int httpCode = client.GET();
if (httpCode == 200)
{
lv_create_custom_menu_button(printer_name, parent, btn_switch_printer, "Switch", config);
}
else
{
lv_create_custom_menu_label(printer_name, parent, "Offline");
}
}
}
}
static void show_switch_printer_screen(lv_event_t * e){
switch_printer_init();
}
void draw_switch_printer_button()
{
if (!global_config.multi_printer_mode)
{
return;
}
lv_obj_t * btn = lv_btn_create(lv_scr_act());
lv_obj_set_size(btn, CYD_SCREEN_MIN_BUTTON_WIDTH_PX, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX);
lv_obj_align(btn, LV_ALIGN_TOP_RIGHT, -CYD_SCREEN_GAP_PX, CYD_SCREEN_GAP_PX);
lv_obj_add_event_cb(btn, show_switch_printer_screen, LV_EVENT_CLICKED, NULL);
lv_obj_t * label = lv_label_create(btn);
lv_label_set_text(label, LV_SYMBOL_HOME);
lv_obj_center(label);
}

View File

@@ -0,0 +1,4 @@
#pragma once
void switch_printer_init();
void draw_switch_printer_button();

View File

@@ -192,4 +192,11 @@ void lv_create_custom_menu_dropdown(const char *label_text, lv_obj_t *root_panel
lv_obj_add_event_cb(dropdown, on_change, LV_EVENT_VALUE_CHANGED, user_data); lv_obj_add_event_cb(dropdown, on_change, LV_EVENT_VALUE_CHANGED, user_data);
lv_create_custom_menu_entry(label_text, dropdown, root_panel, true); lv_create_custom_menu_entry(label_text, dropdown, root_panel, true);
}
void lv_create_custom_menu_label(const char *label_text, lv_obj_t* root_panel, const char *text)
{
lv_obj_t * label = lv_label_create(lv_scr_act());
lv_label_set_text(label, text);
lv_create_custom_menu_entry(label_text, label, root_panel, false);
} }

View File

@@ -42,4 +42,5 @@ void lv_create_keyboard_text_entry(lv_event_cb_t keyboard_callback, lv_keyboard_
void lv_create_custom_menu_entry(const char* label_text, lv_obj_t* object, lv_obj_t* root_panel, bool set_height = true); void lv_create_custom_menu_entry(const char* label_text, lv_obj_t* object, lv_obj_t* root_panel, bool set_height = true);
void lv_create_custom_menu_button(const char *label_text, lv_obj_t* root_panel, lv_event_cb_t on_click, const char *btn_text, void * user_data = NULL); void lv_create_custom_menu_button(const char *label_text, lv_obj_t* root_panel, lv_event_cb_t on_click, const char *btn_text, void * user_data = NULL);
void lv_create_custom_menu_switch(const char *label_text, lv_obj_t* root_panel, lv_event_cb_t on_toggle, bool state, void * user_data = NULL); void lv_create_custom_menu_switch(const char *label_text, lv_obj_t* root_panel, lv_event_cb_t on_toggle, bool state, void * user_data = NULL);
void lv_create_custom_menu_dropdown(const char *label_text, lv_obj_t *root_panel, lv_event_cb_t on_change, const char *options, int index, void * user_data = NULL); void lv_create_custom_menu_dropdown(const char *label_text, lv_obj_t *root_panel, lv_event_cb_t on_change, const char *options, int index, void * user_data = NULL);
void lv_create_custom_menu_label(const char *label_text, lv_obj_t* root_panel, const char *text);