spi_lcd_touch_example_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 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 "driver/spi_master.h"
  15. #include "esp_err.h"
  16. #include "esp_log.h"
  17. #include "lvgl.h"
  18. #include "ui.h"
  19. #if CONFIG_EXAMPLE_LCD_CONTROLLER_ILI9341
  20. #include "esp_lcd_ili9341.h"
  21. #elif CONFIG_EXAMPLE_LCD_CONTROLLER_GC9A01
  22. #include "esp_lcd_gc9a01.h"
  23. #endif
  24. #if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_STMPE610
  25. #include "esp_lcd_touch_stmpe610.h"
  26. #endif
  27. static const char *TAG = "example";
  28. // Using SPI2 in the example
  29. #define LCD_HOST SPI2_HOST
  30. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. //////////////////// Please update the following configuration according to your LCD spec //////////////////////////////
  32. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  33. #define EXAMPLE_LCD_PIXEL_CLOCK_HZ (20 * 1000 * 1000)
  34. #define EXAMPLE_LCD_BK_LIGHT_ON_LEVEL 1
  35. #define EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL !EXAMPLE_LCD_BK_LIGHT_ON_LEVEL
  36. #define EXAMPLE_PIN_NUM_SCLK 6
  37. #define EXAMPLE_PIN_NUM_MOSI 7
  38. #define EXAMPLE_PIN_NUM_MISO 2
  39. #define EXAMPLE_PIN_NUM_LCD_DC 2
  40. #define EXAMPLE_PIN_NUM_LCD_RST -1
  41. #define EXAMPLE_PIN_NUM_LCD_CS 10
  42. #define EXAMPLE_PIN_NUM_BK_LIGHT 3
  43. #define EXAMPLE_PIN_NUM_TOUCH_CS 1
  44. // The pixel number in horizontal and vertical
  45. #if CONFIG_EXAMPLE_LCD_CONTROLLER_ILI9341
  46. #define EXAMPLE_LCD_H_RES 240
  47. #define EXAMPLE_LCD_V_RES 320
  48. #elif CONFIG_EXAMPLE_LCD_CONTROLLER_GC9A01
  49. #define EXAMPLE_LCD_H_RES 240
  50. #define EXAMPLE_LCD_V_RES 240
  51. #endif
  52. // Bit number used to represent command and parameter
  53. #define EXAMPLE_LCD_CMD_BITS 8
  54. #define EXAMPLE_LCD_PARAM_BITS 8
  55. #define EXAMPLE_LVGL_TICK_PERIOD_MS 2
  56. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  57. esp_lcd_touch_handle_t tp = NULL;
  58. #endif
  59. extern void example_lvgl_demo_ui(lv_disp_t *disp);
  60. extern void ui_Screen1_screen_init(void);
  61. extern void lv_example_1(void);
  62. 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)
  63. {
  64. lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
  65. lv_disp_flush_ready(disp_driver);
  66. return false;
  67. }
  68. static void example_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
  69. {
  70. esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;
  71. int offsetx1 = area->x1;
  72. int offsetx2 = area->x2;
  73. int offsety1 = area->y1;
  74. int offsety2 = area->y2;
  75. // copy a buffer's content to a specific area of the display
  76. esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
  77. }
  78. /* Rotate display and touch, when rotated screen in LVGL. Called when driver parameters are updated. */
  79. static void example_lvgl_port_update_callback(lv_disp_drv_t *drv)
  80. {
  81. esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;
  82. switch (drv->rotated) {
  83. case LV_DISP_ROT_NONE:
  84. // Rotate LCD display
  85. esp_lcd_panel_swap_xy(panel_handle, false);
  86. esp_lcd_panel_mirror(panel_handle, true, false);
  87. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  88. // Rotate LCD touch
  89. esp_lcd_touch_set_mirror_y(tp, false);
  90. esp_lcd_touch_set_mirror_x(tp, false);
  91. #endif
  92. break;
  93. case LV_DISP_ROT_90:
  94. // Rotate LCD display
  95. esp_lcd_panel_swap_xy(panel_handle, true);
  96. esp_lcd_panel_mirror(panel_handle, true, true);
  97. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  98. // Rotate LCD touch
  99. esp_lcd_touch_set_mirror_y(tp, false);
  100. esp_lcd_touch_set_mirror_x(tp, false);
  101. #endif
  102. break;
  103. case LV_DISP_ROT_180:
  104. // Rotate LCD display
  105. esp_lcd_panel_swap_xy(panel_handle, false);
  106. esp_lcd_panel_mirror(panel_handle, false, true);
  107. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  108. // Rotate LCD touch
  109. esp_lcd_touch_set_mirror_y(tp, false);
  110. esp_lcd_touch_set_mirror_x(tp, false);
  111. #endif
  112. break;
  113. case LV_DISP_ROT_270:
  114. // Rotate LCD display
  115. esp_lcd_panel_swap_xy(panel_handle, true);
  116. esp_lcd_panel_mirror(panel_handle, false, false);
  117. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  118. // Rotate LCD touch
  119. esp_lcd_touch_set_mirror_y(tp, false);
  120. esp_lcd_touch_set_mirror_x(tp, false);
  121. #endif
  122. break;
  123. }
  124. }
  125. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  126. static void example_lvgl_touch_cb(lv_indev_drv_t * drv, lv_indev_data_t * data)
  127. {
  128. uint16_t touchpad_x[1] = {0};
  129. uint16_t touchpad_y[1] = {0};
  130. uint8_t touchpad_cnt = 0;
  131. /* Read touch controller data */
  132. esp_lcd_touch_read_data(drv->user_data);
  133. /* Get coordinates */
  134. bool touchpad_pressed = esp_lcd_touch_get_coordinates(drv->user_data, touchpad_x, touchpad_y, NULL, &touchpad_cnt, 1);
  135. if (touchpad_pressed && touchpad_cnt > 0) {
  136. data->point.x = touchpad_x[0];
  137. data->point.y = touchpad_y[0];
  138. data->state = LV_INDEV_STATE_PRESSED;
  139. } else {
  140. data->state = LV_INDEV_STATE_RELEASED;
  141. }
  142. }
  143. #endif
  144. static void example_increase_lvgl_tick(void *arg)
  145. {
  146. /* Tell LVGL how many milliseconds has elapsed */
  147. lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
  148. }
  149. void app_main(void)
  150. {
  151. static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
  152. static lv_disp_drv_t disp_drv; // contains callback functions
  153. ESP_LOGI(TAG, "Turn off LCD backlight");
  154. gpio_config_t bk_gpio_config = {
  155. .mode = GPIO_MODE_OUTPUT,
  156. .pin_bit_mask = 1ULL << EXAMPLE_PIN_NUM_BK_LIGHT
  157. };
  158. ESP_ERROR_CHECK(gpio_config(&bk_gpio_config));
  159. ESP_LOGI(TAG, "Initialize SPI bus");
  160. spi_bus_config_t buscfg = {
  161. .sclk_io_num = EXAMPLE_PIN_NUM_SCLK,
  162. .mosi_io_num = EXAMPLE_PIN_NUM_MOSI,
  163. .miso_io_num = EXAMPLE_PIN_NUM_MISO,
  164. .quadwp_io_num = -1,
  165. .quadhd_io_num = -1,
  166. .max_transfer_sz = EXAMPLE_LCD_H_RES * 80 * sizeof(uint16_t),
  167. };
  168. ESP_ERROR_CHECK(spi_bus_initialize(LCD_HOST, &buscfg, SPI_DMA_CH_AUTO));
  169. ESP_LOGI(TAG, "Install panel IO");
  170. esp_lcd_panel_io_handle_t io_handle = NULL;
  171. esp_lcd_panel_io_spi_config_t io_config = {
  172. .dc_gpio_num = EXAMPLE_PIN_NUM_LCD_DC,
  173. .cs_gpio_num = EXAMPLE_PIN_NUM_LCD_CS,
  174. .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
  175. .lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS,
  176. .lcd_param_bits = EXAMPLE_LCD_PARAM_BITS,
  177. .spi_mode = 0,
  178. .trans_queue_depth = 10,
  179. .on_color_trans_done = example_notify_lvgl_flush_ready,
  180. .user_ctx = &disp_drv,
  181. };
  182. // Attach the LCD to the SPI bus
  183. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &io_config, &io_handle));
  184. esp_lcd_panel_handle_t panel_handle = NULL;
  185. esp_lcd_panel_dev_config_t panel_config = {
  186. .reset_gpio_num = EXAMPLE_PIN_NUM_LCD_RST,
  187. .rgb_endian = LCD_RGB_ENDIAN_BGR,
  188. .bits_per_pixel = 16,
  189. };
  190. #if CONFIG_EXAMPLE_LCD_CONTROLLER_ILI9341
  191. ESP_LOGI(TAG, "Install ILI9341 panel driver");
  192. ESP_ERROR_CHECK(esp_lcd_new_panel_ili9341(io_handle, &panel_config, &panel_handle));
  193. #elif CONFIG_EXAMPLE_LCD_CONTROLLER_GC9A01
  194. ESP_LOGI(TAG, "Install GC9A01 panel driver");
  195. ESP_ERROR_CHECK(esp_lcd_new_panel_gc9a01(io_handle, &panel_config, &panel_handle));
  196. #endif
  197. ESP_ERROR_CHECK(esp_lcd_panel_reset(panel_handle));
  198. ESP_ERROR_CHECK(esp_lcd_panel_init(panel_handle));
  199. #if CONFIG_EXAMPLE_LCD_CONTROLLER_GC9A01
  200. ESP_ERROR_CHECK(esp_lcd_panel_invert_color(panel_handle, true));
  201. #endif
  202. ESP_ERROR_CHECK(esp_lcd_panel_mirror(panel_handle, true, false));
  203. // user can flush pre-defined pattern to the screen before we turn on the screen or backlight
  204. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
  205. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  206. esp_lcd_panel_io_handle_t tp_io_handle = NULL;
  207. esp_lcd_panel_io_spi_config_t tp_io_config = ESP_LCD_TOUCH_IO_SPI_STMPE610_CONFIG(EXAMPLE_PIN_NUM_TOUCH_CS);
  208. // Attach the TOUCH to the SPI bus
  209. ESP_ERROR_CHECK(esp_lcd_new_panel_io_spi((esp_lcd_spi_bus_handle_t)LCD_HOST, &tp_io_config, &tp_io_handle));
  210. esp_lcd_touch_config_t tp_cfg = {
  211. .x_max = EXAMPLE_LCD_H_RES,
  212. .y_max = EXAMPLE_LCD_V_RES,
  213. .rst_gpio_num = -1,
  214. .int_gpio_num = -1,
  215. .flags = {
  216. .swap_xy = 0,
  217. .mirror_x = 0,
  218. .mirror_y = 0,
  219. },
  220. };
  221. #if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_STMPE610
  222. ESP_LOGI(TAG, "Initialize touch controller STMPE610");
  223. ESP_ERROR_CHECK(esp_lcd_touch_new_spi_stmpe610(tp_io_handle, &tp_cfg, &tp));
  224. #endif // CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_STMPE610
  225. #endif // CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  226. ESP_LOGI(TAG, "Turn on LCD backlight");
  227. gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_ON_LEVEL);
  228. ESP_LOGI(TAG, "Initialize LVGL library");
  229. lv_init();
  230. // alloc draw buffers used by LVGL
  231. // it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
  232. lv_color_t *buf1 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 20 * sizeof(lv_color_t), MALLOC_CAP_DMA);
  233. assert(buf1);
  234. lv_color_t *buf2 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 20 * sizeof(lv_color_t), MALLOC_CAP_DMA);
  235. assert(buf2);
  236. // initialize LVGL draw buffers
  237. lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * 20);
  238. ESP_LOGI(TAG, "Register display driver to LVGL");
  239. lv_disp_drv_init(&disp_drv);
  240. disp_drv.hor_res = EXAMPLE_LCD_H_RES;
  241. disp_drv.ver_res = EXAMPLE_LCD_V_RES;
  242. disp_drv.flush_cb = example_lvgl_flush_cb;
  243. disp_drv.drv_update_cb = example_lvgl_port_update_callback;
  244. disp_drv.draw_buf = &disp_buf;
  245. disp_drv.user_data = panel_handle;
  246. lv_disp_t *disp = lv_disp_drv_register(&disp_drv);
  247. ESP_LOGI(TAG, "Install LVGL tick timer");
  248. // Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
  249. const esp_timer_create_args_t lvgl_tick_timer_args = {
  250. .callback = &example_increase_lvgl_tick,
  251. .name = "lvgl_tick"
  252. };
  253. esp_timer_handle_t lvgl_tick_timer = NULL;
  254. ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
  255. ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000));
  256. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  257. static lv_indev_drv_t indev_drv; // Input device driver (Touch)
  258. lv_indev_drv_init(&indev_drv);
  259. indev_drv.type = LV_INDEV_TYPE_POINTER;
  260. indev_drv.disp = disp;
  261. indev_drv.read_cb = example_lvgl_touch_cb;
  262. indev_drv.user_data = tp;
  263. lv_indev_drv_register(&indev_drv);
  264. #endif
  265. ESP_LOGI(TAG, "Display LVGL Meter Widget");
  266. // example_lvgl_demo_ui(disp);
  267. // ui_Screen1_screen_init();
  268. // lv_example_1();
  269. ui_init();
  270. while (1) {
  271. // raise the task priority of LVGL and/or reduce the handler period can improve the performance
  272. vTaskDelay(pdMS_TO_TICKS(10));
  273. // The task running lv_timer_handler should have lower priority than that running `lv_tick_inc`
  274. lv_timer_handler();
  275. }
  276. }