diff --git a/CYD-Klipper-Display/src/conf/global_config.cpp b/CYD-Klipper-Display/src/conf/global_config.cpp index da3b692..a23e3c3 100644 --- a/CYD-Klipper-Display/src/conf/global_config.cpp +++ b/CYD-Klipper-Display/src/conf/global_config.cpp @@ -39,11 +39,11 @@ void VerifyVersion(){ void LoadGlobalConfig() { global_config.version = CONFIG_VERSION; + global_config.brightness = 255; + global_config.screenTimeout = 5; VerifyVersion(); Preferences preferences; preferences.begin("global_config", true); preferences.getBytes("global_config", &global_config, sizeof(global_config)); preferences.end(); - - Serial.printf("Touch: %d\n", global_config.screenCalibrated); } \ No newline at end of file diff --git a/CYD-Klipper-Display/src/conf/global_config.h b/CYD-Klipper-Display/src/conf/global_config.h index fedfd73..05f510e 100644 --- a/CYD-Klipper-Display/src/conf/global_config.h +++ b/CYD-Klipper-Display/src/conf/global_config.h @@ -3,7 +3,7 @@ #include "lvgl.h" -#define CONFIG_VERSION 80 +#define CONFIG_VERSION 2 typedef struct _GLOBAL_CONFIG { unsigned char version; @@ -15,6 +15,8 @@ typedef struct _GLOBAL_CONFIG { bool ipConfigured : 1; bool lightMode : 1; bool invertColors : 1; + bool rotateScreen : 1; + bool onDuringPrint : 1; }; }; float screenCalXOffset; @@ -29,6 +31,8 @@ typedef struct _GLOBAL_CONFIG { unsigned short klipperPort; unsigned char color_scheme; + unsigned char brightness; + unsigned char screenTimeout; } GLOBAL_CONFIG; typedef struct _COLOR_DEF { diff --git a/CYD-Klipper-Display/src/core/screen_driver.cpp b/CYD-Klipper-Display/src/core/screen_driver.cpp index 660c6e2..8b6adb7 100644 --- a/CYD-Klipper-Display/src/core/screen_driver.cpp +++ b/CYD-Klipper-Display/src/core/screen_driver.cpp @@ -17,14 +17,16 @@ TFT_eSPI tft = TFT_eSPI(); bool isScreenInSleep = false; lv_timer_t *screenSleepTimer; -TS_Point touchscreen_point(){ +TS_Point touchscreen_point() +{ TS_Point p = touchscreen.getPoint(); p.x = round((p.x * global_config.screenCalXMult) + global_config.screenCalXOffset); p.y = round((p.y * global_config.screenCalYMult) + global_config.screenCalYOffset); return p; } -void touchscreen_calibrate(bool force){ +void touchscreen_calibrate(bool force) +{ if (global_config.screenCalibrated && !force) { return; @@ -85,11 +87,19 @@ void screen_setBrightness(byte brightness) analogWrite(TFT_BL, brightness); } +void set_screen_brightness() +{ + if (global_config.brightness < 32) + screen_setBrightness(255); + else + screen_setBrightness(global_config.brightness); +} + void screen_timer_wake() { lv_timer_reset(screenSleepTimer); isScreenInSleep = false; - screen_setBrightness(255); + set_screen_brightness(); } void screen_timer_sleep(lv_timer_t *timer) @@ -100,7 +110,7 @@ void screen_timer_sleep(lv_timer_t *timer) void screen_timer_setup() { - screenSleepTimer = lv_timer_create(screen_timer_sleep, 5 * 1000 * 60, NULL); + screenSleepTimer = lv_timer_create(screen_timer_sleep, global_config.screenTimeout * 1000 * 60, NULL); lv_timer_pause(screenSleepTimer); } @@ -119,6 +129,11 @@ void screen_timer_period(uint32_t period) lv_timer_set_period(screenSleepTimer, period); } +void set_screen_timer_period() +{ + screen_timer_period(global_config.screenTimeout * 1000 * 60); +} + void screen_lv_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) { uint32_t w = (area->x2 - area->x1 + 1); @@ -172,13 +187,14 @@ void screen_setup() { touchscreen_spi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS); touchscreen.begin(touchscreen_spi); - touchscreen.setRotation(1); + touchscreen.setRotation(global_config.rotateScreen ? 3 : 1); lv_init(); tft.init(); - tft.setRotation(1); + tft.setRotation(global_config.rotateScreen ? 3 : 1); tft.fillScreen(TFT_BLACK); + set_screen_brightness(); set_invert_display(); touchscreen_spi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS); diff --git a/CYD-Klipper-Display/src/core/screen_driver.h b/CYD-Klipper-Display/src/core/screen_driver.h index 2f95db3..6b68fcd 100644 --- a/CYD-Klipper-Display/src/core/screen_driver.h +++ b/CYD-Klipper-Display/src/core/screen_driver.h @@ -23,6 +23,9 @@ void screen_timer_period(uint32_t period); void set_color_scheme(); void screen_setup(); void set_invert_display(); +void screen_timer_wake(); +void set_screen_timer_period(); +void set_screen_brightness(); extern TFT_eSPI tft; diff --git a/CYD-Klipper-Display/src/ui/main_ui.cpp b/CYD-Klipper-Display/src/ui/main_ui.cpp index 19d65d9..cc37bc1 100644 --- a/CYD-Klipper-Display/src/ui/main_ui.cpp +++ b/CYD-Klipper-Display/src/ui/main_ui.cpp @@ -1,13 +1,22 @@ #include "main_ui.h" #include "../core/data_setup.h" +#include "../conf/global_config.h" +#include "../core/screen_driver.h" #include "lvgl.h" #include "nav_buttons.h" -#include char extruder_temp_buff[20]; char bed_temp_buff[20]; char position_buff[20]; +static void btn_click_restart(lv_event_t * e){ + send_gcode(false, "RESTART"); +} + +static void btn_click_firmware_restart(lv_event_t * e){ + send_gcode(false, "FIRMWARE_RESTART"); +} + void error_ui(){ lv_obj_clean(lv_scr_act()); @@ -22,9 +31,39 @@ void error_ui(){ 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); + + lv_obj_t * btn = lv_btn_create(lv_scr_act()); + lv_obj_align(btn, LV_ALIGN_BOTTOM_LEFT, 10, -10); + lv_obj_set_size(btn, TFT_HEIGHT / 2 - 15, 30); + lv_obj_add_event_cb(btn, btn_click_restart, LV_EVENT_CLICKED, NULL); + + label = lv_label_create(btn); + lv_label_set_text(label, "Restart"); + lv_obj_center(label); + + btn = lv_btn_create(lv_scr_act()); + lv_obj_align(btn, LV_ALIGN_BOTTOM_RIGHT, -10, -10); + lv_obj_set_size(btn, TFT_HEIGHT / 2 - 15, 30); + lv_obj_add_event_cb(btn, btn_click_firmware_restart, LV_EVENT_CLICKED, NULL); + + label = lv_label_create(btn); + lv_label_set_text(label, "Firmware Restart"); + lv_obj_center(label); +} + +void check_if_screen_needs_to_be_disabled(){ + if (global_config.onDuringPrint && printer.state == PRINTER_STATE_PRINTING){ + screen_timer_wake(); + screen_timer_stop(); + } + else { + screen_timer_start(); + } } static void on_state_change(void * s, lv_msg_t * m){ + check_if_screen_needs_to_be_disabled(); + if (printer.state == PRINTER_STATE_ERROR){ error_ui(); } diff --git a/CYD-Klipper-Display/src/ui/main_ui.h b/CYD-Klipper-Display/src/ui/main_ui.h index 3cebe7b..6aa8fcb 100644 --- a/CYD-Klipper-Display/src/ui/main_ui.h +++ b/CYD-Klipper-Display/src/ui/main_ui.h @@ -1 +1,2 @@ -void main_ui_setup(); \ No newline at end of file +void main_ui_setup(); +void check_if_screen_needs_to_be_disabled(); \ No newline at end of file diff --git a/CYD-Klipper-Display/src/ui/nav_buttons.cpp b/CYD-Klipper-Display/src/ui/nav_buttons.cpp index 0687d91..022ba83 100644 --- a/CYD-Klipper-Display/src/ui/nav_buttons.cpp +++ b/CYD-Klipper-Display/src/ui/nav_buttons.cpp @@ -97,7 +97,7 @@ void nav_buttons_setup(unsigned char active_panel){ lv_obj_align(label, LV_ALIGN_CENTER, 0, icon_text_spacing); lv_obj_add_style(label, &nav_button_text_style, 0); lv_obj_add_event_cb(label, update_printer_data_time, LV_EVENT_MSG_RECEIVED, NULL); - lv_msg_subsribe_obj(DATA_PRINTER_STATE, label, NULL); + lv_msg_subsribe_obj(DATA_PRINTER_DATA, label, NULL); // Move btn = lv_btn_create(lv_scr_act()); diff --git a/CYD-Klipper-Display/src/ui/panels/panel.h b/CYD-Klipper-Display/src/ui/panels/panel.h index 7b2aaa0..251f833 100644 --- a/CYD-Klipper-Display/src/ui/panels/panel.h +++ b/CYD-Klipper-Display/src/ui/panels/panel.h @@ -1,5 +1,7 @@ #include "lvgl.h" +#define SIZEOF(arr) (sizeof(arr) / sizeof(*arr)) + void settings_panel_init(lv_obj_t* panel); void temp_panel_init(lv_obj_t* panel); void print_panel_init(lv_obj_t* panel); diff --git a/CYD-Klipper-Display/src/ui/panels/settings_panel.cpp b/CYD-Klipper-Display/src/ui/panels/settings_panel.cpp index c2d2069..c7404f3 100644 --- a/CYD-Klipper-Display/src/ui/panels/settings_panel.cpp +++ b/CYD-Klipper-Display/src/ui/panels/settings_panel.cpp @@ -2,6 +2,7 @@ #include "panel.h" #include "../../core/screen_driver.h" #include "../../conf/global_config.h" +#include "../main_ui.h" static void invert_color_switch(lv_event_t * e){ auto state = lv_obj_get_state(lv_event_get_target(e)); @@ -36,64 +37,164 @@ static void theme_dropdown(lv_event_t * e){ lv_obj_t * dropdown = lv_event_get_target(e); auto selected = lv_dropdown_get_selected(dropdown); global_config.color_scheme = selected; - WriteGlobalConfig(); set_color_scheme(); + WriteGlobalConfig(); +} + +const char* brightness_options = "100%\n75%\n50%\n25%"; +const char brightness_options_values[] = { 255, 192, 128, 64 }; + +static void brightness_dropdown(lv_event_t * e){ + lv_obj_t * dropdown = lv_event_get_target(e); + auto selected = lv_dropdown_get_selected(dropdown); + global_config.brightness = brightness_options_values[selected]; + set_screen_brightness(); + WriteGlobalConfig(); +} + +const char* wake_timeout_options = "1m\n2m\n5m\n10m\n15m\n30m\n1h\n2h\n4h"; +const char wake_timeout_options_values[] = { 1, 2, 5, 10, 15, 30, 60, 120, 240 }; + +static void wake_timeout_dropdown(lv_event_t * e){ + lv_obj_t * dropdown = lv_event_get_target(e); + auto selected = lv_dropdown_get_selected(dropdown); + global_config.screenTimeout = wake_timeout_options_values[selected]; + set_screen_timer_period(); + WriteGlobalConfig(); +} + +static void rotate_screen_switch(lv_event_t* e){ + auto state = lv_obj_get_state(lv_event_get_target(e)); + bool checked = (state & LV_STATE_CHECKED == LV_STATE_CHECKED); + global_config.rotateScreen = checked; + global_config.screenCalibrated = false; + WriteGlobalConfig(); + ESP.restart(); +} + +static void on_during_print_switch(lv_event_t* e){ + auto state = lv_obj_get_state(lv_event_get_target(e)); + bool checked = (state & LV_STATE_CHECKED == LV_STATE_CHECKED); + global_config.onDuringPrint = checked; + check_if_screen_needs_to_be_disabled(); + WriteGlobalConfig(); +} + +int y_offset = 0; +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} }; + +void create_settings_widget(const char* label_text, lv_obj_t* object, 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); + 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, label_text); + lv_obj_align(label, LV_ALIGN_LEFT_MID, 0, 0); + + lv_obj_set_parent(object, panel); + lv_obj_align(object, LV_ALIGN_RIGHT_MID, 0, 0); + y_offset += y_element_size; } void settings_panel_init(lv_obj_t* panel){ - auto panel_width = TFT_HEIGHT - 40; + y_offset = 0; lv_obj_t * btn = lv_btn_create(panel); - lv_obj_align(btn, LV_ALIGN_TOP_LEFT, 10, 5); lv_obj_add_event_cb(btn, reset_wifi_click, LV_EVENT_CLICKED, NULL); - lv_obj_set_size(btn, panel_width / 2 - 15, 30); lv_obj_t * label = lv_label_create(btn); - lv_label_set_text(label, "WiFi Setup"); + lv_label_set_text(label, "Restart"); lv_obj_center(label); + create_settings_widget("Configure WiFi", btn, panel); + btn = lv_btn_create(panel); - lv_obj_align(btn, LV_ALIGN_TOP_RIGHT, -10, 5); lv_obj_add_event_cb(btn, reset_calibration_click, LV_EVENT_CLICKED, NULL); - lv_obj_set_size(btn, panel_width / 2 - 15, 30); label = lv_label_create(btn); - lv_label_set_text(label, "Touch Cal"); + lv_label_set_text(label, "Restart"); lv_obj_center(label); + create_settings_widget("Calibrate Touch", btn, panel); lv_obj_t * toggle = lv_switch_create(panel); - lv_obj_align(toggle, LV_ALIGN_TOP_RIGHT, -14, 57); lv_obj_add_event_cb(toggle, invert_color_switch, LV_EVENT_VALUE_CHANGED, NULL); if (global_config.invertColors) lv_obj_add_state(toggle, LV_STATE_CHECKED); - label = lv_label_create(panel); - lv_label_set_text(label, "Invert Colors"); - lv_obj_align(label, LV_ALIGN_TOP_RIGHT, -10, 40); - lv_obj_set_style_text_font(label, &lv_font_montserrat_10, 0); + create_settings_widget("Invert Colors", toggle, panel); + toggle = lv_switch_create(panel); - lv_obj_align(toggle, LV_ALIGN_TOP_LEFT, 13, 57); lv_obj_add_event_cb(toggle, light_mode_switch, LV_EVENT_VALUE_CHANGED, NULL); if (global_config.lightMode) lv_obj_add_state(toggle, LV_STATE_CHECKED); - label = lv_label_create(panel); - lv_label_set_text(label, "Light Mode"); - lv_obj_align(label, LV_ALIGN_TOP_LEFT, 10, 40); - lv_obj_set_style_text_font(label, &lv_font_montserrat_10, 0); + create_settings_widget("Light Mode", toggle, panel); lv_obj_t * dropdown = lv_dropdown_create(panel); lv_dropdown_set_options(dropdown, "Blue\nGreen\nGrey\nYellow\nOrange\nRed\nPurple"); lv_dropdown_set_selected(dropdown, global_config.color_scheme); - lv_obj_align(dropdown, LV_ALIGN_TOP_MID, 0, 55); lv_obj_add_event_cb(dropdown, theme_dropdown, LV_EVENT_VALUE_CHANGED, NULL); - label = lv_label_create(panel); - lv_label_set_text(label, "Theme"); - lv_obj_align(label, LV_ALIGN_TOP_MID, 0, 40); - lv_obj_set_style_text_font(label, &lv_font_montserrat_10, 0); + create_settings_widget("Theme", dropdown, panel); + + dropdown = lv_dropdown_create(panel); + lv_dropdown_set_options(dropdown, brightness_options); + lv_obj_add_event_cb(dropdown, brightness_dropdown, LV_EVENT_VALUE_CHANGED, NULL); + + for (int i = 0; i < SIZEOF(brightness_options_values); i++){ + if (brightness_options_values[i] == global_config.brightness){ + lv_dropdown_set_selected(dropdown, i); + break; + } + } + + create_settings_widget("Brightness", dropdown, panel); + + dropdown = lv_dropdown_create(panel); + lv_dropdown_set_options(dropdown, wake_timeout_options); + lv_obj_add_event_cb(dropdown, wake_timeout_dropdown, LV_EVENT_VALUE_CHANGED, NULL); + + for (int i = 0; i < SIZEOF(wake_timeout_options_values); i++){ + if (wake_timeout_options_values[i] == global_config.screenTimeout){ + lv_dropdown_set_selected(dropdown, i); + break; + } + } + + create_settings_widget("Wake Timeout", dropdown, panel); + + toggle = lv_switch_create(panel); + lv_obj_add_event_cb(toggle, rotate_screen_switch, LV_EVENT_VALUE_CHANGED, NULL); + + if (global_config.rotateScreen) + lv_obj_add_state(toggle, LV_STATE_CHECKED); + + create_settings_widget("Rotate Screen", toggle, panel); + + toggle = lv_switch_create(panel); + lv_obj_add_event_cb(toggle, on_during_print_switch, LV_EVENT_VALUE_CHANGED, NULL); + + if (global_config.onDuringPrint) + lv_obj_add_state(toggle, LV_STATE_CHECKED); + + create_settings_widget("Screen On During Print", toggle, panel); } \ No newline at end of file diff --git a/CYD-Klipper-Display/src/ui/wifi_setup.cpp b/CYD-Klipper-Display/src/ui/wifi_setup.cpp index ddaea2f..9907983 100644 --- a/CYD-Klipper-Display/src/ui/wifi_setup.cpp +++ b/CYD-Klipper-Display/src/ui/wifi_setup.cpp @@ -154,6 +154,6 @@ void wifi_init(){ void wifi_ok(){ if (WiFi.status() != WL_CONNECTED){ - wifi_init(); + ESP.restart(); } } \ No newline at end of file