Move ip connect to panel; add restart to wifi

This commit is contained in:
suchmememanyskill
2024-03-20 22:24:06 +01:00
parent 6cde9cb887
commit c7c6b26730
13 changed files with 161 additions and 161 deletions

View File

@@ -10,14 +10,9 @@
#include "../ui/ui_utils.h"
#include "macros_query.h"
const char *printer_state_messages[] = {
"Error",
"Idle",
"Printing"};
Printer printer = {0};
PrinterMinimal *printer_minimal;
int klipper_request_consecutive_fail_count = 0;
int klipper_request_consecutive_fail_count = 999;
char filename_buff[512] = {0};
SemaphoreHandle_t freezeRenderThreadSemaphore, freezeRequestThreadSemaphore;
const long data_update_interval = 780;
@@ -120,12 +115,19 @@ void fetch_printer_data()
delay(10);
if (httpCode == 200)
{
int printer_state = printer.state;
if (printer.state == PRINTER_STATE_OFFLINE)
{
printer.state = PRINTER_STATE_ERROR;
}
klipper_request_consecutive_fail_count = 0;
JsonDocument doc;
deserializeJson(doc, client.getStream());
auto status = doc["result"]["status"];
bool emit_state_update = false;
int printer_state = printer.state;
delay(10);
unfreeze_request_thread();
freeze_render_thread();
@@ -298,6 +300,13 @@ void fetch_printer_data()
else
{
klipper_request_consecutive_fail_count++;
if (klipper_request_consecutive_fail_count == 5)
{
printer.state = PRINTER_STATE_OFFLINE;
lv_msg_send(DATA_PRINTER_STATE, &printer);
}
Serial.printf("Failed to fetch printer data: %d\n", httpCode);
unfreeze_request_thread();
}
@@ -312,7 +321,7 @@ void fetch_printer_data_minimal()
if (!config->ip_configured)
{
data[i].online = false;
data[i].state = PRINTER_STATE_OFFLINE;
continue;
}
@@ -325,7 +334,11 @@ void fetch_printer_data_minimal()
delay(10);
if (httpCode == 200)
{
data[i].online = true;
if (data[i].state == PRINTER_STATE_OFFLINE)
{
data[i].state = PRINTER_STATE_ERROR;
}
data[i].power_devices = 0;
JsonDocument doc;
deserializeJson(doc, client.getStream());
@@ -379,7 +392,7 @@ void fetch_printer_data_minimal()
}
else
{
data[i].online = false;
data[i].state = PRINTER_STATE_OFFLINE;
data[i].power_devices = power_devices_count(config);
unfreeze_request_thread();
}

View File

@@ -1,14 +1,13 @@
#pragma once
enum {
PRINTER_STATE_ERROR = 0,
PRINTER_STATE_IDLE = 1,
PRINTER_STATE_PRINTING = 2,
PRINTER_STATE_PAUSED = 3,
PRINTER_STATE_OFFLINE = 0,
PRINTER_STATE_ERROR = 1,
PRINTER_STATE_IDLE = 2,
PRINTER_STATE_PRINTING = 3,
PRINTER_STATE_PAUSED = 4,
};
extern const char* printer_state_messages[];
typedef struct _Printer {
unsigned char state;
char* state_message;
@@ -38,7 +37,6 @@ typedef struct _Printer {
} Printer;
typedef struct _PrinterMinimal {
bool online;
unsigned char state;
float print_progress; // 0 -> 1
unsigned int power_devices;

View File

@@ -29,7 +29,6 @@ void setup() {
void loop(){
wifi_ok();
ip_ok();
data_loop();
lv_timer_handler();
lv_task_handler();

View File

@@ -10,12 +10,13 @@
#include "switch_printer.h"
#include "macros.h"
bool connect_ok = false;
int prev_power_device_count = 0;
lv_obj_t * hostEntry;
lv_obj_t * portEntry;
lv_obj_t * label = NULL;
void show_ip_entry();
void show_auth_entry();
/* Create a custom keyboard to allow hostnames or ip addresses (a-z, 0 - 9, and -) */
static const char * kb_map[] = {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", LV_SYMBOL_BACKSPACE, "\n",
@@ -31,8 +32,6 @@ static const lv_btnmatrix_ctrl_t kb_ctrl[] = {
LV_KEYBOARD_CTRL_BTN_FLAGS | 6, 4, 4, 4, 4, 4, 4, 4, 4, 4, LV_KEYBOARD_CTRL_BTN_FLAGS | 6
};
void ip_init_inner();
enum connection_status_t {
CONNECT_FAIL = 0,
CONNECT_OK = 1,
@@ -57,7 +56,7 @@ connection_status_t verify_ip(){
}
}
static void ta_event_cb(lv_event_t * e) {
static void keyboard_event_ip_entry(lv_event_t * e) {
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * ta = lv_event_get_target(e);
lv_obj_t * kb = (lv_obj_t *)lv_event_get_user_data(e);
@@ -80,13 +79,10 @@ static void ta_event_cb(lv_event_t * e) {
{
get_current_printer_config()->ip_configured = true;
write_global_config();
connect_ok = true;
}
else if (status == CONNECT_AUTH_REQUIRED)
{
label = NULL;
get_current_printer_config()->ip_configured = true;
write_global_config();
show_auth_entry();
}
else
{
@@ -108,54 +104,6 @@ static void ta_event_cb(lv_event_t * e) {
}
}
static void reset_btn_event_handler(lv_event_t * e){
lv_event_code_t code = lv_event_get_code(e);
if(code == LV_EVENT_CLICKED) {
get_current_printer_config()->ip_configured = false;
ip_init_inner();
}
}
static void power_devices_button(lv_event_t * e) {
macros_draw_power_fullscreen();
}
void redraw_connect_screen(){
lv_obj_clean(lv_scr_act());
label = lv_label_create(lv_scr_act());
lv_label_set_text(label, "Connecting to Klipper");
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
lv_obj_t * button_row = lv_create_empty_panel(lv_scr_act());
lv_obj_set_size(button_row, CYD_SCREEN_WIDTH_PX, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX);
lv_layout_flex_row(button_row, LV_FLEX_ALIGN_CENTER);
lv_obj_align(button_row, LV_ALIGN_CENTER, 0, CYD_SCREEN_GAP_PX + CYD_SCREEN_MIN_BUTTON_HEIGHT_PX);
lv_obj_t * reset_btn = lv_btn_create(button_row);
lv_obj_add_event_cb(reset_btn, reset_btn_event_handler, LV_EVENT_CLICKED, NULL);
lv_obj_set_height(reset_btn, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX);
lv_obj_t * btn_label = lv_label_create(reset_btn);
lv_label_set_text(btn_label, "Reset");
lv_obj_center(btn_label);
if (prev_power_device_count >= 1){
lv_obj_t * power_devices_btn = lv_btn_create(button_row);
lv_obj_add_event_cb(power_devices_btn, power_devices_button, LV_EVENT_CLICKED, NULL);
lv_obj_set_height(power_devices_btn, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX);
btn_label = lv_label_create(power_devices_btn);
lv_label_set_text(btn_label, "Power Devices");
lv_obj_center(btn_label);
}
draw_switch_printer_button();
}
static bool auth_entry_done = false;
static void keyboard_event_auth_entry(lv_event_t * e) {
lv_event_code_t code = lv_event_get_code(e);
lv_obj_t * ta = lv_event_get_target(e);
@@ -169,18 +117,26 @@ static void keyboard_event_auth_entry(lv_event_t * e) {
{
get_current_printer_config()->auth_configured = true;
strcpy(get_current_printer_config()->klipper_auth, txt);
write_global_config();
auth_entry_done = true;
if (verify_ip() == CONNECT_OK)
{
get_current_printer_config()->ip_configured = true;
write_global_config();
}
else
{
lv_label_set_text(label, "Failed to connect");
}
}
}
else if (code == LV_EVENT_CANCEL)
{
auth_entry_done = true;
show_ip_entry();
}
}
void handle_auth_entry(){
auth_entry_done = false;
void show_auth_entry()
{
get_current_printer_config()->klipper_auth[32] = 0;
lv_obj_clean(lv_scr_act());
@@ -194,7 +150,7 @@ void handle_auth_entry(){
lv_obj_set_flex_grow(top_root, 1);
lv_obj_set_style_pad_all(top_root, CYD_SCREEN_GAP_PX, 0);
lv_obj_t * label = lv_label_create(top_root);
label = lv_label_create(top_root);
lv_label_set_text(label, "Enter API Key");
lv_obj_set_width(label, CYD_SCREEN_WIDTH_PX - CYD_SCREEN_GAP_PX * 2);
@@ -212,25 +168,13 @@ void handle_auth_entry(){
lv_obj_add_event_cb(passEntry, keyboard_event_auth_entry, LV_EVENT_ALL, keyboard);
lv_obj_set_flex_grow(passEntry, 1);
lv_keyboard_set_textarea(keyboard, passEntry);
lv_keyboard_set_map(keyboard, LV_KEYBOARD_MODE_USER_1, kb_map, kb_ctrl);
lv_keyboard_set_mode(keyboard, LV_KEYBOARD_MODE_USER_1);
while (!auth_entry_done) {
lv_timer_handler();
lv_task_handler();
}
redraw_connect_screen();
}
void ip_init_inner(){
if (get_current_printer_config()->ip_configured) {
redraw_connect_screen();
return;
}
void show_ip_entry()
{
lv_obj_clean(lv_scr_act());
lv_obj_t * root = lv_create_empty_panel(lv_scr_act());
@@ -267,8 +211,8 @@ void ip_init_inner(){
lv_obj_t * keyboard = lv_keyboard_create(root);
lv_keyboard_set_map(keyboard, LV_KEYBOARD_MODE_USER_1, kb_map, kb_ctrl);
lv_obj_add_event_cb(hostEntry, ta_event_cb, LV_EVENT_ALL, keyboard);
lv_obj_add_event_cb(portEntry, ta_event_cb, LV_EVENT_ALL, keyboard);
lv_obj_add_event_cb(hostEntry, keyboard_event_ip_entry, LV_EVENT_ALL, keyboard);
lv_obj_add_event_cb(portEntry, keyboard_event_ip_entry, LV_EVENT_ALL, keyboard);
lv_keyboard_set_mode(keyboard, LV_KEYBOARD_MODE_USER_1);
lv_keyboard_set_textarea(keyboard, hostEntry);
@@ -280,51 +224,15 @@ void ip_init_inner(){
}
}
long last_data_update_ip = -10000;
const long data_update_interval_ip = 10000;
int retry_count = 0;
void ip_init(){
connect_ok = false;
retry_count = 0;
prev_power_device_count = 0;
if (!get_current_printer_config()->ip_configured)
{
show_ip_entry();
}
ip_init_inner();
while (!connect_ok)
while (!get_current_printer_config()->ip_configured)
{
lv_timer_handler();
lv_task_handler();
if (!connect_ok && get_current_printer_config()->ip_configured && (millis() - last_data_update_ip) > data_update_interval_ip){
connection_status_t status = verify_ip();
connect_ok = status == CONNECT_OK;
last_data_update_ip = millis();
retry_count++;
if (label != NULL){
String retry_count_text = "Connecting to Klipper (Try " + String(retry_count + 1) + ")";
lv_label_set_text(label, retry_count_text.c_str());
}
if (status == CONNECT_AUTH_REQUIRED)
handle_auth_entry();
unsigned int power_device_count = power_devices_count();
if (power_device_count != prev_power_device_count) {
prev_power_device_count = power_device_count;
redraw_connect_screen();
}
}
}
}
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);
}
}

View File

@@ -1,2 +1,3 @@
#pragma once
void ip_init();
void ip_ok();

View File

@@ -24,7 +24,10 @@ void check_if_screen_needs_to_be_disabled(){
static void on_state_change(void * s, lv_msg_t * m){
check_if_screen_needs_to_be_disabled();
if (printer.state == PRINTER_STATE_ERROR){
if (printer.state == PRINTER_STATE_OFFLINE){
nav_buttons_setup(PANEL_CONNECTING);
}
else if (printer.state == PRINTER_STATE_ERROR){
nav_buttons_setup(PANEL_ERROR);
}
else {

View File

@@ -85,6 +85,10 @@ static void btn_click_err(lv_event_t * e){
nav_buttons_setup(PANEL_ERROR);
}
static void btn_click_conn(lv_event_t * e){
nav_buttons_setup(PANEL_CONNECTING);
}
void create_button(const char* icon, const char* name, lv_event_cb_t button_click, lv_event_cb_t label_update, lv_obj_t * root){
lv_obj_t* btn = lv_btn_create(root);
lv_obj_set_flex_grow(btn, 1);
@@ -128,7 +132,7 @@ void nav_buttons_setup(unsigned char active_panel){
#endif
if (printer.state != PRINTER_STATE_ERROR){
if (printer.state > PRINTER_STATE_ERROR){
// Files/Print
create_button(LV_SYMBOL_COPY, "Idle", btn_click_files, update_printer_data_time, root_panel);
@@ -138,10 +142,14 @@ void nav_buttons_setup(unsigned char active_panel){
// Extrude/Temp
create_button(LV_SYMBOL_WARNING, "?/?", btn_click_extrude, update_printer_data_temp, root_panel);
}
else {
else if (printer.state == PRINTER_STATE_ERROR) {
// Error UI
create_button(LV_SYMBOL_WARNING, "Error", btn_click_err, NULL, root_panel);
}
else {
// Connecting
create_button(LV_SYMBOL_REFRESH, "Link", btn_click_conn, NULL, root_panel);
}
// Macros
create_button(LV_SYMBOL_GPS, "Macro", btn_click_macros, NULL, root_panel);
@@ -181,6 +189,9 @@ void nav_buttons_setup(unsigned char active_panel){
case PANEL_ERROR:
error_panel_init(panel);
break;
case PANEL_CONNECTING:
connecting_panel_init(panel);
break;
}
lv_msg_send(DATA_PRINTER_DATA, &printer);

View File

@@ -8,6 +8,7 @@
#define PANEL_STATS 5
#define PANEL_PRINTER 6
#define PANEL_ERROR 7
#define PANEL_CONNECTING 8
void nav_buttons_setup(unsigned char active_panel);
void nav_style_setup();

View File

@@ -0,0 +1,9 @@
#include "panel.h"
#include "../../conf/global_config.h"
void connecting_panel_init(lv_obj_t* panel)
{
lv_obj_t* label = lv_label_create(panel);
lv_label_set_text_fmt(label, "Connecting to %s...", (get_current_printer_config()->printer_name[0] == 0) ? get_current_printer_config()->klipper_host : get_current_printer_config()->printer_name);
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
}

View File

@@ -12,3 +12,4 @@ void macros_panel_init(lv_obj_t* panel);
void stats_panel_init(lv_obj_t* panel);
void printer_panel_init(lv_obj_t* panel);
void error_panel_init(lv_obj_t* panel);
void connecting_panel_init(lv_obj_t* panel);

View File

@@ -10,6 +10,7 @@
#include "../macros.h"
const char * printer_status[] = {
"Offline",
"Error",
"Idle",
"Printing",
@@ -41,7 +42,7 @@ static void update_printer_status_text(lv_event_t * e)
return;
}
if (!printer->online)
if (printer->state == PRINTER_STATE_OFFLINE)
{
lv_label_set_text(label, "Offline");
return;
@@ -57,7 +58,7 @@ static void update_printer_percentage_bar(lv_event_t * e)
int index = config - global_config.printer_config;
PrinterMinimal * printer = &printer_minimal[index];
if (printer->online && (printer->state == PRINTER_STATE_PRINTING || printer->state == PRINTER_STATE_PAUSED)){
if (printer->state != PRINTER_STATE_OFFLINE && (printer->state == PRINTER_STATE_PRINTING || printer->state == PRINTER_STATE_PAUSED)){
lv_bar_set_value(percentage, printer->print_progress * 100, LV_ANIM_OFF);
}
else {
@@ -72,7 +73,7 @@ static void update_printer_percentage_text(lv_event_t * e)
int index = config - global_config.printer_config;
PrinterMinimal * printer = &printer_minimal[index];
if (printer->online && (printer->state == PRINTER_STATE_PRINTING || printer->state == PRINTER_STATE_PAUSED))
if (printer->state != PRINTER_STATE_OFFLINE && (printer->state == PRINTER_STATE_PRINTING || printer->state == PRINTER_STATE_PAUSED))
{
char percentage_buffer[12];
sprintf(percentage_buffer, "%.2f%%", printer->print_progress * 100);
@@ -91,7 +92,7 @@ static void update_printer_control_button_text(lv_event_t * e)
int index = config - global_config.printer_config;
PrinterMinimal * printer = &printer_minimal[index];
if (!printer->online && printer->power_devices > 0)
if (printer->state == PRINTER_STATE_OFFLINE && printer->power_devices > 0)
{
lv_label_set_text(label, "Power");
}
@@ -125,7 +126,7 @@ static void btn_enable_control(lv_event_t * e)
int index = config - global_config.printer_config;
PrinterMinimal * printer = &printer_minimal[index];
if (config == get_current_printer_config() || (!printer->online && printer->power_devices <= 0))
if (config == get_current_printer_config() || (printer->state == PRINTER_STATE_OFFLINE && printer->power_devices <= 0))
{
// Disable
lv_obj_add_state(btn, LV_STATE_DISABLED);
@@ -180,7 +181,7 @@ static void btn_printer_activate(lv_event_t * e)
int index = config - global_config.printer_config;
PrinterMinimal * printer = &printer_minimal[index];
if (!printer->online)
if (printer->state == PRINTER_STATE_OFFLINE)
{
macros_draw_power_fullscreen(config);
return;

View File

@@ -58,14 +58,7 @@ void switch_printer_init() {
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");
}
lv_create_custom_menu_button(printer_name, parent, btn_switch_printer, (httpCode == 200) ? "Switch" : "Offline", config);
}
}
}

View File

@@ -5,6 +5,9 @@
#include "WiFi.h"
void wifi_init_inner();
void wifi_pass_entry(const char* ssid);
const char * current_ssid_ptr = NULL;
static void reset_btn_event_handler(lv_event_t * e) {
lv_event_code_t code = lv_event_get_code(e);
@@ -33,7 +36,9 @@ static void ta_event_cb(lv_event_t * e) {
int len = strlen(txt);
if (len > 0)
{
global_config.wifi_configured = true;
strcpy(global_config.wifi_SSID, current_ssid_ptr);
strcpy(global_config.wifi_password, txt);
write_global_config();
wifi_init_inner();
@@ -45,7 +50,63 @@ static void ta_event_cb(lv_event_t * e) {
}
}
void wifi_pass_entry(const char* ssid){
static void btn_reuse_password(lv_event_t * e)
{
ESP.restart();
}
static void btn_no_reuse_password(lv_event_t * e)
{
const char * ssid = (const char *)lv_event_get_user_data(e);
global_config.wifi_SSID[0] = '\0';
wifi_pass_entry(ssid);
}
void ask_reuse_password(const char * ssid){
lv_obj_clean(lv_scr_act());
lv_obj_t * root = lv_obj_create(lv_scr_act());
lv_obj_set_size(root, CYD_SCREEN_WIDTH_PX - CYD_SCREEN_GAP_PX * 2, CYD_SCREEN_HEIGHT_PX - CYD_SCREEN_GAP_PX * 2);
lv_obj_align(root, LV_ALIGN_CENTER, 0, 0);
lv_layout_flex_column(root, LV_FLEX_ALIGN_SPACE_BETWEEN);
lv_obj_t * label = lv_label_create(root);
lv_label_set_text_fmt(label, "Reuse stored WiFi Password?\n(Password Length: %d)", strlen(global_config.wifi_password));
lv_obj_t * button_row = lv_create_empty_panel(root);
lv_layout_flex_row(button_row, LV_FLEX_ALIGN_SPACE_BETWEEN);
lv_obj_set_size(button_row, lv_pct(100), CYD_SCREEN_MIN_BUTTON_HEIGHT_PX);
lv_obj_t * no_btn = lv_btn_create(button_row);
lv_obj_add_event_cb(no_btn, btn_no_reuse_password, LV_EVENT_CLICKED, (void*)ssid);
lv_obj_set_height(no_btn, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX);
lv_obj_set_width(no_btn, CYD_SCREEN_MIN_BUTTON_WIDTH_PX);
lv_obj_set_style_pad_all(no_btn, CYD_SCREEN_GAP_PX, 0);
label = lv_label_create(no_btn);
lv_label_set_text(label, LV_SYMBOL_CLOSE);
lv_obj_center(label);
lv_obj_t * yes_btn = lv_btn_create(button_row);
lv_obj_add_event_cb(yes_btn, btn_reuse_password, LV_EVENT_CLICKED, (void*)ssid);
lv_obj_set_height(yes_btn, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX);
lv_obj_set_width(yes_btn, CYD_SCREEN_MIN_BUTTON_WIDTH_PX);
lv_obj_set_style_pad_all(yes_btn, CYD_SCREEN_GAP_PX, 0);
label = lv_label_create(yes_btn);
lv_label_set_text(label, LV_SYMBOL_OK);
lv_obj_center(label);
}
void wifi_pass_entry(const char* ssid)
{
current_ssid_ptr = ssid;
if (strcmp(ssid, global_config.wifi_SSID) == 0){
ask_reuse_password(ssid);
return;
}
lv_obj_clean(lv_scr_act());
lv_obj_t * root = lv_create_empty_panel(lv_scr_act());
@@ -79,7 +140,6 @@ static void wifi_btn_event_handler(lv_event_t * e){
if(code == LV_EVENT_CLICKED) {
delay(100);
char* ssid = (char*)e->user_data;
strcpy(global_config.wifi_SSID, ssid);
Serial.println(ssid);
wifi_pass_entry(ssid);
}
@@ -92,6 +152,8 @@ void wifi_init_inner(){
if (global_config.wifi_configured){
WiFi.begin(global_config.wifi_SSID, global_config.wifi_password);
Serial.printf("Connecting to %s with a password length of %d\n", global_config.wifi_SSID, strlen(global_config.wifi_password));
lv_obj_t * label = lv_label_create(lv_scr_act());
lv_label_set_text(label, "Connecting to WiFi");
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);