mirror of
https://github.com/suchmememanyskill/CYD-Klipper.git
synced 2026-03-21 05:33:24 +00:00
Add macro support
This commit is contained in:
3
CYD-Klipper/.vscode/settings.json
vendored
3
CYD-Klipper/.vscode/settings.json
vendored
@@ -8,6 +8,7 @@
|
|||||||
"unordered_set": "cpp",
|
"unordered_set": "cpp",
|
||||||
"vector": "cpp",
|
"vector": "cpp",
|
||||||
"string_view": "cpp",
|
"string_view": "cpp",
|
||||||
"initializer_list": "cpp"
|
"initializer_list": "cpp",
|
||||||
|
"algorithm": "cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
#include "../conf/global_config.h"
|
#include "../conf/global_config.h"
|
||||||
#include <HTTPClient.h>
|
#include <HTTPClient.h>
|
||||||
#include <ArduinoJson.h>
|
#include <ArduinoJson.h>
|
||||||
|
#include "macros_query.h"
|
||||||
|
|
||||||
const char *printer_state_messages[] = {
|
const char *printer_state_messages[] = {
|
||||||
"Error",
|
"Error",
|
||||||
@@ -179,4 +180,5 @@ void data_setup()
|
|||||||
{
|
{
|
||||||
printer.print_filename = filename_buff;
|
printer.print_filename = filename_buff;
|
||||||
fetch_printer_data();
|
fetch_printer_data();
|
||||||
|
macros_query_setup();
|
||||||
}
|
}
|
||||||
51
CYD-Klipper/src/core/macros_query.cpp
Normal file
51
CYD-Klipper/src/core/macros_query.cpp
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#include "lvgl.h"
|
||||||
|
#include "macros_query.h"
|
||||||
|
#include "./data_setup.h"
|
||||||
|
#include <HTTPClient.h>
|
||||||
|
#include "../conf/global_config.h"
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
|
||||||
|
static char* macros[64] = {0};
|
||||||
|
static int macros_count = 0;
|
||||||
|
|
||||||
|
static void on_state_change(void * s, lv_msg_t * m) {
|
||||||
|
if (printer.state == PRINTER_STATE_ERROR || printer.state == PRINTER_STATE_PAUSED){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String url = "http://" + String(global_config.klipperHost) + ":" + String(global_config.klipperPort) + "/printer/gcode/help";
|
||||||
|
HTTPClient client;
|
||||||
|
client.begin(url.c_str());
|
||||||
|
int httpCode = client.GET();
|
||||||
|
if (httpCode == 200){
|
||||||
|
String payload = client.getString();
|
||||||
|
DynamicJsonDocument doc(16384);
|
||||||
|
deserializeJson(doc, payload);
|
||||||
|
auto result = doc["result"].as<JsonObject>();
|
||||||
|
|
||||||
|
for (int i = 0; i < macros_count; i++){
|
||||||
|
free(macros[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
macros_count = 0;
|
||||||
|
|
||||||
|
for (JsonPair i : result){
|
||||||
|
const char *key = i.key().c_str();
|
||||||
|
const char *value = i.value().as<String>().c_str();
|
||||||
|
if (strcmp(value, "CYD_SCREEN_MACRO") == 0) {
|
||||||
|
char* macro = (char*)malloc(strlen(key) + 1);
|
||||||
|
strcpy(macro, key);
|
||||||
|
macros[macros_count++] = macro;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
MACROSQUERY macros_query() {
|
||||||
|
return {(const char**)macros, macros_count};
|
||||||
|
}
|
||||||
|
|
||||||
|
void macros_query_setup(){
|
||||||
|
lv_msg_subscribe(DATA_PRINTER_STATE, on_state_change, NULL);
|
||||||
|
on_state_change(NULL, NULL);
|
||||||
|
}
|
||||||
9
CYD-Klipper/src/core/macros_query.h
Normal file
9
CYD-Klipper/src/core/macros_query.h
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char** macros;
|
||||||
|
uint32_t count;
|
||||||
|
} MACROSQUERY;
|
||||||
|
|
||||||
|
MACROSQUERY macros_query();
|
||||||
|
void macros_query_setup();
|
||||||
@@ -71,6 +71,10 @@ static void btn_click_settings(lv_event_t * e){
|
|||||||
nav_buttons_setup(3);
|
nav_buttons_setup(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void btn_click_macros(lv_event_t * e){
|
||||||
|
nav_buttons_setup(4);
|
||||||
|
}
|
||||||
|
|
||||||
void nav_buttons_setup(unsigned char active_panel){
|
void nav_buttons_setup(unsigned char active_panel){
|
||||||
lv_obj_clean(lv_scr_act());
|
lv_obj_clean(lv_scr_act());
|
||||||
lv_obj_clear_flag(lv_scr_act(), LV_OBJ_FLAG_SCROLLABLE);
|
lv_obj_clear_flag(lv_scr_act(), LV_OBJ_FLAG_SCROLLABLE);
|
||||||
@@ -140,14 +144,14 @@ void nav_buttons_setup(unsigned char active_panel){
|
|||||||
lv_obj_set_size(btn, button_width, button_height);
|
lv_obj_set_size(btn, button_width, button_height);
|
||||||
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, 0, button_height * 3);
|
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, 0, button_height * 3);
|
||||||
lv_obj_add_style(btn, &nav_button_style, 0);
|
lv_obj_add_style(btn, &nav_button_style, 0);
|
||||||
lv_obj_add_event_cb(btn, btn_click_settings, LV_EVENT_CLICKED, NULL);
|
lv_obj_add_event_cb(btn, btn_click_macros, LV_EVENT_CLICKED, NULL);
|
||||||
|
|
||||||
label = lv_label_create(btn);
|
label = lv_label_create(btn);
|
||||||
lv_label_set_text(label, LV_SYMBOL_SETTINGS);
|
lv_label_set_text(label, LV_SYMBOL_GPS);
|
||||||
lv_obj_align(label, LV_ALIGN_CENTER, 0, -1 * icon_text_spacing);
|
lv_obj_align(label, LV_ALIGN_CENTER, 0, -1 * icon_text_spacing);
|
||||||
|
|
||||||
label = lv_label_create(btn);
|
label = lv_label_create(btn);
|
||||||
lv_label_set_text(label, "Screen");
|
lv_label_set_text(label, "Macro");
|
||||||
lv_obj_align(label, LV_ALIGN_CENTER, 0, icon_text_spacing);
|
lv_obj_align(label, LV_ALIGN_CENTER, 0, icon_text_spacing);
|
||||||
lv_obj_add_style(label, &nav_button_text_style, 0);
|
lv_obj_add_style(label, &nav_button_text_style, 0);
|
||||||
|
|
||||||
@@ -171,6 +175,9 @@ void nav_buttons_setup(unsigned char active_panel){
|
|||||||
case 3:
|
case 3:
|
||||||
settings_panel_init(panel);
|
settings_panel_init(panel);
|
||||||
break;
|
break;
|
||||||
|
case 4:
|
||||||
|
macros_panel_init(panel);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
79
CYD-Klipper/src/ui/panels/macros_panel.cpp
Normal file
79
CYD-Klipper/src/ui/panels/macros_panel.cpp
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
#include "lvgl.h"
|
||||||
|
#include "panel.h"
|
||||||
|
#include "../nav_buttons.h"
|
||||||
|
#include "../../core/data_setup.h"
|
||||||
|
#include "../../core/macros_query.h"
|
||||||
|
#include <HardwareSerial.h>
|
||||||
|
|
||||||
|
int y_offset_macros = 40;
|
||||||
|
const int y_element_size = 50;
|
||||||
|
const int y_seperator_size = 1;
|
||||||
|
const int y_seperator_x_padding = 50;
|
||||||
|
const int panel_width = TFT_HEIGHT - 40;
|
||||||
|
const int y_element_x_padding = 30;
|
||||||
|
const static lv_point_t line_points[] = { {0, 0}, {panel_width - y_seperator_x_padding, 0} };
|
||||||
|
|
||||||
|
static void btn_press(lv_event_t * e){
|
||||||
|
lv_obj_t * btn = lv_event_get_target(e);
|
||||||
|
const char* macro = (const char*)lv_event_get_user_data(e);
|
||||||
|
Serial.printf("Macro: %s\n", macro);
|
||||||
|
send_gcode(false, macro);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void btn_goto_settings(lv_event_t * e){
|
||||||
|
nav_buttons_setup(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
void create_macro_widget(const char* macro, lv_obj_t* root_panel){
|
||||||
|
lv_obj_t * panel = lv_obj_create(root_panel);
|
||||||
|
lv_obj_set_style_border_width(panel, 0, 0);
|
||||||
|
lv_obj_set_style_bg_opa(panel, LV_OPA_TRANSP, 0);
|
||||||
|
lv_obj_set_style_pad_all(panel, 0, 0);
|
||||||
|
lv_obj_align(panel, LV_ALIGN_TOP_MID, 0, y_offset_macros);
|
||||||
|
lv_obj_set_size(panel, panel_width - y_element_x_padding, y_element_size);
|
||||||
|
|
||||||
|
lv_obj_t * line = lv_line_create(panel);
|
||||||
|
lv_line_set_points(line, line_points, 2);
|
||||||
|
lv_obj_set_style_line_width(line, y_seperator_size, 0);
|
||||||
|
lv_obj_set_style_line_color(line, lv_color_hex(0xAAAAAA), 0);
|
||||||
|
lv_obj_align(line, LV_ALIGN_BOTTOM_MID, 0, 0);
|
||||||
|
|
||||||
|
lv_obj_t * label = lv_label_create(panel);
|
||||||
|
lv_label_set_text(label, macro);
|
||||||
|
lv_obj_align(label, LV_ALIGN_LEFT_MID, 0, 0);
|
||||||
|
|
||||||
|
lv_obj_t * btn = lv_btn_create(panel);
|
||||||
|
lv_obj_align(btn, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||||
|
lv_obj_add_event_cb(btn, btn_press, LV_EVENT_CLICKED, (void*)macro);
|
||||||
|
|
||||||
|
label = lv_label_create(btn);
|
||||||
|
lv_label_set_text(label, "Run");
|
||||||
|
lv_obj_center(label);
|
||||||
|
|
||||||
|
y_offset_macros += y_element_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
void macros_panel_init(lv_obj_t* panel) {
|
||||||
|
y_offset_macros = 40;
|
||||||
|
|
||||||
|
lv_obj_t * btn = lv_btn_create(panel);
|
||||||
|
lv_obj_add_event_cb(btn, btn_goto_settings, LV_EVENT_CLICKED, NULL);
|
||||||
|
lv_obj_set_size(btn, TFT_HEIGHT - 40 - 20, 30);
|
||||||
|
lv_obj_align(btn, LV_ALIGN_TOP_MID, 0, 5);
|
||||||
|
|
||||||
|
lv_obj_t * label = lv_label_create(btn);
|
||||||
|
lv_label_set_text(label, LV_SYMBOL_SETTINGS " Screen Settings");
|
||||||
|
lv_obj_center(label);
|
||||||
|
|
||||||
|
MACROSQUERY query = macros_query();
|
||||||
|
if (query.count == 0){
|
||||||
|
label = lv_label_create(panel);
|
||||||
|
lv_label_set_text(label, "No macros found.\nMacros with the description\n\"CYD_SCREEN_MACRO\"\nwill show up here.");
|
||||||
|
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < query.count; i++){
|
||||||
|
create_macro_widget(query.macros[i], panel);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,4 +6,5 @@ void settings_panel_init(lv_obj_t* panel);
|
|||||||
void temp_panel_init(lv_obj_t* panel);
|
void temp_panel_init(lv_obj_t* panel);
|
||||||
void print_panel_init(lv_obj_t* panel);
|
void print_panel_init(lv_obj_t* panel);
|
||||||
void move_panel_init(lv_obj_t* panel);
|
void move_panel_init(lv_obj_t* panel);
|
||||||
void progress_panel_init(lv_obj_t* panel);
|
void progress_panel_init(lv_obj_t* panel);
|
||||||
|
void macros_panel_init(lv_obj_t* panel);
|
||||||
Reference in New Issue
Block a user