spi_lcd_touch_example_main.c 13 KB

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