|
|
|
|
@@ -10,22 +10,134 @@
|
|
|
|
|
#define TAG "St7789Display"
|
|
|
|
|
#define LCD_LEDC_CH LEDC_CHANNEL_0
|
|
|
|
|
|
|
|
|
|
#define ST7789_LVGL_TICK_PERIOD_MS 2
|
|
|
|
|
#define ST7789_LVGL_TASK_MAX_DELAY_MS 20
|
|
|
|
|
#define ST7789_LVGL_TASK_MIN_DELAY_MS 1
|
|
|
|
|
#define ST7789_LVGL_TASK_STACK_SIZE (4 * 1024)
|
|
|
|
|
#define ST7789_LVGL_TASK_PRIORITY 10
|
|
|
|
|
|
|
|
|
|
LV_FONT_DECLARE(font_puhui_14_1);
|
|
|
|
|
LV_FONT_DECLARE(font_awesome_30_1);
|
|
|
|
|
LV_FONT_DECLARE(font_awesome_14_1);
|
|
|
|
|
|
|
|
|
|
static SemaphoreHandle_t lvgl_mux = NULL;
|
|
|
|
|
static lv_disp_drv_t disp_drv;
|
|
|
|
|
static void st7789_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
|
|
|
|
|
{
|
|
|
|
|
esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t)drv->user_data;
|
|
|
|
|
int offsetx1 = area->x1;
|
|
|
|
|
int offsetx2 = area->x2;
|
|
|
|
|
int offsety1 = area->y1;
|
|
|
|
|
int offsety2 = area->y2;
|
|
|
|
|
// copy a buffer's content to a specific area of the display
|
|
|
|
|
esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
|
|
|
|
|
lv_disp_flush_ready(&disp_drv);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Rotate display and touch, when rotated screen in LVGL. Called when driver parameters are updated. */
|
|
|
|
|
static void st7789_lvgl_port_update_callback(lv_disp_drv_t *drv)
|
|
|
|
|
{
|
|
|
|
|
esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t)drv->user_data;
|
|
|
|
|
|
|
|
|
|
switch (drv->rotated)
|
|
|
|
|
{
|
|
|
|
|
case LV_DISP_ROT_NONE:
|
|
|
|
|
// Rotate LCD display
|
|
|
|
|
esp_lcd_panel_swap_xy(panel_handle, false);
|
|
|
|
|
esp_lcd_panel_mirror(panel_handle, true, false);
|
|
|
|
|
#if CONFIG_ST7789_LCD_TOUCH_ENABLED
|
|
|
|
|
// Rotate LCD touch
|
|
|
|
|
esp_lcd_touch_set_mirror_y(tp, false);
|
|
|
|
|
esp_lcd_touch_set_mirror_x(tp, false);
|
|
|
|
|
#endif
|
|
|
|
|
break;
|
|
|
|
|
case LV_DISP_ROT_90:
|
|
|
|
|
// Rotate LCD display
|
|
|
|
|
esp_lcd_panel_swap_xy(panel_handle, true);
|
|
|
|
|
esp_lcd_panel_mirror(panel_handle, true, true);
|
|
|
|
|
#if CONFIG_ST7789_LCD_TOUCH_ENABLED
|
|
|
|
|
// Rotate LCD touch
|
|
|
|
|
esp_lcd_touch_set_mirror_y(tp, false);
|
|
|
|
|
esp_lcd_touch_set_mirror_x(tp, false);
|
|
|
|
|
#endif
|
|
|
|
|
break;
|
|
|
|
|
case LV_DISP_ROT_180:
|
|
|
|
|
// Rotate LCD display
|
|
|
|
|
esp_lcd_panel_swap_xy(panel_handle, false);
|
|
|
|
|
esp_lcd_panel_mirror(panel_handle, false, true);
|
|
|
|
|
#if CONFIG_ST7789_LCD_TOUCH_ENABLED
|
|
|
|
|
// Rotate LCD touch
|
|
|
|
|
esp_lcd_touch_set_mirror_y(tp, false);
|
|
|
|
|
esp_lcd_touch_set_mirror_x(tp, false);
|
|
|
|
|
#endif
|
|
|
|
|
break;
|
|
|
|
|
case LV_DISP_ROT_270:
|
|
|
|
|
// Rotate LCD display
|
|
|
|
|
esp_lcd_panel_swap_xy(panel_handle, true);
|
|
|
|
|
esp_lcd_panel_mirror(panel_handle, false, false);
|
|
|
|
|
#if CONFIG_ST7789_LCD_TOUCH_ENABLED
|
|
|
|
|
// Rotate LCD touch
|
|
|
|
|
esp_lcd_touch_set_mirror_y(tp, false);
|
|
|
|
|
esp_lcd_touch_set_mirror_x(tp, false);
|
|
|
|
|
#endif
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
static void st7789_increase_lvgl_tick(void *arg)
|
|
|
|
|
{
|
|
|
|
|
/* Tell LVGL how many milliseconds has elapsed */
|
|
|
|
|
lv_tick_inc(ST7789_LVGL_TICK_PERIOD_MS);
|
|
|
|
|
}
|
|
|
|
|
static bool st7789_lvgl_lock(int timeout_ms)
|
|
|
|
|
{
|
|
|
|
|
// Convert timeout in milliseconds to FreeRTOS ticks
|
|
|
|
|
// If `timeout_ms` is set to -1, the program will block until the condition is met
|
|
|
|
|
const TickType_t timeout_ticks = (timeout_ms == -1) ? portMAX_DELAY : pdMS_TO_TICKS(timeout_ms);
|
|
|
|
|
return xSemaphoreTakeRecursive(lvgl_mux, timeout_ticks) == pdTRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void st7789_lvgl_unlock(void)
|
|
|
|
|
{
|
|
|
|
|
xSemaphoreGiveRecursive(lvgl_mux);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void st7789_lvgl_port_task(void *arg)
|
|
|
|
|
{
|
|
|
|
|
ESP_LOGI(TAG, "Starting LVGL task");
|
|
|
|
|
uint32_t task_delay_ms = ST7789_LVGL_TASK_MAX_DELAY_MS;
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
// Lock the mutex due to the LVGL APIs are not thread-safe
|
|
|
|
|
if (st7789_lvgl_lock(-1))
|
|
|
|
|
{
|
|
|
|
|
task_delay_ms = lv_timer_handler();
|
|
|
|
|
// Release the mutex
|
|
|
|
|
st7789_lvgl_unlock();
|
|
|
|
|
}
|
|
|
|
|
if (task_delay_ms > ST7789_LVGL_TASK_MAX_DELAY_MS)
|
|
|
|
|
{
|
|
|
|
|
task_delay_ms = ST7789_LVGL_TASK_MAX_DELAY_MS;
|
|
|
|
|
}
|
|
|
|
|
else if (task_delay_ms < ST7789_LVGL_TASK_MIN_DELAY_MS)
|
|
|
|
|
{
|
|
|
|
|
task_delay_ms = ST7789_LVGL_TASK_MIN_DELAY_MS;
|
|
|
|
|
}
|
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(task_delay_ms));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
St7789Display::St7789Display(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_handle_t panel,
|
|
|
|
|
gpio_num_t backlight_pin, bool backlight_output_invert,
|
|
|
|
|
int width, int height, bool mirror_x, bool mirror_y, bool swap_xy)
|
|
|
|
|
int width, int height, int offset_x, int offset_y, bool mirror_x, bool mirror_y, bool swap_xy)
|
|
|
|
|
: panel_io_(panel_io), panel_(panel), backlight_pin_(backlight_pin), backlight_output_invert_(backlight_output_invert),
|
|
|
|
|
mirror_x_(mirror_x), mirror_y_(mirror_y), swap_xy_(swap_xy) {
|
|
|
|
|
width_ = width;
|
|
|
|
|
height_ = height;
|
|
|
|
|
offset_x_ = offset_x;
|
|
|
|
|
offset_y_ = offset_y;
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Initialize LVGL");
|
|
|
|
|
lvgl_port_cfg_t port_cfg = ESP_LVGL_PORT_INIT_CONFIG();
|
|
|
|
|
lvgl_port_init(&port_cfg);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
InitializeBacklight(backlight_pin);
|
|
|
|
|
|
|
|
|
|
// draw white
|
|
|
|
|
@@ -38,32 +150,44 @@ St7789Display::St7789Display(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_h
|
|
|
|
|
ESP_LOGI(TAG, "Turning display on");
|
|
|
|
|
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_, true));
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Adding LCD screen");
|
|
|
|
|
const lvgl_port_display_cfg_t display_cfg = {
|
|
|
|
|
.io_handle = panel_io_,
|
|
|
|
|
.panel_handle = panel_,
|
|
|
|
|
.control_handle = nullptr,
|
|
|
|
|
.buffer_size = static_cast<uint32_t>(width_ * 20),
|
|
|
|
|
.double_buffer = false,
|
|
|
|
|
.trans_size = 0,
|
|
|
|
|
.hres = static_cast<uint32_t>(width_),
|
|
|
|
|
.vres = static_cast<uint32_t>(height_),
|
|
|
|
|
.monochrome = false,
|
|
|
|
|
.rotation = {
|
|
|
|
|
.swap_xy = swap_xy_,
|
|
|
|
|
.mirror_x = mirror_x_,
|
|
|
|
|
.mirror_y = mirror_y_,
|
|
|
|
|
},
|
|
|
|
|
.flags = {
|
|
|
|
|
.buff_dma = 1,
|
|
|
|
|
.buff_spiram = 0,
|
|
|
|
|
.sw_rotate = 0,
|
|
|
|
|
.full_refresh = 0,
|
|
|
|
|
.direct_mode = 0,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
ESP_LOGI(TAG, "Initialize LVGL library");
|
|
|
|
|
lv_init();
|
|
|
|
|
// alloc draw buffers used by LVGL
|
|
|
|
|
static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
|
|
|
|
|
// it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
|
|
|
|
|
lv_color_t *buf1 = (lv_color_t *)heap_caps_malloc(width_ * 10 * sizeof(lv_color_t), MALLOC_CAP_DMA);
|
|
|
|
|
assert(buf1);
|
|
|
|
|
lv_color_t *buf2 = (lv_color_t *)heap_caps_malloc(width_ * 10 * sizeof(lv_color_t), MALLOC_CAP_DMA);
|
|
|
|
|
assert(buf2);
|
|
|
|
|
// initialize LVGL draw buffers
|
|
|
|
|
lv_disp_draw_buf_init(&disp_buf, buf1, buf2, width_ * 10);
|
|
|
|
|
|
|
|
|
|
disp_ = lvgl_port_add_disp(&display_cfg);
|
|
|
|
|
ESP_LOGI(TAG, "Register display driver to LVGL");
|
|
|
|
|
lv_disp_drv_init(&disp_drv);
|
|
|
|
|
disp_drv.hor_res = width_;
|
|
|
|
|
disp_drv.ver_res = height_;
|
|
|
|
|
disp_drv.offset_x = offset_x_;
|
|
|
|
|
disp_drv.offset_y = offset_y_;
|
|
|
|
|
disp_drv.flush_cb = st7789_lvgl_flush_cb;
|
|
|
|
|
disp_drv.drv_update_cb = st7789_lvgl_port_update_callback;
|
|
|
|
|
disp_drv.draw_buf = &disp_buf;
|
|
|
|
|
disp_drv.user_data = panel_;
|
|
|
|
|
|
|
|
|
|
lv_disp_t *disp = lv_disp_drv_register(&disp_drv);
|
|
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "Install LVGL tick timer");
|
|
|
|
|
// Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
|
|
|
|
|
const esp_timer_create_args_t lvgl_tick_timer_args = {
|
|
|
|
|
.callback = &st7789_increase_lvgl_tick,
|
|
|
|
|
.name = "lvgl_tick"};
|
|
|
|
|
esp_timer_handle_t lvgl_tick_timer = NULL;
|
|
|
|
|
ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
|
|
|
|
|
ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, ST7789_LVGL_TICK_PERIOD_MS * 1000));
|
|
|
|
|
|
|
|
|
|
lvgl_mux = xSemaphoreCreateRecursiveMutex();
|
|
|
|
|
assert(lvgl_mux);
|
|
|
|
|
ESP_LOGI(TAG, "Create LVGL task");
|
|
|
|
|
xTaskCreate(st7789_lvgl_port_task, "LVGL", ST7789_LVGL_TASK_STACK_SIZE, NULL, ST7789_LVGL_TASK_PRIORITY, NULL);
|
|
|
|
|
|
|
|
|
|
SetBacklight(100);
|
|
|
|
|
|
|
|
|
|
@@ -141,17 +265,20 @@ void St7789Display::SetBacklight(uint8_t brightness) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void St7789Display::Lock() {
|
|
|
|
|
lvgl_port_lock(0);
|
|
|
|
|
// lvgl_port_lock(0);
|
|
|
|
|
st7789_lvgl_lock(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void St7789Display::Unlock() {
|
|
|
|
|
lvgl_port_unlock();
|
|
|
|
|
// lvgl_port_unlock();
|
|
|
|
|
st7789_lvgl_unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void St7789Display::SetupUI() {
|
|
|
|
|
DisplayLockGuard lock(this);
|
|
|
|
|
|
|
|
|
|
auto screen = lv_disp_get_scr_act(disp_);
|
|
|
|
|
auto screen = lv_disp_get_scr_act(lv_disp_get_default());
|
|
|
|
|
// auto screen = lv_scr_act();
|
|
|
|
|
lv_obj_set_style_text_font(screen, &font_puhui_14_1, 0);
|
|
|
|
|
lv_obj_set_style_text_color(screen, lv_color_black(), 0);
|
|
|
|
|
|
|
|
|
|
|