lvgl_example_main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: CC0-1.0
  5. */
  6. #include <stdio.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "esp_timer.h"
  10. #include "esp_lcd_panel_io.h"
  11. #include "esp_lcd_panel_vendor.h"
  12. #include "esp_lcd_panel_ops.h"
  13. #include "driver/gpio.h"
  14. #include "esp_err.h"
  15. #include "esp_log.h"
  16. #include "lvgl.h"
  17. static const char *TAG = "example";
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  19. //////////////////// Please update the following configuration according to your LCD spec //////////////////////////////
  20. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. #define EXAMPLE_LCD_PIXEL_CLOCK_HZ (10 * 1000 * 1000)
  22. #define EXAMPLE_LCD_BK_LIGHT_ON_LEVEL 1
  23. #define EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL !EXAMPLE_LCD_BK_LIGHT_ON_LEVEL
  24. #define EXAMPLE_PIN_NUM_DATA0 19
  25. #define EXAMPLE_PIN_NUM_DATA1 21
  26. #define EXAMPLE_PIN_NUM_DATA2 0
  27. #define EXAMPLE_PIN_NUM_DATA3 22
  28. #define EXAMPLE_PIN_NUM_DATA4 23
  29. #define EXAMPLE_PIN_NUM_DATA5 33
  30. #define EXAMPLE_PIN_NUM_DATA6 32
  31. #define EXAMPLE_PIN_NUM_DATA7 27
  32. #define EXAMPLE_PIN_NUM_PCLK 18
  33. #define EXAMPLE_PIN_NUM_CS 4
  34. #define EXAMPLE_PIN_NUM_DC 5
  35. #define EXAMPLE_PIN_NUM_RST -1
  36. #define EXAMPLE_PIN_NUM_BK_LIGHT 2
  37. // The pixel number in horizontal and vertical
  38. #define EXAMPLE_LCD_H_RES 240
  39. #define EXAMPLE_LCD_V_RES 280
  40. // Bit number used to represent command and parameter
  41. #define EXAMPLE_LCD_CMD_BITS 8
  42. #define EXAMPLE_LCD_PARAM_BITS 8
  43. #define EXAMPLE_LVGL_TICK_PERIOD_MS 2
  44. extern void example_lvgl_demo_ui(lv_obj_t *scr);
  45. 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)
  46. {
  47. lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
  48. lv_disp_flush_ready(disp_driver);
  49. return false;
  50. }
  51. static void example_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
  52. {
  53. esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;
  54. int offsetx1 = area->x1;
  55. int offsetx2 = area->x2;
  56. int offsety1 = area->y1;
  57. int offsety2 = area->y2;
  58. // copy a buffer's content to a specific area of the display
  59. esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
  60. }
  61. static void example_increase_lvgl_tick(void *arg)
  62. {
  63. /* Tell LVGL how many milliseconds has elapsed */
  64. lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
  65. }
  66. void app_main(void)
  67. {
  68. static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
  69. static lv_disp_drv_t disp_drv; // contains callback functions
  70. ESP_LOGI(TAG, "Turn off LCD backlight");
  71. gpio_config_t bk_gpio_config = {
  72. .mode = GPIO_MODE_OUTPUT,
  73. .pin_bit_mask = 1ULL << EXAMPLE_PIN_NUM_BK_LIGHT
  74. };
  75. ESP_ERROR_CHECK(gpio_config(&bk_gpio_config));
  76. gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL);
  77. ESP_LOGI(TAG, "Initialize Intel 8080 bus");
  78. esp_lcd_i80_bus_handle_t i80_bus = NULL;
  79. esp_lcd_i80_bus_config_t bus_config = {
  80. .dc_gpio_num = EXAMPLE_PIN_NUM_DC,
  81. .wr_gpio_num = EXAMPLE_PIN_NUM_PCLK,
  82. .data_gpio_nums = {
  83. EXAMPLE_PIN_NUM_DATA0,
  84. EXAMPLE_PIN_NUM_DATA1,
  85. EXAMPLE_PIN_NUM_DATA2,
  86. EXAMPLE_PIN_NUM_DATA3,
  87. EXAMPLE_PIN_NUM_DATA4,
  88. EXAMPLE_PIN_NUM_DATA5,
  89. EXAMPLE_PIN_NUM_DATA6,
  90. EXAMPLE_PIN_NUM_DATA7,
  91. },
  92. .bus_width = 8,
  93. .max_transfer_bytes = EXAMPLE_LCD_H_RES * 40 * sizeof(uint16_t)
  94. };
  95. ESP_ERROR_CHECK(esp_lcd_new_i80_bus(&bus_config, &i80_bus));
  96. esp_lcd_panel_io_handle_t io_handle = NULL;
  97. esp_lcd_panel_io_i80_config_t io_config = {
  98. .cs_gpio_num = EXAMPLE_PIN_NUM_CS,
  99. .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
  100. .trans_queue_depth = 10,
  101. .dc_levels = {
  102. .dc_idle_level = 0,
  103. .dc_cmd_level = 0,
  104. .dc_dummy_level = 0,
  105. .dc_data_level = 1,
  106. },
  107. .on_color_trans_done = example_notify_lvgl_flush_ready,
  108. .user_ctx = &disp_drv,
  109. .lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS,
  110. .lcd_param_bits = EXAMPLE_LCD_PARAM_BITS,
  111. };
  112. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i80(i80_bus, &io_config, &io_handle));
  113. ESP_LOGI(TAG, "Install LCD driver of st7789");
  114. esp_lcd_panel_handle_t panel_handle = NULL;
  115. esp_lcd_panel_dev_config_t panel_config = {
  116. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  117. .color_space = ESP_LCD_COLOR_SPACE_RGB,
  118. .bits_per_pixel = 16,
  119. };
  120. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
  121. esp_lcd_panel_reset(panel_handle);
  122. esp_lcd_panel_init(panel_handle);
  123. esp_lcd_panel_invert_color(panel_handle, true);
  124. // the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
  125. esp_lcd_panel_set_gap(panel_handle, 0, 20);
  126. ESP_LOGI(TAG, "Turn on LCD backlight");
  127. gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_ON_LEVEL);
  128. ESP_LOGI(TAG, "Initialize LVGL library");
  129. lv_init();
  130. // alloc draw buffers used by LVGL
  131. // it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
  132. lv_color_t *buf1 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 20 * sizeof(lv_color_t), MALLOC_CAP_DMA);
  133. assert(buf1);
  134. lv_color_t *buf2 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 20 * sizeof(lv_color_t), MALLOC_CAP_DMA);
  135. assert(buf2);
  136. // initialize LVGL draw buffers
  137. lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * 20);
  138. ESP_LOGI(TAG, "Register display driver to LVGL");
  139. lv_disp_drv_init(&disp_drv);
  140. disp_drv.hor_res = EXAMPLE_LCD_H_RES;
  141. disp_drv.ver_res = EXAMPLE_LCD_V_RES;
  142. disp_drv.flush_cb = example_lvgl_flush_cb;
  143. disp_drv.draw_buf = &disp_buf;
  144. disp_drv.user_data = panel_handle;
  145. lv_disp_t *disp = lv_disp_drv_register(&disp_drv);
  146. ESP_LOGI(TAG, "Install LVGL tick timer");
  147. // Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
  148. const esp_timer_create_args_t lvgl_tick_timer_args = {
  149. .callback = &example_increase_lvgl_tick,
  150. .name = "lvgl_tick"
  151. };
  152. esp_timer_handle_t lvgl_tick_timer = NULL;
  153. ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
  154. ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000));
  155. ESP_LOGI(TAG, "Display LVGL animation");
  156. lv_obj_t *scr = lv_disp_get_scr_act(disp);
  157. example_lvgl_demo_ui(scr);
  158. while (1) {
  159. // raise the task priority of LVGL and/or reduce the handler period can improve the performance
  160. vTaskDelay(pdMS_TO_TICKS(10));
  161. // The task running lv_timer_handler should have lower priority than that running `lv_tick_inc`
  162. lv_timer_handler();
  163. }
  164. }