From 8f2c7ddc4bcd931890b6d64a60b43f1a7a3ca415 Mon Sep 17 00:00:00 2001 From: Peter Siegmund Date: Tue, 8 Aug 2023 17:03:53 +0200 Subject: [PATCH] starting with launch screen --- include/gfx/lgfx.h | 17 ++++++++ include/gfx/lv_setup.h | 4 ++ include/ui/launch_screen.h | 3 ++ platformio.ini | 2 +- src/gfx/lgfx.cpp | 83 +++++++++++++++++++++++++++++++++++++ src/gfx/lv_setup.cpp | 84 ++++++++++++++++++++++++++++++++++++++ src/main.cpp | 16 +++----- src/ui/launch_screen.cpp | 22 ++++++++++ 8 files changed, 219 insertions(+), 12 deletions(-) create mode 100644 include/gfx/lgfx.h create mode 100644 include/gfx/lv_setup.h create mode 100644 include/ui/launch_screen.h create mode 100644 src/gfx/lgfx.cpp create mode 100644 src/gfx/lv_setup.cpp create mode 100644 src/ui/launch_screen.cpp diff --git a/include/gfx/lgfx.h b/include/gfx/lgfx.h new file mode 100644 index 0000000..e71420c --- /dev/null +++ b/include/gfx/lgfx.h @@ -0,0 +1,17 @@ +#pragma once + +#define LGFX_USE_V1 + +#include + +class LGFX : public lgfx::LGFX_Device +{ +private: + lgfx::Panel_ST7796 _panel_instance; + lgfx::Bus_Parallel8 _bus_instance; + lgfx::Light_PWM _light_instance; + lgfx::Touch_FT5x06 _touch_instance; + +public: + LGFX(); +}; diff --git a/include/gfx/lv_setup.h b/include/gfx/lv_setup.h new file mode 100644 index 0000000..70acb64 --- /dev/null +++ b/include/gfx/lv_setup.h @@ -0,0 +1,4 @@ +#pragma once + +void lv_begin(); +void lv_handler(); diff --git a/include/ui/launch_screen.h b/include/ui/launch_screen.h new file mode 100644 index 0000000..0b21c59 --- /dev/null +++ b/include/ui/launch_screen.h @@ -0,0 +1,3 @@ +#pragma once + +void ui_LaunchScreen_open(); diff --git a/platformio.ini b/platformio.ini index 47729d9..5fcd4d1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -36,4 +36,4 @@ lib_ldf_mode = deep lib_deps = me-no-dev/AsyncTCP @ ^1.1.1 lovyan03/LovyanGFX @ ^1.1.8 - lvgl/lvgl @ ^8.3.7 + lvgl/lvgl @ ^8.3.9 diff --git a/src/gfx/lgfx.cpp b/src/gfx/lgfx.cpp new file mode 100644 index 0000000..586ebef --- /dev/null +++ b/src/gfx/lgfx.cpp @@ -0,0 +1,83 @@ +#include "gfx/lgfx.h" + +LGFX::LGFX() +{ + { + auto cfg = _bus_instance.config(); + + // cfg.i2s_port = I2S_NUM_0; + cfg.freq_write = 20000000; + cfg.pin_wr = 47; + cfg.pin_rd = -1; + cfg.pin_rs = 0; + cfg.pin_d0 = 9; + cfg.pin_d1 = 46; + cfg.pin_d2 = 3; + cfg.pin_d3 = 8; + cfg.pin_d4 = 18; + cfg.pin_d5 = 17; + cfg.pin_d6 = 16; + cfg.pin_d7 = 15; + + _bus_instance.config(cfg); + _panel_instance.setBus(&_bus_instance); + } + + { + auto cfg = _panel_instance.config(); + + cfg.pin_cs = -1; + cfg.pin_rst = 4; + cfg.pin_busy = -1; + cfg.panel_width = SCREEN_HEIGHT; + cfg.panel_height = SCREEN_WIDTH; + cfg.offset_x = 0; + cfg.offset_y = 0; + cfg.offset_rotation = 0; + cfg.dummy_read_pixel = 8; + cfg.dummy_read_bits = 1; + cfg.readable = true; + cfg.invert = true; + cfg.rgb_order = false; + cfg.dlen_16bit = false; + cfg.bus_shared = true; + cfg.memory_width = SCREEN_HEIGHT; + cfg.memory_height = SCREEN_WIDTH; + + _panel_instance.config(cfg); + } + + { + auto cfg = _light_instance.config(); + + cfg.pin_bl = 45; + cfg.invert = false; + cfg.freq = 44100; + cfg.pwm_channel = 7; + + _light_instance.config(cfg); + _panel_instance.setLight(&_light_instance); + } + + { + auto cfg = _touch_instance.config(); + + cfg.x_min = 0; + cfg.x_max = 319; + cfg.y_min = 0; + cfg.y_max = 479; + cfg.pin_int = 7; + cfg.bus_shared = true; + cfg.offset_rotation = 0; + cfg.i2c_port = 1; + cfg.i2c_addr = 0x38; + cfg.pin_sda = 6; + cfg.pin_scl = 5; + cfg.freq = 400000; + + _touch_instance.config(cfg); + _panel_instance.setTouch(&_touch_instance); + } + + setPanel(&_panel_instance); +} diff --git a/src/gfx/lv_setup.cpp b/src/gfx/lv_setup.cpp new file mode 100644 index 0000000..0e5fa51 --- /dev/null +++ b/src/gfx/lv_setup.cpp @@ -0,0 +1,84 @@ +#include "gfx/lv_setup.h" + +#include +#include + +#define LV_BUF_SIZE (SCREEN_WIDTH * SCREEN_HEIGHT) + +LGFX tft; + +void lv_handler() +{ + static uint32_t previousUpdate = 0; + static uint32_t interval = 0; + + if (millis() - previousUpdate > interval) + { + previousUpdate = millis(); + interval = lv_timer_handler(); // Update the UI + } +} + +void flush_cb(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p) +{ + uint32_t w = (area->x2 - area->x1 + 1); + uint32_t h = (area->y2 - area->y1 + 1); + + tft.startWrite(); + tft.setAddrWindow(area->x1, area->y1, w, h); + tft.writePixels((lgfx::rgb565_t *)&color_p->full, w * h); + tft.endWrite(); + + lv_disp_flush_ready(disp); +} + +void read_cb(lv_indev_drv_t *indev_driver, lv_indev_data_t *data) +{ + uint16_t touchX, touchY; + bool touched = tft.getTouch(&touchX, &touchY); + if (touched) + { + data->point.x = touchX; + data->point.y = touchY; + data->state = LV_INDEV_STATE_PR; + } + else + { + data->state = LV_INDEV_STATE_REL; + } +} + +void print_cb(const char *buf) +{ + log_d("%s", buf); +} + +void lv_begin() +{ + tft.init(); + tft.setRotation(1); + + lv_log_register_print_cb(print_cb); + + lv_init(); + + static lv_disp_draw_buf_t draw_buf; + static lv_color_t *buf1, *buf2; + buf1 = (lv_color_t *)heap_caps_malloc(LV_BUF_SIZE * sizeof(lv_color_t), MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM); + buf2 = (lv_color_t *)heap_caps_malloc(LV_BUF_SIZE * sizeof(lv_color_t), MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM); + lv_disp_draw_buf_init(&draw_buf, buf1, buf2, LV_BUF_SIZE); + + static lv_disp_drv_t disp_drv; + lv_disp_drv_init(&disp_drv); + disp_drv.hor_res = SCREEN_WIDTH; + disp_drv.ver_res = SCREEN_HEIGHT; + disp_drv.flush_cb = flush_cb; + disp_drv.draw_buf = &draw_buf; + lv_disp_drv_register(&disp_drv); + + static lv_indev_drv_t indev_drv; + lv_indev_drv_init(&indev_drv); + indev_drv.type = LV_INDEV_TYPE_POINTER; + indev_drv.read_cb = read_cb; + lv_indev_drv_register(&indev_drv); +} diff --git a/src/main.cpp b/src/main.cpp index b16c178..90f7c7d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,22 +18,16 @@ //---------------------------------------------------------------------------- #include -// put function declarations here: -int myFunction(int, int); +#include "gfx/lv_setup.h" +#include "ui/launch_screen.h" void setup() { - // put your setup code here, to run once: - int result = myFunction(2, 3); + lv_begin(); + ui_LaunchScreen_open(); } void loop() { - // put your main code here, to run repeatedly: -} - -// put function definitions here: -int myFunction(int x, int y) -{ - return x + y; + lv_handler(); } diff --git a/src/ui/launch_screen.cpp b/src/ui/launch_screen.cpp new file mode 100644 index 0000000..275d8db --- /dev/null +++ b/src/ui/launch_screen.cpp @@ -0,0 +1,22 @@ +#include "ui/launch_screen.h" + +#include + +lv_obj_t *ui_SplashScreen; + +void ui_LaunchScreen_open() +{ + // Create a screen + ui_SplashScreen = lv_obj_create(NULL); + lv_obj_clear_flag(ui_SplashScreen, LV_OBJ_FLAG_SCROLLABLE); + lv_obj_set_style_bg_color(ui_SplashScreen, lv_color_make(0x78, 0x94, 0xa7), 0); + + // Create a label + lv_obj_t *label = lv_label_create(ui_SplashScreen); + lv_label_set_text(label, "Hello world!"); + + // Align the label to the center + lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0); + + lv_disp_load_scr(ui_SplashScreen); +}