Skip to content

Commit

Permalink
Refactor + Power menu in printer menu
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Mar 12, 2024
1 parent a265301 commit a8c94fe
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 122 deletions.
6 changes: 4 additions & 2 deletions CYD-Klipper/src/core/data_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <UrlEncode.h>
#include "http_client.h"
#include "../ui/ui_utils.h"
#include "macros_query.h"

const char *printer_state_messages[] = {
"Error",
Expand Down Expand Up @@ -325,6 +326,7 @@ void fetch_printer_data_minimal()
if (httpCode == 200)
{
data[i].online = true;
data[i].power_devices = 0;
JsonDocument doc;
deserializeJson(doc, client.getStream());
auto status = doc["result"]["status"];
Expand Down Expand Up @@ -377,7 +379,8 @@ void fetch_printer_data_minimal()
}
else
{
printer_minimal->online = false;
data[i].online = false;
data[i].power_devices = power_devices_count(config);
unfreeze_request_thread();
}
}
Expand Down Expand Up @@ -420,7 +423,6 @@ void data_setup()
printer.print_filename = filename_buff;
fetch_printer_data();

macros_query_setup();
freeze_render_thread();
xTaskCreatePinnedToCore(data_loop_background, "data_loop_background", 5000, NULL, 2, &background_loop, 0);
}
1 change: 1 addition & 0 deletions CYD-Klipper/src/core/data_setup.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef struct _PrinterMinimal {
bool online;
unsigned char state;
float print_progress; // 0 -> 1
unsigned int power_devices;
} PrinterMinimal;

extern Printer printer;
Expand Down
142 changes: 100 additions & 42 deletions CYD-Klipper/src/core/macros_query.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "lvgl.h"
#include "macros_query.h"
#include "./data_setup.h"
#include "../conf/global_config.h"
#include <ArduinoJson.h>
#include <UrlEncode.h>
#include "http_client.h"
Expand All @@ -11,12 +10,15 @@ static int macros_count = 0;

static char* power_devices[16] = {0};
static bool power_device_states[16] = {0};
static int power_devices_count = 0;
static unsigned int stored_power_devices_count = 0;

void _macros_query_internal(){
SETUP_HTTP_CLIENT("/printer/gcode/help")
MACROSQUERY macros_query(PRINTER_CONFIG * config)
{
HTTPClient client;
configure_http_client(client, get_full_url("/printer/gcode/help", config), true, 1000);

int httpCode = client.GET();

if (httpCode == 200){
JsonDocument doc;
deserializeJson(doc, client.getStream());
Expand All @@ -37,76 +39,132 @@ void _macros_query_internal(){
macros[macros_count++] = macro;
}
}
}
}

void power_devices_clear(){
for (int i = 0; i < power_devices_count; i++){
free(power_devices[i]);
return {(const char**)macros, (unsigned int)macros_count};
}
else {
return {NULL, 0};
}
}

power_devices_count = 0;
MACROSQUERY macros_query()
{
return macros_query(get_current_printer_config());
}

void _power_devices_query_internal(){
SETUP_HTTP_CLIENT("/machine/device_power/devices")
unsigned int macro_count(PRINTER_CONFIG * config)
{
HTTPClient client;
configure_http_client(client, get_full_url("/printer/gcode/help", config), true, 1000);

int httpCode = client.GET();

if (httpCode == 200 || httpCode == 404 || httpCode == 500){
power_devices_clear();
if (httpCode == 200){
JsonDocument doc;
deserializeJson(doc, client.getStream());
auto result = doc["result"].as<JsonObject>();

unsigned int count = 0;

for (JsonPair i : result){
const char *value = i.value().as<String>().c_str();
if (strcmp(value, "CYD_SCREEN_MACRO") == 0) {
count++;
}
}

return count;
}
else {
return 0;
}
}

unsigned int macro_count()
{
return macro_count(get_current_printer_config());
}

POWERQUERY power_devices_query(PRINTER_CONFIG * config)
{
HTTPClient client;
configure_http_client(client, get_full_url("/machine/device_power/devices", config), true, 1000);

int httpCode = client.GET();

if (httpCode == 200){
JsonDocument doc;
deserializeJson(doc, client.getStream());
auto result = doc["result"]["devices"].as<JsonArray>();

for (int i = 0; i < stored_power_devices_count; i++){
free(power_devices[i]);
}

stored_power_devices_count = 0;

for (auto i : result){
const char * device_name = i["device"];
const char * device_state = i["status"];
power_devices[power_devices_count] = (char*)malloc(strlen(device_name) + 1);
strcpy(power_devices[power_devices_count], device_name);
power_device_states[power_devices_count] = strcmp(device_state, "on") == 0;
power_devices_count++;
power_devices[stored_power_devices_count] = (char*)malloc(strlen(device_name) + 1);
strcpy(power_devices[stored_power_devices_count], device_name);
power_device_states[stored_power_devices_count] = strcmp(device_state, "on") == 0;
stored_power_devices_count++;
}
}
}

static void on_state_change(void * s, lv_msg_t * m) {
if (printer.state == PRINTER_STATE_ERROR || printer.state == PRINTER_STATE_PAUSED){
return;
return {(const char**)power_devices, (const bool*)power_device_states, (unsigned int)stored_power_devices_count};
}
else {
return {NULL, NULL, 0};
}
}

_macros_query_internal();
_power_devices_query_internal();
POWERQUERY power_devices_query()
{
return power_devices_query(get_current_printer_config());
}

bool set_power_state(const char* device_name, bool state) {
SETUP_HTTP_CLIENT("/machine/device_power/device?device=" + urlEncode(device_name) + "&action=" + (state ? "on" : "off"));
unsigned int power_devices_count(PRINTER_CONFIG * config)
{
HTTPClient client;
configure_http_client(client, get_full_url("/machine/device_power/devices", config), true, 1000);

int httpCode = client.GET();

if (httpCode == 200){
JsonDocument doc;
deserializeJson(doc, client.getStream());
auto result = doc["result"]["devices"].as<JsonArray>();

if (client.POST("") != 200)
return false;
unsigned int count = 0;

for (int i = 0; i < power_devices_count; i++){
if (strcmp(power_devices[i], device_name) == 0){
power_device_states[i] = state;
return true;
for (auto i : result){
count++;
}
}

return true;
return count;
}
else {
return 0;
}
}

MACROSQUERY macros_query() {
return {(const char**)macros, (unsigned int)macros_count};
unsigned int power_devices_count()
{
return power_devices_count(get_current_printer_config());
}

POWERQUERY power_devices_query() {
return {(const char**)power_devices, (const bool*)power_device_states, (unsigned int)power_devices_count};


bool set_power_state(const char* device_name, bool state, PRINTER_CONFIG * config)
{
HTTPClient client;
configure_http_client(client, get_full_url("/machine/device_power/device?device=" + urlEncode(device_name) + "&action=" + (state ? "on" : "off"), config), true, 1000);

return client.POST("") == 200;
}

void macros_query_setup(){
lv_msg_subscribe(DATA_PRINTER_STATE, on_state_change, NULL);
on_state_change(NULL, NULL);
bool set_power_state(const char* device_name, bool state)
{
return set_power_state(device_name, state, get_current_printer_config());
}
15 changes: 10 additions & 5 deletions CYD-Klipper/src/core/macros_query.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "../conf/global_config.h"

typedef struct {
const char** macros;
uint32_t count;
Expand All @@ -11,10 +13,13 @@ typedef struct {
uint32_t count;
} POWERQUERY;

MACROSQUERY macros_query(PRINTER_CONFIG * config);
MACROSQUERY macros_query();
unsigned int macro_count(PRINTER_CONFIG * config);
unsigned int macro_count();
POWERQUERY power_devices_query(PRINTER_CONFIG * config);
POWERQUERY power_devices_query();
void macros_query_setup();
bool set_power_state(const char* device_name, bool state);
void _power_devices_query_internal();
void _macros_query_internal();
void power_devices_clear();
unsigned int power_devices_count(PRINTER_CONFIG * config);
unsigned int power_devices_count();
bool set_power_state(const char* device_name, bool state, PRINTER_CONFIG * config);
bool set_power_state(const char* device_name, bool state);
32 changes: 9 additions & 23 deletions CYD-Klipper/src/ui/ip_setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
#include "panels/panel.h"
#include "../core/http_client.h"
#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;
Expand Down Expand Up @@ -116,21 +118,7 @@ static void reset_btn_event_handler(lv_event_t * e){
}

static void power_devices_button(lv_event_t * e) {
lv_obj_t * panel = lv_create_empty_panel(lv_scr_act());
lv_obj_set_style_bg_opa(panel, LV_OPA_COVER, 0);
lv_layout_flex_column(panel);
lv_obj_set_size(panel, CYD_SCREEN_WIDTH_PX, CYD_SCREEN_HEIGHT_PX - CYD_SCREEN_GAP_PX);
lv_obj_align(panel, LV_ALIGN_TOP_LEFT, 0, CYD_SCREEN_GAP_PX);

lv_obj_t * button = lv_btn_create(panel);
lv_obj_set_size(button, CYD_SCREEN_WIDTH_PX - CYD_SCREEN_GAP_PX * 2, CYD_SCREEN_MIN_BUTTON_HEIGHT_PX);
lv_obj_add_event_cb(button, destroy_event_user_data, LV_EVENT_CLICKED, panel);

lv_obj_t * label = lv_label_create(button);
lv_label_set_text(label, LV_SYMBOL_CLOSE " Close");
lv_obj_center(label);

macros_panel_add_power_devices_to_panel(panel, power_devices_query());
macros_draw_power_fullscreen();
}

void redraw_connect_screen(){
Expand All @@ -153,7 +141,7 @@ void redraw_connect_screen(){
lv_label_set_text(btn_label, "Reset");
lv_obj_center(btn_label);

if (power_devices_query().count >= 1){
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);
Expand Down Expand Up @@ -292,7 +280,7 @@ int retry_count = 0;
void ip_init(){
connect_ok = false;
retry_count = 0;
int prev_power_device_count = 0;
prev_power_device_count = 0;

ip_init_inner();

Expand All @@ -312,13 +300,12 @@ void ip_init(){
lv_label_set_text(label, retry_count_text.c_str());
}

if (status != CONNECT_AUTH_REQUIRED)
_power_devices_query_internal();
else
if (status == CONNECT_AUTH_REQUIRED)
handle_auth_entry();

if (power_devices_query().count != prev_power_device_count) {
prev_power_device_count = power_devices_query().count;
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();
}
}
Expand All @@ -328,7 +315,6 @@ void ip_init(){
void ip_ok(){
if (klipper_request_consecutive_fail_count > 5){
freeze_request_thread();
power_devices_clear();
ip_init();
unfreeze_request_thread();
klipper_request_consecutive_fail_count = 0;
Expand Down
Loading

0 comments on commit a8c94fe

Please sign in to comment.