diff --git a/CYD-Klipper/.vscode/settings.json b/CYD-Klipper/.vscode/settings.json index 75d2259..4d652ee 100644 --- a/CYD-Klipper/.vscode/settings.json +++ b/CYD-Klipper/.vscode/settings.json @@ -9,6 +9,7 @@ "vector": "cpp", "string_view": "cpp", "initializer_list": "cpp", - "algorithm": "cpp" + "algorithm": "cpp", + "cstddef": "cpp" } } \ No newline at end of file diff --git a/CYD-Klipper/platformio.ini b/CYD-Klipper/platformio.ini index a4b28d5..81b35c9 100644 --- a/CYD-Klipper/platformio.ini +++ b/CYD-Klipper/platformio.ini @@ -41,3 +41,25 @@ build_flags = -DSPI_FREQUENCY=55000000 -DSPI_READ_FREQUENCY=20000000 -DSPI_TOUCH_FREQUENCY=2500000 + + ### Porting options ### + # Defines the screen height + -DCYD_SCREEN_HEIGHT=240 + # Defines the screen width + -DCYD_SCREEN_WIDTH=320 + # Defines the pixel gap used for large gaps (like between buttons) + -DCYD_SCREEN_BIG_GAP_PX=8 + # Defines the pixel gap used for small gaps (like between text and buttons) + -DCYD_SCREEN_SMALL_GAP_PX=4 + # Defines the minimum pixel height of a button + -DCYD_SCREEN_MIN_BUTTON_HEIGHT=35 + # Defines the minimum pixel width of a button + -DCYD_SCREEN_MIN_BUTTON_WIDTH=35 + # Defines the size of font used + -DCYD_SCREEN_FONT=&lv_font_montserrat_14 + # Defines the size of font used for small text + -DCYD_SCREEN_FONT_SMALL=&lv_font_montserrat_10 + # Defines the size of the sizebar + -DCYD_SCREEN_SIDEBAR_SIZE_PX=40 + # Defines the screen driver + -DCYD_SCREEN_DRIVER_ESP32_2432S028R=1 \ No newline at end of file diff --git a/CYD-Klipper/src/core/screen_driver.cpp b/CYD-Klipper/src/core/device/ESP32-2432S028R.cpp similarity index 95% rename from CYD-Klipper/src/core/screen_driver.cpp rename to CYD-Klipper/src/core/device/ESP32-2432S028R.cpp index ae06f78..99347b8 100644 --- a/CYD-Klipper/src/core/screen_driver.cpp +++ b/CYD-Klipper/src/core/device/ESP32-2432S028R.cpp @@ -1,8 +1,19 @@ -#include "screen_driver.h" +#include "ESP32-2432S028R.h" + #include #include -#include "../conf/global_config.h" +#include "../../conf/global_config.h" #include "lvgl.h" +#include +#include + +#define XPT2046_IRQ 36 +#define XPT2046_MOSI 32 +#define XPT2046_MISO 39 +#define XPT2046_CLK 25 +#define XPT2046_CS 33 +#define CPU_FREQ_HIGH 240 +#define CPU_FREQ_LOW 80 SPIClass touchscreen_spi = SPIClass(HSPI); XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ); @@ -229,7 +240,7 @@ void screen_setup() touchscreen_spi.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS); touchscreen.begin(touchscreen_spi); - touchscreen_calibrate(); + touchscreen_calibrate(false); lv_disp_draw_buf_init(&draw_buf, buf, NULL, TFT_WIDTH * TFT_HEIGHT / 10); diff --git a/CYD-Klipper/src/core/device/ESP32-2432S028R.h b/CYD-Klipper/src/core/device/ESP32-2432S028R.h new file mode 100644 index 0000000..7b9637e --- /dev/null +++ b/CYD-Klipper/src/core/device/ESP32-2432S028R.h @@ -0,0 +1 @@ +#pragma once \ No newline at end of file diff --git a/CYD-Klipper/src/core/screen_driver.h b/CYD-Klipper/src/core/screen_driver.h index 90f808e..1e6380c 100644 --- a/CYD-Klipper/src/core/screen_driver.h +++ b/CYD-Klipper/src/core/screen_driver.h @@ -1,35 +1,21 @@ #pragma once // Adapted from https://github.com/xperiments-in/xtouch/blob/main/src/devices/2.8/screen.h -#ifndef _SCREEN_DRIVER_INIT -#define _SCREEN_DRIVER_INIT +#ifdef CYD_SCREEN_DRIVER_ESP32_2432S028R + #include "device/ESP32-2432S028R.h" +#else + #error "No screen driver defined" +#endif -#define CPU_FREQ_HIGH 240 -#define CPU_FREQ_LOW 80 - -#include -#include - -#define XPT2046_IRQ 36 -#define XPT2046_MOSI 32 -#define XPT2046_MISO 39 -#define XPT2046_CLK 25 -#define XPT2046_CS 33 - -TS_Point touchscreen_point(); void touchscreen_calibrate(bool force = false); -void screen_setBrightness(byte brightness); +void screen_setBrightness(unsigned char brightness); void screen_timer_setup(); void screen_timer_start(); void screen_timer_stop(); -void screen_timer_period(uint32_t period); +void screen_timer_period(unsigned int 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; - -#endif // _SCREEN_DRIVER_INIT \ No newline at end of file +void set_screen_brightness(); \ No newline at end of file diff --git a/CYD-Klipper/src/main.cpp b/CYD-Klipper/src/main.cpp index a517416..4b6b0af 100644 --- a/CYD-Klipper/src/main.cpp +++ b/CYD-Klipper/src/main.cpp @@ -6,6 +6,7 @@ #include "core/data_setup.h" #include "ui/main_ui.h" #include "ui/nav_buttons.h" +#include static void event_handler(lv_event_t * e){ lv_event_code_t code = lv_event_get_code(e); diff --git a/CYD-Klipper/src/ui/panels/macros_panel.cpp b/CYD-Klipper/src/ui/panels/macros_panel.cpp index 16348fb..ebb2c07 100644 --- a/CYD-Klipper/src/ui/panels/macros_panel.cpp +++ b/CYD-Klipper/src/ui/panels/macros_panel.cpp @@ -3,15 +3,10 @@ #include "../nav_buttons.h" #include "../../core/data_setup.h" #include "../../core/macros_query.h" +#include "../ui_utils.h" #include -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} }; +const static lv_point_t line_points[] = { {0, 0}, {(short int)((CYD_SCREEN_PANEL_WIDTH - CYD_SCREEN_BIG_GAP_PX * 2) * 0.85f), 0} }; static void btn_press(lv_event_t * e){ lv_obj_t * btn = lv_event_get_target(e); @@ -24,44 +19,11 @@ 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_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR); - lv_obj_set_width(label, (TFT_HEIGHT - 40) * 0.75f); - - 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_set_size(btn, CYD_SCREEN_PANEL_WIDTH - CYD_SCREEN_BIG_GAP_PX * 2, CYD_SCREEN_MIN_BUTTON_HEIGHT); + lv_obj_align(btn, LV_ALIGN_TOP_MID, 0, CYD_SCREEN_BIG_GAP_PX); lv_obj_t * label = lv_label_create(btn); lv_label_set_text(label, LV_SYMBOL_SETTINGS " Screen Settings"); @@ -75,7 +37,42 @@ void macros_panel_init(lv_obj_t* panel) { return; } + lv_obj_t * root_panel = lv_create_empty_panel(panel); + lv_obj_set_size(root_panel, CYD_SCREEN_PANEL_WIDTH, CYD_SCREEN_HEIGHT - CYD_SCREEN_MIN_BUTTON_HEIGHT - CYD_SCREEN_BIG_GAP_PX); + lv_obj_align(root_panel, LV_ALIGN_TOP_MID, 0, CYD_SCREEN_MIN_BUTTON_HEIGHT + CYD_SCREEN_BIG_GAP_PX); + lv_obj_set_layout(root_panel, LV_LAYOUT_FLEX); + lv_obj_set_flex_flow(root_panel, LV_FLEX_FLOW_COLUMN); + lv_obj_set_flex_align(root_panel, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); + lv_obj_set_style_pad_column(root_panel, 0, 0); + lv_obj_set_style_pad_row(root_panel, 0, 0); + + for (int j = 0; j < 2; j++) for (int i = 0; i < query.count; i++){ - create_macro_widget(query.macros[i], panel); + const char* macro = query.macros[i]; + + lv_obj_t * panel = lv_create_empty_panel(root_panel); + lv_obj_set_size(panel, CYD_SCREEN_PANEL_WIDTH - CYD_SCREEN_BIG_GAP_PX * 3, CYD_SCREEN_MIN_BUTTON_HEIGHT + CYD_SCREEN_BIG_GAP_PX * 2); + + lv_obj_t * line = lv_line_create(panel); + lv_line_set_points(line, line_points, 2); + lv_obj_set_style_line_width(line, 1, 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_label_set_long_mode(label, LV_LABEL_LONG_SCROLL_CIRCULAR); + // TODO: Hack. Needs to be fixed for proper porting + lv_obj_set_width(label, CYD_SCREEN_PANEL_WIDTH * 0.75f); + + lv_obj_t * btn = lv_btn_create(panel); + lv_obj_set_height(btn, CYD_SCREEN_MIN_BUTTON_HEIGHT); + 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); } } \ No newline at end of file diff --git a/CYD-Klipper/src/ui/panels/settings_panel.cpp b/CYD-Klipper/src/ui/panels/settings_panel.cpp index c7404f3..53b2797 100644 --- a/CYD-Klipper/src/ui/panels/settings_panel.cpp +++ b/CYD-Klipper/src/ui/panels/settings_panel.cpp @@ -3,6 +3,8 @@ #include "../../core/screen_driver.h" #include "../../conf/global_config.h" #include "../main_ui.h" +#include "../ui_utils.h" +#include static void invert_color_switch(lv_event_t * e){ auto state = lv_obj_get_state(lv_event_get_target(e)); @@ -80,25 +82,20 @@ static void on_during_print_switch(lv_event_t* e){ 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} }; +const static lv_point_t line_points[] = { {0, 0}, {(short int)((CYD_SCREEN_PANEL_WIDTH - CYD_SCREEN_BIG_GAP_PX * 2) * 0.85f), 0} }; void create_settings_widget(const char* label_text, lv_obj_t* object, lv_obj_t* root_panel){ + lv_obj_set_height(object, CYD_SCREEN_MIN_BUTTON_HEIGHT); + 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_set_size(panel, CYD_SCREEN_PANEL_WIDTH - CYD_SCREEN_BIG_GAP_PX * 3, CYD_SCREEN_MIN_BUTTON_HEIGHT + CYD_SCREEN_BIG_GAP_PX * 2); 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_width(line, 1, 0); lv_obj_set_style_line_color(line, lv_color_hex(0xAAAAAA), 0); lv_obj_align(line, LV_ALIGN_BOTTOM_MID, 0, 0); @@ -108,11 +105,15 @@ void create_settings_widget(const char* label_text, lv_obj_t* object, lv_obj_t* 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){ - y_offset = 0; + + lv_obj_set_layout(panel, LV_LAYOUT_FLEX); + lv_obj_set_flex_flow(panel, LV_FLEX_FLOW_COLUMN); + lv_obj_set_flex_align(panel, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER); + lv_obj_set_style_pad_column(panel, 0, 0); + lv_obj_set_style_pad_row(panel, 0, 0); lv_obj_t * btn = lv_btn_create(panel); lv_obj_add_event_cb(btn, reset_wifi_click, LV_EVENT_CLICKED, NULL); diff --git a/CYD-Klipper/src/ui/ui_utils.cpp b/CYD-Klipper/src/ui/ui_utils.cpp new file mode 100644 index 0000000..8c1075d --- /dev/null +++ b/CYD-Klipper/src/ui/ui_utils.cpp @@ -0,0 +1,10 @@ +#include "lvgl.h" +#include "ui_utils.h" + +lv_obj_t* lv_create_empty_panel(lv_obj_t* root) { + lv_obj_t* panel = lv_obj_create(root); + 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); + return panel; +} \ No newline at end of file diff --git a/CYD-Klipper/src/ui/ui_utils.h b/CYD-Klipper/src/ui/ui_utils.h new file mode 100644 index 0000000..381b8a5 --- /dev/null +++ b/CYD-Klipper/src/ui/ui_utils.h @@ -0,0 +1,6 @@ +#pragma once + +#define CYD_SCREEN_PANEL_WIDTH \ + (CYD_SCREEN_WIDTH - CYD_SCREEN_SIDEBAR_SIZE_PX) + +lv_obj_t* lv_create_empty_panel(lv_obj_t* root); \ No newline at end of file