diff --git a/docs/en/api-reference/peripherals/lcd.rst b/docs/en/api-reference/peripherals/lcd.rst index 09d5160b68..7fdf780759 100644 --- a/docs/en/api-reference/peripherals/lcd.rst +++ b/docs/en/api-reference/peripherals/lcd.rst @@ -24,6 +24,7 @@ LCD examples are located under: :example:`peripherals/lcd`: * Jpeg decoding and LCD display - :example:`peripherals/lcd/tjpgd` * LVGL porting and animation UI - :example:`peripherals/lcd/lvgl` +* GC9A01 user customized driver and dash board UI - :example:`peripherals/lcd/gc9a01` API Reference ------------- diff --git a/examples/peripherals/lcd/gc9a01/CMakeLists.txt b/examples/peripherals/lcd/gc9a01/CMakeLists.txt new file mode 100644 index 0000000000..50065c7404 --- /dev/null +++ b/examples/peripherals/lcd/gc9a01/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(lcd_gc9a01) + +# As the upstream LVGL library has build warnings in esp-idf build system, this is only for temporarily workaround +# Will remove this workaround when upstream LVGL fixes the warnings in the next release +idf_component_get_property(lvgl_lib lvgl__lvgl COMPONENT_LIB) +target_compile_options(${lvgl_lib} PRIVATE "-Wno-empty-body" "-Wno-strict-prototypes") diff --git a/examples/peripherals/lcd/gc9a01/README.md b/examples/peripherals/lcd/gc9a01/README.md new file mode 100644 index 0000000000..79f7bc787a --- /dev/null +++ b/examples/peripherals/lcd/gc9a01/README.md @@ -0,0 +1,83 @@ +# GC9A01 porting example + +[esp_lcd](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/lcd.html) provides several panel drivers out-of box, e.g. ST7789, SSD1306, NT35510. However, there're a lot of other panels on the market, it's beyond `esp_lcd` component's responsibility to include them all. + +`esp_lcd` allows user to add their own panel drivers in the project scope (i.e. panel driver can live outside of esp-idf), so that the upper layer code like LVGL porting code can be reused without any modifications, as long as user-implemented panel driver follows the interface defined in the `esp_lcd` component. + +This example shows how to add the GC9A01 driver in the project folder but still use the API provided by `esp_lcd` component. As GC9A01 is famous in the form of a circular screen, this example will draw a fancy dash board with the LVGL library. For more information about porting the LVGL library, you can also refer to another [lvgl porting example](../lvgl/README.md). + +## How to use the example + +### Hardware Required + +* An ESP development board +* An GC9A01 LCD panel, with SPI interface +* An USB cable for power supply and programming + +### Hardware Connection + +The connection between ESP Board and the LCD is as follows: + +``` + ESP Board GC9A01 Panel +┌──────────────────────┐ ┌────────────────────┐ +│ GND ├─────────────►│ GND │ +│ │ │ │ +│ 3V3 ├─────────────►│ VCC │ +│ │ │ │ +│ PCLK ├─────────────►│ SCL │ +│ │ │ │ +│ DATA0 ├─────────────►│ SDA │ +│ │ │ │ +│ RST ├─────────────►│ RES │ +│ │ │ │ +│ DC ├─────────────►│ DC │ +│ │ │ │ +│ CS ├─────────────►│ CS │ +│ │ │ │ +│ BK_LIGHT ├─────────────►│ BLK │ +└──────────────────────┘ └────────────────────┘ +``` + +The GPIO number used by this example can be changed in [lvgl_example_main.c](main/lvgl_example_main.c). +Especially, please pay attention to the level used to turn on the LCD backlight, some LCD module needs a low level to turn it on, while others take a high level. You can change the backlight level macro `EXAMPLE_LCD_BK_LIGHT_ON_LEVEL` in [lvgl_example_main.c](main/lvgl_example_main.c). + +### Build and Flash + +Run `idf.py -p PORT build flash monitor` to build, flash and monitor the project. A fancy animation will show up on the LCD as expected. + +The first time you run `idf.py` for the example will cost extra time as the build system needs to address the component dependencies and downloads the missing components from registry into `managed_components` folder. + +(To exit the serial monitor, type ``Ctrl-]``.) + +See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects. + +### Example Output + +```bash +... +I (304) cpu_start: Starting scheduler. +I (308) example: Turn off LCD backlight +I (308) gpio: GPIO[2]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 +I (318) example: Initialize SPI bus +I (318) example: Install panel IO +I (328) gpio: GPIO[5]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 +I (338) example: Install GC9A01 panel driver +I (338) gpio: GPIO[3]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 +I (468) example: Turn on LCD backlight +I (468) example: Initialize LVGL library +I (468) example: Register display driver to LVGL +I (468) example: Install LVGL tick timer +I (468) example: Display LVGL Meter Widget +... +``` + + +## Troubleshooting + +* Why the LCD doesn't light up? + * Check the backlight's turn-on level, and update it in `EXAMPLE_LCD_BK_LIGHT_ON_LEVEL` +* Weird color display? + * Each LCD panel has it's own initialize code, see the `vendor_specific_init` array defined in [GC9A01 driver](main/esp_lcd_panel_gc9a01.c), where contains the Gama setting. You should consult your the LCD supplier. + +For any technical queries, please open an [issue] (https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon. \ No newline at end of file diff --git a/examples/peripherals/lcd/gc9a01/main/CMakeLists.txt b/examples/peripherals/lcd/gc9a01/main/CMakeLists.txt new file mode 100644 index 0000000000..2bdc22d71e --- /dev/null +++ b/examples/peripherals/lcd/gc9a01/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "esp_lcd_panel_gc9a01.c" "lvgl_example_main.c" "lvgl_demo_ui.c" + INCLUDE_DIRS ".") diff --git a/examples/peripherals/lcd/gc9a01/main/esp_lcd_panel_gc9a01.c b/examples/peripherals/lcd/gc9a01/main/esp_lcd_panel_gc9a01.c new file mode 100644 index 0000000000..11b67cf659 --- /dev/null +++ b/examples/peripherals/lcd/gc9a01/main/esp_lcd_panel_gc9a01.c @@ -0,0 +1,325 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_lcd_panel_interface.h" +#include "esp_lcd_panel_io.h" +#include "esp_lcd_panel_vendor.h" +#include "esp_lcd_panel_ops.h" +#include "esp_lcd_panel_commands.h" +#include "driver/gpio.h" +#include "esp_log.h" +#include "esp_check.h" + +static const char *TAG = "gc9a01"; + +static esp_err_t panel_gc9a01_del(esp_lcd_panel_t *panel); +static esp_err_t panel_gc9a01_reset(esp_lcd_panel_t *panel); +static esp_err_t panel_gc9a01_init(esp_lcd_panel_t *panel); +static esp_err_t panel_gc9a01_draw_bitmap(esp_lcd_panel_t *panel, int x_start, int y_start, int x_end, int y_end, const void *color_data); +static esp_err_t panel_gc9a01_invert_color(esp_lcd_panel_t *panel, bool invert_color_data); +static esp_err_t panel_gc9a01_mirror(esp_lcd_panel_t *panel, bool mirror_x, bool mirror_y); +static esp_err_t panel_gc9a01_swap_xy(esp_lcd_panel_t *panel, bool swap_axes); +static esp_err_t panel_gc9a01_set_gap(esp_lcd_panel_t *panel, int x_gap, int y_gap); +static esp_err_t panel_gc9a01_disp_off(esp_lcd_panel_t *panel, bool off); + +typedef struct { + esp_lcd_panel_t base; + esp_lcd_panel_io_handle_t io; + int reset_gpio_num; + bool reset_level; + int x_gap; + int y_gap; + unsigned int bits_per_pixel; + uint8_t madctl_val; // save current value of LCD_CMD_MADCTL register + uint8_t colmod_cal; // save surrent value of LCD_CMD_COLMOD register +} gc9a01_panel_t; + +esp_err_t esp_lcd_new_panel_gc9a01(const esp_lcd_panel_io_handle_t io, const esp_lcd_panel_dev_config_t *panel_dev_config, esp_lcd_panel_handle_t *ret_panel) +{ + esp_err_t ret = ESP_OK; + gc9a01_panel_t *gc9a01 = NULL; + ESP_GOTO_ON_FALSE(io && panel_dev_config && ret_panel, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument"); + gc9a01 = calloc(1, sizeof(gc9a01_panel_t)); + ESP_GOTO_ON_FALSE(gc9a01, ESP_ERR_NO_MEM, err, TAG, "no mem for gc9a01 panel"); + + if (panel_dev_config->reset_gpio_num >= 0) { + gpio_config_t io_conf = { + .mode = GPIO_MODE_OUTPUT, + .pin_bit_mask = 1ULL << panel_dev_config->reset_gpio_num, + }; + ESP_GOTO_ON_ERROR(gpio_config(&io_conf), err, TAG, "configure GPIO for RST line failed"); + } + + switch (panel_dev_config->color_space) { + case ESP_LCD_COLOR_SPACE_RGB: + gc9a01->madctl_val = 0; + break; + case ESP_LCD_COLOR_SPACE_BGR: + gc9a01->madctl_val |= LCD_CMD_BGR_BIT; + break; + default: + ESP_GOTO_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, err, TAG, "unsupported color space"); + break; + } + + switch (panel_dev_config->bits_per_pixel) { + case 16: + gc9a01->colmod_cal = 0x55; + break; + case 18: + gc9a01->colmod_cal = 0x66; + break; + default: + ESP_GOTO_ON_FALSE(false, ESP_ERR_NOT_SUPPORTED, err, TAG, "unsupported pixel width"); + break; + } + + gc9a01->io = io; + gc9a01->bits_per_pixel = panel_dev_config->bits_per_pixel; + gc9a01->reset_gpio_num = panel_dev_config->reset_gpio_num; + gc9a01->reset_level = panel_dev_config->flags.reset_active_high; + gc9a01->base.del = panel_gc9a01_del; + gc9a01->base.reset = panel_gc9a01_reset; + gc9a01->base.init = panel_gc9a01_init; + gc9a01->base.draw_bitmap = panel_gc9a01_draw_bitmap; + gc9a01->base.invert_color = panel_gc9a01_invert_color; + gc9a01->base.set_gap = panel_gc9a01_set_gap; + gc9a01->base.mirror = panel_gc9a01_mirror; + gc9a01->base.swap_xy = panel_gc9a01_swap_xy; + gc9a01->base.disp_off = panel_gc9a01_disp_off; + *ret_panel = &(gc9a01->base); + ESP_LOGD(TAG, "new gc9a01 panel @%p", gc9a01); + + return ESP_OK; + +err: + if (gc9a01) { + if (panel_dev_config->reset_gpio_num >= 0) { + gpio_reset_pin(panel_dev_config->reset_gpio_num); + } + free(gc9a01); + } + return ret; +} + +static esp_err_t panel_gc9a01_del(esp_lcd_panel_t *panel) +{ + gc9a01_panel_t *gc9a01 = __containerof(panel, gc9a01_panel_t, base); + + if (gc9a01->reset_gpio_num >= 0) { + gpio_reset_pin(gc9a01->reset_gpio_num); + } + ESP_LOGD(TAG, "del gc9a01 panel @%p", gc9a01); + free(gc9a01); + return ESP_OK; +} + +static esp_err_t panel_gc9a01_reset(esp_lcd_panel_t *panel) +{ + gc9a01_panel_t *gc9a01 = __containerof(panel, gc9a01_panel_t, base); + esp_lcd_panel_io_handle_t io = gc9a01->io; + + // perform hardware reset + if (gc9a01->reset_gpio_num >= 0) { + gpio_set_level(gc9a01->reset_gpio_num, gc9a01->reset_level); + vTaskDelay(pdMS_TO_TICKS(10)); + gpio_set_level(gc9a01->reset_gpio_num, !gc9a01->reset_level); + vTaskDelay(pdMS_TO_TICKS(10)); + } else { // perform software reset + esp_lcd_panel_io_tx_param(io, LCD_CMD_SWRESET, NULL, 0); + vTaskDelay(pdMS_TO_TICKS(20)); // spec, wait at least 5ms before sending new command + } + + return ESP_OK; +} + +typedef struct { + uint8_t cmd; + uint8_t data[16]; + uint8_t data_bytes; // Length of data in above data array; 0xFF = end of cmds. +} lcd_init_cmd_t; + +static const lcd_init_cmd_t vendor_specific_init[] = { + // Enable Inter Register + {0xfe, {0}, 0}, + {0xef, {0}, 0}, + {0xeb, {0x14}, 1}, + {0x84, {0x60}, 1}, + {0x85, {0xff}, 1}, + {0x86, {0xff}, 1}, + {0x87, {0xff}, 1}, + {0x8e, {0xff}, 1}, + {0x8f, {0xff}, 1}, + {0x88, {0x0a}, 1}, + {0x89, {0x23}, 1}, + {0x8a, {0x00}, 1}, + {0x8b, {0x80}, 1}, + {0x8c, {0x01}, 1}, + {0x8d, {0x03}, 1}, + {0x90, {0x08, 0x08, 0x08, 0x08}, 4}, + {0xff, {0x60, 0x01, 0x04}, 3}, + {0xC3, {0x13}, 1}, + {0xC4, {0x13}, 1}, + {0xC9, {0x30}, 1}, + {0xbe, {0x11}, 1}, + {0xe1, {0x10, 0x0e}, 2}, + {0xdf, {0x21, 0x0c, 0x02}, 3}, + // Set gamma + {0xF0, {0x45, 0x09, 0x08, 0x08, 0x26, 0x2a}, 6}, + {0xF1, {0x43, 0x70, 0x72, 0x36, 0x37, 0x6f}, 6}, + {0xF2, {0x45, 0x09, 0x08, 0x08, 0x26, 0x2a}, 6}, + {0xF3, {0x43, 0x70, 0x72, 0x36, 0x37, 0x6f}, 6}, + {0xed, {0x1b, 0x0b}, 2}, + {0xae, {0x77}, 1}, + {0xcd, {0x63}, 1}, + {0x70, {0x07, 0x07, 0x04, 0x0e, 0x0f, 0x09, 0x07, 0x08, 0x03}, 9}, + {0xE8, {0x34}, 1}, // 4 dot inversion + {0x60, {0x38, 0x0b, 0x6D, 0x6D, 0x39, 0xf0, 0x6D, 0x6D}, 8}, + {0x61, {0x38, 0xf4, 0x6D, 0x6D, 0x38, 0xf7, 0x6D, 0x6D}, 8}, + {0x62, {0x38, 0x0D, 0x71, 0xED, 0x70, 0x70, 0x38, 0x0F, 0x71, 0xEF, 0x70, 0x70}, 12}, + {0x63, {0x38, 0x11, 0x71, 0xF1, 0x70, 0x70, 0x38, 0x13, 0x71, 0xF3, 0x70, 0x70}, 12}, + {0x64, {0x28, 0x29, 0xF1, 0x01, 0xF1, 0x00, 0x07}, 7}, + {0x66, {0x3C, 0x00, 0xCD, 0x67, 0x45, 0x45, 0x10, 0x00, 0x00, 0x00}, 10}, + {0x67, {0x00, 0x3C, 0x00, 0x00, 0x00, 0x01, 0x54, 0x10, 0x32, 0x98}, 10}, + {0x74, {0x10, 0x45, 0x80, 0x00, 0x00, 0x4E, 0x00}, 7}, + {0x98, {0x3e, 0x07}, 2}, + {0x99, {0x3e, 0x07}, 2}, + {0, {0}, 0xff}, +}; + +static esp_err_t panel_gc9a01_init(esp_lcd_panel_t *panel) +{ + gc9a01_panel_t *gc9a01 = __containerof(panel, gc9a01_panel_t, base); + esp_lcd_panel_io_handle_t io = gc9a01->io; + + // LCD goes into sleep mode and display will be turned off after power on reset, exit sleep mode first + esp_lcd_panel_io_tx_param(io, LCD_CMD_SLPOUT, NULL, 0); + vTaskDelay(pdMS_TO_TICKS(100)); + esp_lcd_panel_io_tx_param(io, LCD_CMD_MADCTL, (uint8_t[]) { + gc9a01->madctl_val, + }, 1); + esp_lcd_panel_io_tx_param(io, LCD_CMD_COLMOD, (uint8_t[]) { + gc9a01->colmod_cal, + }, 1); + + // vendor specific initialization, it can be different between manufacturers + // should consult the LCD supplier for initialization sequence code + int cmd = 0; + while (vendor_specific_init[cmd].data_bytes != 0xff) { + esp_lcd_panel_io_tx_param(io, vendor_specific_init[cmd].cmd, vendor_specific_init[cmd].data, vendor_specific_init[cmd].data_bytes & 0x1F); + cmd++; + } + + // turn on display + esp_lcd_panel_io_tx_param(io, LCD_CMD_DISPON, NULL, 0); + return ESP_OK; +} + +static esp_err_t panel_gc9a01_draw_bitmap(esp_lcd_panel_t *panel, int x_start, int y_start, int x_end, int y_end, const void *color_data) +{ + gc9a01_panel_t *gc9a01 = __containerof(panel, gc9a01_panel_t, base); + assert((x_start < x_end) && (y_start < y_end) && "start position must be smaller than end position"); + esp_lcd_panel_io_handle_t io = gc9a01->io; + + x_start += gc9a01->x_gap; + x_end += gc9a01->x_gap; + y_start += gc9a01->y_gap; + y_end += gc9a01->y_gap; + + // define an area of frame memory where MCU can access + esp_lcd_panel_io_tx_param(io, LCD_CMD_CASET, (uint8_t[]) { + (x_start >> 8) & 0xFF, + x_start & 0xFF, + ((x_end - 1) >> 8) & 0xFF, + (x_end - 1) & 0xFF, + }, 4); + esp_lcd_panel_io_tx_param(io, LCD_CMD_RASET, (uint8_t[]) { + (y_start >> 8) & 0xFF, + y_start & 0xFF, + ((y_end - 1) >> 8) & 0xFF, + (y_end - 1) & 0xFF, + }, 4); + // transfer frame buffer + size_t len = (x_end - x_start) * (y_end - y_start) * gc9a01->bits_per_pixel / 8; + esp_lcd_panel_io_tx_color(io, LCD_CMD_RAMWR, color_data, len); + + return ESP_OK; +} + +static esp_err_t panel_gc9a01_invert_color(esp_lcd_panel_t *panel, bool invert_color_data) +{ + gc9a01_panel_t *gc9a01 = __containerof(panel, gc9a01_panel_t, base); + esp_lcd_panel_io_handle_t io = gc9a01->io; + int command = 0; + if (invert_color_data) { + command = LCD_CMD_INVON; + } else { + command = LCD_CMD_INVOFF; + } + esp_lcd_panel_io_tx_param(io, command, NULL, 0); + return ESP_OK; +} + +static esp_err_t panel_gc9a01_mirror(esp_lcd_panel_t *panel, bool mirror_x, bool mirror_y) +{ + gc9a01_panel_t *gc9a01 = __containerof(panel, gc9a01_panel_t, base); + esp_lcd_panel_io_handle_t io = gc9a01->io; + if (mirror_x) { + gc9a01->madctl_val |= LCD_CMD_MX_BIT; + } else { + gc9a01->madctl_val &= ~LCD_CMD_MX_BIT; + } + if (mirror_y) { + gc9a01->madctl_val |= LCD_CMD_MY_BIT; + } else { + gc9a01->madctl_val &= ~LCD_CMD_MY_BIT; + } + esp_lcd_panel_io_tx_param(io, LCD_CMD_MADCTL, (uint8_t[]) { + gc9a01->madctl_val + }, 1); + return ESP_OK; +} + +static esp_err_t panel_gc9a01_swap_xy(esp_lcd_panel_t *panel, bool swap_axes) +{ + gc9a01_panel_t *gc9a01 = __containerof(panel, gc9a01_panel_t, base); + esp_lcd_panel_io_handle_t io = gc9a01->io; + if (swap_axes) { + gc9a01->madctl_val |= LCD_CMD_MV_BIT; + } else { + gc9a01->madctl_val &= ~LCD_CMD_MV_BIT; + } + esp_lcd_panel_io_tx_param(io, LCD_CMD_MADCTL, (uint8_t[]) { + gc9a01->madctl_val + }, 1); + return ESP_OK; +} + +static esp_err_t panel_gc9a01_set_gap(esp_lcd_panel_t *panel, int x_gap, int y_gap) +{ + gc9a01_panel_t *gc9a01 = __containerof(panel, gc9a01_panel_t, base); + gc9a01->x_gap = x_gap; + gc9a01->y_gap = y_gap; + return ESP_OK; +} + +static esp_err_t panel_gc9a01_disp_off(esp_lcd_panel_t *panel, bool off) +{ + gc9a01_panel_t *gc9a01 = __containerof(panel, gc9a01_panel_t, base); + esp_lcd_panel_io_handle_t io = gc9a01->io; + int command = 0; + if (off) { + command = LCD_CMD_DISPOFF; + } else { + command = LCD_CMD_DISPON; + } + esp_lcd_panel_io_tx_param(io, command, NULL, 0); + return ESP_OK; +} diff --git a/examples/peripherals/lcd/gc9a01/main/esp_lcd_panel_gc9a01.h b/examples/peripherals/lcd/gc9a01/main/esp_lcd_panel_gc9a01.h new file mode 100644 index 0000000000..3d0c5a0e7d --- /dev/null +++ b/examples/peripherals/lcd/gc9a01/main/esp_lcd_panel_gc9a01.h @@ -0,0 +1,29 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once + +#include "esp_lcd_panel_vendor.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Create LCD panel for model GC9A01 + * + * @param[in] io LCD panel IO handle + * @param[in] panel_dev_config general panel device configuration + * @param[out] ret_panel Returned LCD panel handle + * @return + * - ESP_ERR_INVALID_ARG if parameter is invalid + * - ESP_ERR_NO_MEM if out of memory + * - ESP_OK on success + */ +esp_err_t esp_lcd_new_panel_gc9a01(const esp_lcd_panel_io_handle_t io, const esp_lcd_panel_dev_config_t *panel_dev_config, esp_lcd_panel_handle_t *ret_panel); + +#ifdef __cplusplus +} +#endif diff --git a/examples/peripherals/lcd/gc9a01/main/idf_component.yml b/examples/peripherals/lcd/gc9a01/main/idf_component.yml new file mode 100644 index 0000000000..c5592991c9 --- /dev/null +++ b/examples/peripherals/lcd/gc9a01/main/idf_component.yml @@ -0,0 +1,3 @@ +dependencies: + idf: ">=4.4" + lvgl/lvgl: "==8.0.2" diff --git a/examples/peripherals/lcd/gc9a01/main/lvgl_demo_ui.c b/examples/peripherals/lcd/gc9a01/main/lvgl_demo_ui.c new file mode 100644 index 0000000000..17c5400ab9 --- /dev/null +++ b/examples/peripherals/lcd/gc9a01/main/lvgl_demo_ui.c @@ -0,0 +1,66 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: CC0-1.0 + */ + +// This demo UI is adapted from LVGL official example: https://docs.lvgl.io/master/widgets/extra/meter.html#simple-meter + +#include "lvgl.h" + +static lv_obj_t *meter; + +static void set_value(void *indic, int32_t v) +{ + lv_meter_set_indicator_end_value(meter, indic, v); +} + +void example_lvgl_demo_ui(lv_obj_t *scr) +{ + meter = lv_meter_create(lv_scr_act()); + lv_obj_center(meter); + lv_obj_set_size(meter, 200, 200); + + /*Add a scale first*/ + lv_meter_scale_t *scale = lv_meter_add_scale(meter); + lv_meter_set_scale_ticks(meter, scale, 41, 2, 10, lv_palette_main(LV_PALETTE_GREY)); + lv_meter_set_scale_major_ticks(meter, scale, 8, 4, 15, lv_color_black(), 10); + + lv_meter_indicator_t *indic; + + /*Add a blue arc to the start*/ + indic = lv_meter_add_arc(meter, scale, 3, lv_palette_main(LV_PALETTE_BLUE), 0); + lv_meter_set_indicator_start_value(meter, indic, 0); + lv_meter_set_indicator_end_value(meter, indic, 20); + + /*Make the tick lines blue at the start of the scale*/ + indic = lv_meter_add_scale_lines(meter, scale, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_BLUE), false, 0); + lv_meter_set_indicator_start_value(meter, indic, 0); + lv_meter_set_indicator_end_value(meter, indic, 20); + + /*Add a red arc to the end*/ + indic = lv_meter_add_arc(meter, scale, 3, lv_palette_main(LV_PALETTE_RED), 0); + lv_meter_set_indicator_start_value(meter, indic, 80); + lv_meter_set_indicator_end_value(meter, indic, 100); + + /*Make the tick lines red at the end of the scale*/ + indic = lv_meter_add_scale_lines(meter, scale, lv_palette_main(LV_PALETTE_RED), lv_palette_main(LV_PALETTE_RED), false, 0); + lv_meter_set_indicator_start_value(meter, indic, 80); + lv_meter_set_indicator_end_value(meter, indic, 100); + + /*Add a needle line indicator*/ + indic = lv_meter_add_needle_line(meter, scale, 4, lv_palette_main(LV_PALETTE_GREY), -10); + + /*Create an animation to set the value*/ + lv_anim_t a; + lv_anim_init(&a); + lv_anim_set_exec_cb(&a, set_value); + lv_anim_set_var(&a, indic); + lv_anim_set_values(&a, 0, 100); + lv_anim_set_time(&a, 2000); + lv_anim_set_repeat_delay(&a, 100); + lv_anim_set_playback_time(&a, 500); + lv_anim_set_playback_delay(&a, 100); + lv_anim_set_repeat_count(&a, LV_ANIM_REPEAT_INFINITE); + lv_anim_start(&a); +} diff --git a/examples/peripherals/lcd/gc9a01/main/lvgl_example_main.c b/examples/peripherals/lcd/gc9a01/main/lvgl_example_main.c new file mode 100644 index 0000000000..23fec9ba37 --- /dev/null +++ b/examples/peripherals/lcd/gc9a01/main/lvgl_example_main.c @@ -0,0 +1,170 @@ +/* + * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: CC0-1.0 + */ + +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_timer.h" +#include "esp_lcd_panel_io.h" +#include "esp_lcd_panel_vendor.h" +#include "esp_lcd_panel_ops.h" +#include "driver/gpio.h" +#include "driver/spi_master.h" +#include "esp_err.h" +#include "esp_log.h" +#include "lvgl.h" +#include "esp_lcd_panel_gc9a01.h" + +static const char *TAG = "example"; + +// Using SPI2 in the example +#define LCD_HOST SPI2_HOST + +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////// Please update the following configuration according to your LCD spec ////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// +#define EXAMPLE_LCD_PIXEL_CLOCK_HZ (20 * 1000 * 1000) +#define EXAMPLE_LCD_BK_LIGHT_ON_LEVEL 1 +#define EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL !EXAMPLE_LCD_BK_LIGHT_ON_LEVEL +#define EXAMPLE_PIN_NUM_DATA0 19 +#define EXAMPLE_PIN_NUM_PCLK 18 +#define EXAMPLE_PIN_NUM_CS 4 +#define EXAMPLE_PIN_NUM_DC 5 +#define EXAMPLE_PIN_NUM_RST 3 +#define EXAMPLE_PIN_NUM_BK_LIGHT 2 + +// The pixel number in horizontal and vertical +#define EXAMPLE_LCD_H_RES 240 +#define EXAMPLE_LCD_V_RES 240 +// Bit number used to represent command and parameter +#define EXAMPLE_LCD_CMD_BITS 8 +#define EXAMPLE_LCD_PARAM_BITS 8 + +#define EXAMPLE_LVGL_TICK_PERIOD_MS 2 + +extern void example_lvgl_demo_ui(lv_obj_t *scr); + +static bool example_notify_lvgl_flush_ready(esp_lcd_panel_io_handle_t panel_io, esp_lcd_panel_io_event_data_t *edata, void *user_ctx) +{ + lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx; + lv_disp_flush_ready(disp_driver); + return false; +} + +static void example_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); +} + +static void example_increase_lvgl_tick(void *arg) +{ + /* Tell LVGL how many milliseconds has elapsed */ + lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS); +} + +void app_main(void) +{ + static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s) + static lv_disp_drv_t disp_drv; // contains callback functions + + ESP_LOGI(TAG, "Turn off LCD backlight"); + gpio_config_t bk_gpio_config = { + .mode = GPIO_MODE_OUTPUT, + .pin_bit_mask = 1ULL << EXAMPLE_PIN_NUM_BK_LIGHT + }; + ESP_ERROR_CHECK(gpio_config(&bk_gpio_config)); + + ESP_LOGI(TAG, "Initialize SPI bus"); + spi_bus_config_t buscfg = { + .sclk_io_num = EXAMPLE_PIN_NUM_PCLK, + .mosi_io_num = EXAMPLE_PIN_NUM_DATA0, + .miso_io_num = -1, + .quadwp_io_num = -1, + .quadhd_io_num = -1, + .max_transfer_sz = EXAMPLE_LCD_H_RES * 80 * sizeof(uint16_t), + }; + ESP_ERROR_CHECK(spi_bus_initialize(LCD_HOST, &buscfg, SPI_DMA_CH_AUTO)); + + ESP_LOGI(TAG, "Install panel IO"); + esp_lcd_panel_io_handle_t io_handle = NULL; + esp_lcd_panel_io_spi_config_t io_config = { + .dc_gpio_num = EXAMPLE_PIN_NUM_DC, + .cs_gpio_num = EXAMPLE_PIN_NUM_CS, + .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ, + .lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS, + .lcd_param_bits = EXAMPLE_LCD_PARAM_BITS, + .spi_mode = 0, + .trans_queue_depth = 10, + .on_color_trans_done = example_notify_lvgl_flush_ready, + .user_ctx = &disp_drv, + }; + // Attach the LCD to the SPI bus + ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &io_config, &io_handle)); + + ESP_LOGI(TAG, "Install GC9A01 panel driver"); + esp_lcd_panel_handle_t panel_handle = NULL; + esp_lcd_panel_dev_config_t panel_config = { + .reset_gpio_num = EXAMPLE_PIN_NUM_RST, + .color_space = ESP_LCD_COLOR_SPACE_BGR, + .bits_per_pixel = 16, + }; + ESP_ERROR_CHECK(esp_lcd_new_panel_gc9a01(io_handle, &panel_config, &panel_handle)); + + ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle)); + ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle)); + ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true)); + ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, true, false)); + + ESP_LOGI(TAG, "Turn on LCD backlight"); + gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_ON_LEVEL); + + ESP_LOGI(TAG, "Initialize LVGL library"); + lv_init(); + // alloc draw buffers used by LVGL + // it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized + lv_color_t *buf1 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 20 * sizeof(lv_color_t), MALLOC_CAP_DMA); + assert(buf1); + lv_color_t *buf2 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 20 * sizeof(lv_color_t), MALLOC_CAP_DMA); + assert(buf2); + // initialize LVGL draw buffers + lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * 20); + + ESP_LOGI(TAG, "Register display driver to LVGL"); + lv_disp_drv_init(&disp_drv); + disp_drv.hor_res = EXAMPLE_LCD_H_RES; + disp_drv.ver_res = EXAMPLE_LCD_V_RES; + disp_drv.flush_cb = example_lvgl_flush_cb; + disp_drv.draw_buf = &disp_buf; + disp_drv.user_data = panel_handle; + 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 = &example_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, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000)); + + ESP_LOGI(TAG, "Display LVGL Meter Widget"); + lv_obj_t *scr = lv_disp_get_scr_act(disp); + example_lvgl_demo_ui(scr); + + while (1) { + // raise the task priority of LVGL and/or reduce the handler period can improve the performance + vTaskDelay(pdMS_TO_TICKS(10)); + // The task running lv_timer_handler should have lower priority than that running `lv_tick_inc` + lv_timer_handler(); + } +} diff --git a/examples/peripherals/lcd/gc9a01/sdkconfig.defaults b/examples/peripherals/lcd/gc9a01/sdkconfig.defaults new file mode 100644 index 0000000000..a2ac6edd21 --- /dev/null +++ b/examples/peripherals/lcd/gc9a01/sdkconfig.defaults @@ -0,0 +1,2 @@ +CONFIG_LV_USE_USER_DATA=y +CONFIG_LV_COLOR_16_SWAP=y