Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LVGL v9.1 #175

Merged
merged 22 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
256 changes: 143 additions & 113 deletions README.md

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions include/esp32_smartdisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

#include <Arduino.h>
#include <lvgl.h>
#include <display/lv_display_private.h>
#include <misc/lv_timer_private.h>
#include <indev/lv_indev_private.h>

// Use last PWM_CHANNEL for backlight
#define PWM_CHANNEL_BCKL (SOC_LEDC_CHANNEL_NUM - 1)
Expand Down
6 changes: 3 additions & 3 deletions library.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json",
"name": "esp32_smartdisplay",
"version": "2.0.10",
"description": "LVGL driver for Sunton ESP32 Cheap Yellow Display display boards",
"version": "3.0.0",
"description": "LVGL v9.1 driver for Sunton ESP32 Cheap Yellow Display display boards",
"keywords": "LVGL Sunton CYD LCD TFT Touch",
"repository": {
"type": "git",
Expand Down Expand Up @@ -35,6 +35,6 @@
"frameworks": "arduino",
"platforms": "espressif32",
"dependencies": {
"lvgl/lvgl": "^8.4.0"
"lvgl/lvgl": "https://github.com/lvgl/lvgl.git#d39fc17"
}
}
4 changes: 3 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ build_flags =
'-D ESP_LCD_PANEL_IO_ADDITIONS_VER_PATCH=1'

lib_deps =
lvgl/lvgl@^8.4.0
# Use direct git because rotation commit is after 9.1. So pin the build to v9.2
https://github.com/lvgl/lvgl.git#d39fc17
#lvgl/lvgl@^9.1.0
# The platformio.test_dir contains the test_main.cpp just to have an setup() and loop() function
# so it will compile
${platformio.test_dir}
Expand Down
135 changes: 71 additions & 64 deletions src/esp32_smartdisplay.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,39 @@
#define BRIGHTNESS_DARK_ZONE 250

// Functions to be defined in the tft/touch driver
extern void lvgl_lcd_init(lv_disp_drv_t *disp_drv);
extern void lvgl_touch_init(lv_indev_drv_t *disp_drv);
extern lv_display_t *lvgl_lcd_init();
extern lv_indev_t *lvgl_touch_init();

lv_disp_drv_t disp_drv;
lv_timer_t *update_brightness_timer;
lv_display_t *display;

#ifdef BOARD_HAS_TOUCH
lv_indev_drv_t indev_drv;
lv_indev_t *indev;
touch_calibration_data_t touch_calibration_data;
void (*driver_touch_read_cb)(struct _lv_indev_drv_t *indev_drv, lv_indev_data_t *data);
void (*driver_touch_read_cb)(lv_indev_t *indev, lv_indev_data_t *data);
#endif

void lvgl_display_resolution_changed_callback(lv_event_t *drv);

lv_timer_t *update_brightness_timer;

#ifdef LV_USE_LOG
void lvgl_log(const char *buf)
void lvgl_log(lv_log_level_t level, const char *buf)
{
log_printf("%s", buf);
switch (level)
{
case LV_LOG_LEVEL_TRACE:
log_printf("%s", buf);
break;
case LV_LOG_LEVEL_INFO:
log_i("%s", buf);
break;
case LV_LOG_LEVEL_WARN:
log_w("%s", buf);
break;
case LV_LOG_LEVEL_ERROR:
log_e("%s", buf);
break;
}
}
#endif

Expand All @@ -34,9 +51,9 @@ void smartdisplay_lcd_set_backlight(float duty)
{
log_v("duty:%2f", duty);

if (duty > 1.0)
if (duty > 1.0f)
duty = 1.0f;
if (duty < 0.0)
if (duty < 0.0f)
duty = 0.0f;
#if ESP_ARDUINO_VERSION_MAJOR >= 3
ledcWrite(GPIO_BCKL, duty * PWM_MAX_BCKL);
Expand Down Expand Up @@ -74,7 +91,7 @@ void adaptive_brightness(lv_timer_t *timer)

void smartdisplay_lcd_set_brightness_cb(smartdisplay_lcd_adaptive_brightness_cb_t cb, uint interval)
{
log_v("adaptive_brightness_cb:0x%08x, interval:%d", cb, interval);
log_v("adaptive_brightness_cb:0x%08x, interval:%u", cb, interval);

// Delete current timer if any
if (update_brightness_timer)
Expand All @@ -100,12 +117,12 @@ void smartdisplay_led_set_rgb(bool r, bool g, bool b)

#ifdef BOARD_HAS_TOUCH
// See: https://www.maximintegrated.com/en/design/technical-documents/app-notes/5/5296.html
void lvgl_touch_calibration_transform(lv_indev_drv_t *disp_drv, lv_indev_data_t *data)
void lvgl_touch_calibration_transform(lv_indev_t *indev, lv_indev_data_t *data)
{
log_v("disp_drv:0x%08x, data:0x%08x", disp_drv, data);
log_v("indev:0x%08x, data:0x%08x", indev, data);

// Call low level read from the driver
driver_touch_read_cb(disp_drv, data);
driver_touch_read_cb(indev, data);
// Check if transformation is required
if (touch_calibration_data.valid && data->state == LV_INDEV_STATE_PRESSED)
{
Expand Down Expand Up @@ -137,37 +154,6 @@ touch_calibration_data_t smartdisplay_compute_touch_calibration(const lv_point_t
};
#endif

// Called when driver parameters are updated (rotation)
// Top of the display is top left when connector is at the bottom
// The rotation values are relative to how you would rotate the physical display in the clockwise direction.
// Thus, LV_DISP_ROT_90 means you rotate the hardware 90 degrees clockwise, and the display rotates 90 degrees counterclockwise to compensate.
void lvgl_update_callback(lv_disp_drv_t *drv)
{
if (drv->sw_rotate == false)
{
const esp_lcd_panel_handle_t panel_handle = disp_drv.user_data;
switch (drv->rotated)
{
case LV_DISP_ROT_NONE:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
break;
case LV_DISP_ROT_90:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, !DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, DISPLAY_MIRROR_X, !DISPLAY_MIRROR_Y));
break;
case LV_DISP_ROT_180:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, !DISPLAY_MIRROR_X, !DISPLAY_MIRROR_Y));
break;
case LV_DISP_ROT_270:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, !DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, !DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
break;
}
}
}

void smartdisplay_init()
{
log_d("smartdisplay_init");
Expand Down Expand Up @@ -206,31 +192,52 @@ void smartdisplay_init()
ledcAttachPin(GPIO_BCKL, PWM_CHANNEL_BCKL);
#endif
// Setup TFT display
lv_disp_drv_init(&disp_drv);
disp_drv.hor_res = DISPLAY_WIDTH;
disp_drv.ver_res = DISPLAY_HEIGHT;
// Create drawBuffer
disp_drv.draw_buf = (lv_disp_draw_buf_t *)malloc(sizeof(lv_disp_draw_buf_t));
void *drawBuffer = heap_caps_malloc(sizeof(lv_color_t) * LVGL_BUFFER_PIXELS, LVGL_BUFFER_MALLOC_FLAGS);
lv_disp_draw_buf_init(disp_drv.draw_buf, drawBuffer, NULL, LVGL_BUFFER_PIXELS);
// Register callback for changes to the driver parameters (rotation!)
disp_drv.drv_update_cb = lvgl_update_callback;
// Initialize specific driver
lvgl_lcd_init(&disp_drv);
__attribute__((unused)) lv_disp_t *display = lv_disp_drv_register(&disp_drv);
// Clear screen
display = lvgl_lcd_init();
// Register callback for hardware rotation
if (!display->sw_rotate)
lv_display_add_event_cb(display, lvgl_display_resolution_changed_callback, LV_EVENT_RESOLUTION_CHANGED, NULL);

// Clear screen
lv_obj_clean(lv_scr_act());
// Turn backlight on (50%)
smartdisplay_lcd_set_backlight(0.5f);

// If there is a touch controller defined
#ifdef BOARD_HAS_TOUCH
// Setup touch
lv_indev_drv_init(&indev_drv);
indev_drv.disp = display;
lvgl_touch_init(&indev_drv);
driver_touch_read_cb = indev_drv.read_cb;
indev_drv.read_cb = lvgl_touch_calibration_transform;
lv_indev_drv_register(&indev_drv);
indev = lvgl_touch_init();
indev->disp = display;
// Intercept callback
driver_touch_read_cb = indev->read_cb;
indev->read_cb = lvgl_touch_calibration_transform;
lv_indev_enable(indev, true);
#endif
}

// Called when driver resolution is updated (including rotation)
// Top of the display is top left when connector is at the bottom
// The rotation values are relative to how you would rotate the physical display in the clockwise direction.
// Thus, LV_DISPLAY_ROTATION_90 means you rotate the hardware 90 degrees clockwise, and the display rotates 90 degrees counterclockwise to compensate.
void lvgl_display_resolution_changed_callback(lv_event_t *event)
{
const esp_lcd_panel_handle_t panel_handle = display->user_data;
switch (display->rotation)
{
case LV_DISPLAY_ROTATION_0:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
break;
case LV_DISPLAY_ROTATION_90:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, !DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, DISPLAY_MIRROR_X, !DISPLAY_MIRROR_Y));
break;
case LV_DISPLAY_ROTATION_180:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, !DISPLAY_MIRROR_X, !DISPLAY_MIRROR_Y));
break;
case LV_DISPLAY_ROTATION_270:
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, !DISPLAY_SWAP_XY));
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, !DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
break;
}
}
50 changes: 29 additions & 21 deletions src/lvgl_panel_gc9a01_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,39 @@ bool gc9a01_color_trans_done(esp_lcd_panel_io_handle_t panel_io_handle, esp_lcd_
{
log_v("panel_io_handle:0x%08x, panel_io_event_data:%0x%08x, user_ctx:0x%08x", panel_io_handle, panel_io_event_data, user_ctx);

lv_disp_drv_t *disp_driver = user_ctx;
lv_disp_flush_ready(disp_driver);
lv_display_t *display = user_ctx;
lv_display_flush_ready(display);
return false;
}

void gc9a01_lv_flush(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
void gc9a01_lv_flush(lv_display_t *display, const lv_area_t *area, uint8_t *px_map)
{
log_v("drv:0x%08x, area:%0x%08x, color_map:0x%08x", drv, area, color_map);
log_v("display:0x%08x, area:%0x%08x, color_map:0x%08x", display, area, px_map);

esp_lcd_panel_handle_t panel_handle = drv->user_data;
#if LV_COLOR_16_SWAP != 1
#warning "LV_COLOR_16_SWAP should be 1 for max performance"
ushort pixels = lv_area_get_size(area);
lv_color16_t *p = color_map;
esp_lcd_panel_handle_t panel_handle = display->user_data;
uint32_t pixels = lv_area_get_size(area);
uint16_t *p = (uint16_t *)px_map;
while (pixels--)
p++->full = (uint16_t)((p->full >> 8) | (p->full << 8));
#endif
ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel_handle, area->x1, area->y1, area->x2 + 1, area->y2 + 1, color_map));
{
*p = (uint16_t)((*p >> 8) | (*p << 8));
p++;
}

ESP_ERROR_CHECK(esp_lcd_panel_draw_bitmap(panel_handle, area->x1, area->y1, area->x2 + 1, area->y2 + 1, px_map));
};

void lvgl_lcd_init(lv_disp_drv_t *drv)
lv_display_t *lvgl_lcd_init()
{
log_v("disp_drv:0x%08x", drv);
lv_display_t *display = lv_display_create(DISPLAY_WIDTH, DISPLAY_HEIGHT);
log_v("display:0x%08x", display);
// Create drawBuffer
uint32_t drawBufferSize = sizeof(lv_color_t) * LVGL_BUFFER_PIXELS;
void *drawBuffer = heap_caps_malloc(drawBufferSize, LVGL_BUFFER_MALLOC_FLAGS);
lv_display_set_buffers(display, drawBuffer, NULL, drawBufferSize, LV_DISPLAY_RENDER_MODE_PARTIAL);

// Hardware rotation is supported
drv->sw_rotate = 0;
drv->rotated = LV_DISP_ROT_NONE;
display->sw_rotate = 0;
display->rotation = LV_DISPLAY_ROTATION_0;

// Create SPI bus
const spi_bus_config_t spi_bus_config = {
Expand All @@ -58,7 +64,7 @@ void lvgl_lcd_init(lv_disp_drv_t *drv)
.spi_mode = GC9A01_SPI_CONFIG_SPI_MODE,
.pclk_hz = GC9A01_SPI_CONFIG_PCLK_HZ,
.trans_queue_depth = GC9A01_SPI_CONFIG_TRANS_QUEUE_DEPTH,
.user_ctx = drv,
.user_ctx = display,
.on_color_trans_done = gc9a01_color_trans_done,
.lcd_cmd_bits = GC9A01_SPI_CONFIG_LCD_CMD_BITS,
.lcd_param_bits = GC9A01_SPI_CONFIG_LCD_PARAM_BITS,
Expand Down Expand Up @@ -91,17 +97,19 @@ void lvgl_lcd_init(lv_disp_drv_t *drv)
#if (DISPLAY_SWAP_XY)
ESP_ERROR_CHECK(esp_lcd_panel_swap_xy(panel_handle, DISPLAY_SWAP_XY));
#endif
#if (DISPLAY_MIRROR_X || DISPLAY_MIRROR_Y)
#if (DISPLAY_MIRROR_X || DISPLAY_MIRROR_Y)
ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, DISPLAY_MIRROR_X, DISPLAY_MIRROR_Y));
#endif
#endif
#if (DISPLAY_GAP_X || DISPLAY_GAP_Y)
ESP_ERROR_CHECK(esp_lcd_panel_set_gap(panel_handle, DISPLAY_GAP_X, DISPLAY_GAP_Y));
#endif
// Turn display on
ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));

drv->user_data = panel_handle;
drv->flush_cb = gc9a01_lv_flush;
display->user_data = panel_handle;
display->flush_cb = gc9a01_lv_flush;

return display;
}

#endif
Loading