Implement move ui

This commit is contained in:
suchmememanyskill
2023-11-12 17:04:11 +01:00
parent 6569e428c8
commit ef1afa6560
18 changed files with 473 additions and 63 deletions

View File

@@ -1,12 +1,13 @@
#include "main_ui.h"
#include "../core/data_setup.h"
#include "lvgl.h"
#include "nav_buttons.h"
#include <ArduinoJson.h>
char extruder_temp_buff[20];
char bed_temp_buff[20];
char position_buff[20];
/*
static void update_printer_state(lv_event_t * e){
lv_obj_t * label = lv_event_get_target(e);
@@ -19,7 +20,7 @@ static void update_printer_state_message(lv_event_t * e) {
lv_label_set_text(label, printer.state_message);
}
static void update_printer_data_extruder_temp(lv_event_t * e) {
static void update_printer_data_temp(lv_event_t * e) {
lv_obj_t * label = lv_event_get_target(e);
sprintf(extruder_temp_buff, "E: %.1f/%.1f", printer.extruder_temp, printer.extruder_target_temp);
@@ -59,7 +60,7 @@ void main_ui(){
label = lv_label_create(lv_scr_act());
lv_label_set_text(label, "Waiting for update...");
lv_obj_align(label, LV_ALIGN_BOTTOM_LEFT, 0, 0);
lv_obj_add_event_cb(label, update_printer_data_extruder_temp, LV_EVENT_MSG_RECEIVED, NULL);
lv_obj_add_event_cb(label, update_printer_data_temp, LV_EVENT_MSG_RECEIVED, NULL);
lv_msg_subsribe_obj(DATA_PRINTER_DATA, label, NULL);
label = lv_label_create(lv_scr_act());
@@ -74,10 +75,35 @@ void main_ui(){
lv_obj_add_event_cb(label, update_printer_data_position, LV_EVENT_MSG_RECEIVED, NULL);
lv_msg_subsribe_obj(DATA_PRINTER_DATA, label, NULL);
}
*/
void error_ui(){
lv_obj_clean(lv_scr_act());
lv_obj_t * label;
label = lv_label_create(lv_scr_act());
lv_label_set_text(label, LV_SYMBOL_WARNING " Printer is not ready");
lv_obj_align(label, LV_ALIGN_TOP_LEFT, 10, 10);
label = lv_label_create(lv_scr_act());
lv_label_set_text(label, printer.state_message);
lv_obj_align(label, LV_ALIGN_TOP_LEFT, 10, 30);
lv_obj_set_size(label, TFT_HEIGHT - 20, TFT_WIDTH - 30);
lv_obj_clear_flag(label, LV_OBJ_FLAG_SCROLLABLE);
lv_label_set_long_mode(label, LV_LABEL_LONG_WRAP);
}
static void on_state_change(void * s, lv_msg_t * m){
if (printer.state == PRINTER_STATE_ERROR){
error_ui();
}
else {
nav_buttons_setup(1);
}
}
void main_ui_setup(){
// TODO: Subscribe to events
main_ui();
lv_msg_send(DATA_PRINTER_STATE, &printer);
lv_msg_send(DATA_PRINTER_DATA, &printer);
lv_msg_subscribe(DATA_PRINTER_STATE, on_state_change, NULL);
on_state_change(NULL, NULL);
}

View File

@@ -1,2 +1 @@
void main_ui();
void main_ui_setup();

View File

@@ -0,0 +1,151 @@
#include "lvgl.h"
#include "panels/panel.h"
#include "../core/data_setup.h"
#include "nav_buttons.h"
#include <HTTPClient.h>
static lv_style_t nav_button_style;
static char temp_buffer[10];
static char z_pos_buffer[10];
static lv_style_t nav_button_text_style;
static void update_printer_data_z_pos(lv_event_t * e) {
lv_obj_t * label = lv_event_get_target(e);
sprintf(z_pos_buffer, "Z%.2f", printer.position[2]);
lv_label_set_text(label, z_pos_buffer);
}
static void update_printer_data_temp(lv_event_t * e) {
lv_obj_t * label = lv_event_get_target(e);
sprintf(temp_buffer, "%.0f/%.0f", printer.extruder_temp, printer.bed_temp);
lv_label_set_text(label, temp_buffer);
}
static void btn_click_files(lv_event_t * e){
nav_buttons_setup(0);
}
static void btn_click_move(lv_event_t * e){
nav_buttons_setup(1);
}
static void btn_click_extrude(lv_event_t * e){
nav_buttons_setup(2);
}
static void btn_click_settings(lv_event_t * e){
nav_buttons_setup(3);
}
void nav_buttons_setup(unsigned char active_panel){
lv_obj_clean(lv_scr_act());
lv_obj_clear_flag(lv_scr_act(), LV_OBJ_FLAG_SCROLLABLE);
sprintf(temp_buffer, "%.0f/%.0f", printer.extruder_temp, printer.bed_temp);
sprintf(z_pos_buffer, "Z%.2f", printer.position[2]);
const int button_width = 40;
const int button_height = 60;
const int icon_text_spacing = 10;
// Files/Print
lv_obj_t * btn = lv_btn_create(lv_scr_act());
lv_obj_set_size(btn, button_width, button_height);
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, 0, 0);
lv_obj_add_style(btn, &nav_button_style, 0);
lv_obj_add_event_cb(btn, btn_click_files, LV_EVENT_CLICKED, NULL);
lv_obj_t * label = lv_label_create(btn);
lv_label_set_text(label, LV_SYMBOL_COPY);
lv_obj_align(label, LV_ALIGN_CENTER, 0, -1 * icon_text_spacing);
label = lv_label_create(btn);
lv_label_set_text(label, "Idle");
lv_obj_align(label, LV_ALIGN_CENTER, 0, icon_text_spacing);
lv_obj_add_style(label, &nav_button_text_style, 0);
// Move
btn = lv_btn_create(lv_scr_act());
lv_obj_set_size(btn, button_width, button_height);
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, 0, button_height);
lv_obj_add_style(btn, &nav_button_style, 0);
lv_obj_add_event_cb(btn, btn_click_move, LV_EVENT_CLICKED, NULL);
label = lv_label_create(btn);
lv_label_set_text(label, LV_SYMBOL_CHARGE);
lv_obj_align(label, LV_ALIGN_CENTER, 0, -1 * icon_text_spacing);
label = lv_label_create(btn);
lv_label_set_text(label, z_pos_buffer);
lv_obj_align(label, LV_ALIGN_CENTER, 0, icon_text_spacing);
lv_obj_add_event_cb(label, update_printer_data_z_pos, LV_EVENT_MSG_RECEIVED, NULL);
lv_msg_subsribe_obj(DATA_PRINTER_DATA, label, NULL);
lv_obj_add_style(label, &nav_button_text_style, 0);
// Extrude/Temp
btn = lv_btn_create(lv_scr_act());
lv_obj_set_size(btn, button_width, button_height);
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, 0, button_height * 2);
lv_obj_add_style(btn, &nav_button_style, 0);
lv_obj_add_event_cb(btn, btn_click_extrude, LV_EVENT_CLICKED, NULL);
label = lv_label_create(btn);
lv_label_set_text(label, LV_SYMBOL_WARNING);
lv_obj_align(label, LV_ALIGN_CENTER, 0, -1 * icon_text_spacing);
label = lv_label_create(btn);
lv_label_set_text(label, temp_buffer);
lv_obj_align(label, LV_ALIGN_CENTER, 0, icon_text_spacing);
lv_obj_add_event_cb(label, update_printer_data_temp, LV_EVENT_MSG_RECEIVED, NULL);
lv_msg_subsribe_obj(DATA_PRINTER_DATA, label, NULL);
lv_obj_add_style(label, &nav_button_text_style, 0);
// Settings
btn = lv_btn_create(lv_scr_act());
lv_obj_set_size(btn, button_width, button_height);
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, 0, button_height * 3);
lv_obj_add_style(btn, &nav_button_style, 0);
lv_obj_add_event_cb(btn, btn_click_settings, LV_EVENT_CLICKED, NULL);
label = lv_label_create(btn);
lv_label_set_text(label, LV_SYMBOL_SETTINGS);
lv_obj_align(label, LV_ALIGN_CENTER, 0, -1 * icon_text_spacing);
label = lv_label_create(btn);
lv_label_set_text(label, "Screen");
lv_obj_align(label, LV_ALIGN_CENTER, 0, icon_text_spacing);
lv_obj_add_style(label, &nav_button_text_style, 0);
lv_obj_t * panel = lv_obj_create(lv_scr_act());
lv_obj_set_size(panel, TFT_HEIGHT - button_width, TFT_WIDTH);
lv_obj_align(panel, LV_ALIGN_TOP_RIGHT, 0, 0);
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);
switch (active_panel){
case 0:
print_panel_init(panel);
break;
case 1:
move_panel_init(panel);
break;
case 2:
temp_panel_init(panel);
break;
case 3:
settings_panel_init(panel);
break;
}
}
void nav_style_setup(){
lv_style_init(&nav_button_style);
lv_style_set_radius(&nav_button_style, 0);
lv_style_init(&nav_button_text_style);
lv_style_set_text_font(&nav_button_text_style, &lv_font_montserrat_10);
}

View File

@@ -0,0 +1,2 @@
void nav_buttons_setup(unsigned char active_panel);
void nav_style_setup();

View File

@@ -0,0 +1,215 @@
#include "lvgl.h"
#include "panel.h"
#include "../../core/data_setup.h"
#include <TFT_eSPI.h>
static void move_printer(const char* axis, float amount) {
if (!printer.homed_axis || printer.state == PRINTER_STATE_PRINTING)
return;
char gcode[64];
const char* extra = (amount > 0) ? "+" : "";
bool absolute_coords = printer.absolute_coords;
if (absolute_coords) {
send_gcode(true, "G91");
}
const char * space = "%20";
sprintf(gcode, "G1%s%s%s%.1f", space, axis, extra, amount);
send_gcode(true, gcode);
if (absolute_coords) {
send_gcode(true, "G90");
}
}
static void x_line_button_press(lv_event_t * e) {
float* data_pointer = (float*)lv_event_get_user_data(e);
float data = *data_pointer;
move_printer("X", data);
}
static void y_line_button_press(lv_event_t * e) {
float* data_pointer = (float*)lv_event_get_user_data(e);
float data = *data_pointer;
move_printer("Y", data);
}
static void z_line_button_press(lv_event_t * e) {
float* data_pointer = (float*)lv_event_get_user_data(e);
float data = *data_pointer;
move_printer("Z", data);
}
char x_pos_buff[12];
static void x_pos_update(lv_event_t * e){
lv_obj_t * label = lv_event_get_target(e);
sprintf(x_pos_buff, "X: %.1f", printer.position[0]);
lv_label_set_text(label, x_pos_buff);
}
char y_pos_buff[12];
static void y_pos_update(lv_event_t * e){
lv_obj_t * label = lv_event_get_target(e);
sprintf(y_pos_buff, "Y: %.1f", printer.position[1]);
lv_label_set_text(label, y_pos_buff);
}
char z_pos_buff[12];
static void z_pos_update(lv_event_t * e){
lv_obj_t * label = lv_event_get_target(e);
sprintf(z_pos_buff, "Z: %.2f", printer.position[2]);
lv_label_set_text(label, z_pos_buff);
}
lv_event_cb_t button_callbacks[] = {x_line_button_press, y_line_button_press, z_line_button_press};
lv_event_cb_t position_callbacks[] = {x_pos_update, y_pos_update, z_pos_update};
const float xy_offsets[] = {-100, -10, -1, 1, 10, 100};
const float z_offsets[] = {-25, -1, -0.1, 0.1, 1, 25};
const float* offsets[] = {
xy_offsets,
xy_offsets,
z_offsets
};
const char* xy_offset_labels[] = {"-100", "-10", "-1", "+1", "+10", "+100"};
const char* z_offset_labels[] = {"-25", "-1", "-0.1", "+0.1", "+1", "+25"};
const char** offset_labels[] = {
xy_offset_labels,
xy_offset_labels,
z_offset_labels
};
static void home_button_click(lv_event_t * e) {
if (printer.state == PRINTER_STATE_PRINTING)
return;
send_gcode(false, "G28");
}
static void disable_steppers_click(lv_event_t * e) {
if (printer.state == PRINTER_STATE_PRINTING)
return;
send_gcode(true, "M18");
}
static void stepper_state_update(lv_event_t * e){
lv_obj_t * label = lv_event_get_target(e);
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){
lv_obj_clear_flag(panel, LV_OBJ_FLAG_SCROLLABLE);
const int button_size = 40;
const int button_size_vertical = 40;
const int button_padding = 2;
const int x_offset = 15;
int y_pos = 75;
auto panel_width = TFT_HEIGHT - 40;
lv_obj_t * home_button = lv_btn_create(panel);
lv_obj_align(home_button, LV_ALIGN_TOP_LEFT, 10, 5);
lv_obj_add_event_cb(home_button, home_button_click, LV_EVENT_CLICKED, NULL);
lv_obj_set_size(home_button, panel_width / 2 - 15, 30);
lv_obj_t * home_label = lv_label_create(home_button);
lv_label_set_text(home_label, LV_SYMBOL_HOME "Home Axis");
lv_obj_center(home_label);
lv_obj_t * disable_steppers_button = lv_btn_create(panel);
lv_obj_align(disable_steppers_button, LV_ALIGN_TOP_RIGHT, -10, 5);
lv_obj_add_event_cb(disable_steppers_button, disable_steppers_click, LV_EVENT_CLICKED, NULL);
lv_obj_set_size(disable_steppers_button, panel_width / 2 - 15, 30);
lv_obj_t * disable_steppers_label = lv_label_create(disable_steppers_button);
lv_label_set_text(disable_steppers_label, LV_SYMBOL_EYE_CLOSE "Disable Step");
lv_obj_center(disable_steppers_label);
lv_obj_t * label = lv_label_create(panel);
lv_label_set_text(label, "???");
lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 40);
lv_obj_add_event_cb(label, stepper_state_update, LV_EVENT_MSG_RECEIVED, NULL);
lv_msg_subsribe_obj(DATA_PRINTER_DATA, label, NULL);
for (int i = 0; i < 3; i++) {
lv_obj_t * btn = lv_btn_create(panel);
lv_obj_set_size(btn, button_size, button_size_vertical);
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, x_offset, y_pos);
lv_obj_add_event_cb(btn, button_callbacks[i], LV_EVENT_CLICKED, (void*)(offsets[i]));
lv_obj_t * label = lv_label_create(btn);
lv_label_set_text(label, offset_labels[i][0]);
lv_obj_center(label);
btn = lv_btn_create(panel);
lv_obj_set_size(btn, button_size, button_size_vertical);
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, x_offset + (button_size + button_padding) * 1, y_pos);
lv_obj_add_event_cb(btn, button_callbacks[i], LV_EVENT_CLICKED, (void*)(offsets[i] + 1));
label = lv_label_create(btn);
lv_label_set_text(label, offset_labels[i][1]);
lv_obj_center(label);
btn = lv_btn_create(panel);
lv_obj_set_size(btn, button_size, button_size_vertical);
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, x_offset + (button_size + button_padding) * 2, y_pos);
lv_obj_add_event_cb(btn, button_callbacks[i], LV_EVENT_CLICKED, (void*)(offsets[i] + 2));
label = lv_label_create(btn);
lv_label_set_text(label, offset_labels[i][2]);
lv_obj_center(label);
btn = lv_btn_create(panel);
lv_obj_set_size(btn, button_size, button_size_vertical);
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, x_offset + (button_size + button_padding) * 3, y_pos);
lv_obj_add_event_cb(btn, button_callbacks[i], LV_EVENT_CLICKED, (void*)(offsets[i] + 3));
label = lv_label_create(btn);
lv_label_set_text(label, offset_labels[i][3]);
lv_obj_center(label);
btn = lv_btn_create(panel);
lv_obj_set_size(btn, button_size, button_size_vertical);
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, x_offset + (button_size + button_padding) * 4, y_pos);
lv_obj_add_event_cb(btn, button_callbacks[i], LV_EVENT_CLICKED, (void*)(offsets[i] + 4));
label = lv_label_create(btn);
lv_label_set_text(label, offset_labels[i][4]);
lv_obj_center(label);
btn = lv_btn_create(panel);
lv_obj_set_size(btn, button_size, button_size_vertical);
lv_obj_align(btn, LV_ALIGN_TOP_LEFT, x_offset + (button_size + button_padding) * 5, y_pos);
lv_obj_add_event_cb(btn, button_callbacks[i], LV_EVENT_CLICKED, (void*)(offsets[i] + 5));
label = lv_label_create(btn);
lv_label_set_text(label, offset_labels[i][5]);
lv_obj_center(label);
label = lv_label_create(panel);
lv_label_set_text(label, "???");
lv_obj_align(label, LV_ALIGN_TOP_LEFT, x_offset, y_pos - 15);\
lv_obj_add_event_cb(label, position_callbacks[i], LV_EVENT_MSG_RECEIVED, NULL);
lv_msg_subsribe_obj(DATA_PRINTER_DATA, label, NULL);
y_pos += 60;
}
}
void move_panel_init(lv_obj_t* panel){
move_panel_data(panel);
}

View File

@@ -0,0 +1,6 @@
#include "lvgl.h"
void settings_panel_init(lv_obj_t* panel);
void temp_panel_init(lv_obj_t* panel);
void print_panel_init(lv_obj_t* panel);
void move_panel_init(lv_obj_t* panel);

View File

@@ -0,0 +1,6 @@
#include "lvgl.h"
#include "panel.h"
void print_panel_init(lv_obj_t* panel){
}

View File

@@ -0,0 +1,6 @@
#include "lvgl.h"
#include "panel.h"
void settings_panel_init(lv_obj_t* panel){
}

View File

@@ -0,0 +1,6 @@
#include "lvgl.h"
#include "panel.h"
void temp_panel_init(lv_obj_t* panel){
}