i80_controller_example_main.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 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 "esp_lcd_touch.h"
  14. #include "esp_spiffs.h"
  15. #include "driver/gpio.h"
  16. #include "driver/i2c.h"
  17. #include "esp_err.h"
  18. #include "esp_log.h"
  19. #include "lvgl.h"
  20. #if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_GT911
  21. #include "esp_lcd_touch_gt911.h"
  22. #elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_TT21100
  23. #include "esp_lcd_touch_tt21100.h"
  24. #elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_FT5X06
  25. #include "esp_lcd_touch_ft5x06.h"
  26. #endif
  27. static const char *TAG = "example";
  28. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  29. //////////////////// Please update the following configuration according to your LCD spec //////////////////////////////
  30. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  31. #if CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  32. // PCLK frequency can't go too high as the limitation of PSRAM bandwidth
  33. #define EXAMPLE_LCD_PIXEL_CLOCK_HZ (2 * 1000 * 1000)
  34. #else
  35. #define EXAMPLE_LCD_PIXEL_CLOCK_HZ (10 * 1000 * 1000)
  36. #endif // CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  37. #define EXAMPLE_LCD_BK_LIGHT_ON_LEVEL 1
  38. #define EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL !EXAMPLE_LCD_BK_LIGHT_ON_LEVEL
  39. #define EXAMPLE_PIN_NUM_DATA0 6
  40. #define EXAMPLE_PIN_NUM_DATA1 7
  41. #define EXAMPLE_PIN_NUM_DATA2 8
  42. #define EXAMPLE_PIN_NUM_DATA3 9
  43. #define EXAMPLE_PIN_NUM_DATA4 10
  44. #define EXAMPLE_PIN_NUM_DATA5 11
  45. #define EXAMPLE_PIN_NUM_DATA6 12
  46. #define EXAMPLE_PIN_NUM_DATA7 13
  47. #if CONFIG_EXAMPLE_LCD_I80_BUS_WIDTH > 8
  48. #define EXAMPLE_PIN_NUM_DATA8 14
  49. #define EXAMPLE_PIN_NUM_DATA9 15
  50. #define EXAMPLE_PIN_NUM_DATA10 16
  51. #define EXAMPLE_PIN_NUM_DATA11 17
  52. #define EXAMPLE_PIN_NUM_DATA12 18
  53. #define EXAMPLE_PIN_NUM_DATA13 19
  54. #define EXAMPLE_PIN_NUM_DATA14 20
  55. #define EXAMPLE_PIN_NUM_DATA15 21
  56. #endif
  57. #define EXAMPLE_PIN_NUM_PCLK 5
  58. #define EXAMPLE_PIN_NUM_CS 3
  59. #define EXAMPLE_PIN_NUM_DC 4
  60. #define EXAMPLE_PIN_NUM_RST 2
  61. #define EXAMPLE_PIN_NUM_BK_LIGHT 1
  62. // The pixel number in horizontal and vertical
  63. #define EXAMPLE_LCD_H_RES 240
  64. #define EXAMPLE_LCD_V_RES 280
  65. // Bit number used to represent command and parameter
  66. #if CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ST7789
  67. #define EXAMPLE_LCD_CMD_BITS 8
  68. #define EXAMPLE_LCD_PARAM_BITS 8
  69. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_NT35510
  70. #define EXAMPLE_LCD_CMD_BITS 16
  71. #define EXAMPLE_LCD_PARAM_BITS 16
  72. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ILI9341
  73. #define EXAMPLE_LCD_CMD_BITS 8
  74. #define EXAMPLE_LCD_PARAM_BITS 8
  75. #endif
  76. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  77. #define EXAMPLE_I2C_NUM 0 // I2C number
  78. #define EXAMPLE_I2C_SCL 39
  79. #define EXAMPLE_I2C_SDA 40
  80. #endif
  81. #define EXAMPLE_LVGL_TICK_PERIOD_MS 2
  82. // Supported alignment: 16, 32, 64. A higher alignment can enables higher burst transfer size, thus a higher i80 bus throughput.
  83. #define EXAMPLE_PSRAM_DATA_ALIGNMENT 64
  84. extern void example_lvgl_demo_ui(lv_disp_t *disp);
  85. 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)
  86. {
  87. lv_disp_drv_t *disp_driver = (lv_disp_drv_t *)user_ctx;
  88. lv_disp_flush_ready(disp_driver);
  89. return false;
  90. }
  91. static void example_lvgl_flush_cb(lv_disp_drv_t *drv, const lv_area_t *area, lv_color_t *color_map)
  92. {
  93. esp_lcd_panel_handle_t panel_handle = (esp_lcd_panel_handle_t) drv->user_data;
  94. int offsetx1 = area->x1;
  95. int offsetx2 = area->x2;
  96. int offsety1 = area->y1;
  97. int offsety2 = area->y2;
  98. // copy a buffer's content to a specific area of the display
  99. esp_lcd_panel_draw_bitmap(panel_handle, offsetx1, offsety1, offsetx2 + 1, offsety2 + 1, color_map);
  100. }
  101. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  102. static void example_lvgl_touch_cb(lv_indev_drv_t *drv, lv_indev_data_t *data)
  103. {
  104. uint16_t touchpad_x[1] = {0};
  105. uint16_t touchpad_y[1] = {0};
  106. uint8_t touchpad_cnt = 0;
  107. /* Read touch controller data */
  108. esp_lcd_touch_read_data(drv->user_data);
  109. /* Get coordinates */
  110. bool touchpad_pressed = esp_lcd_touch_get_coordinates(drv->user_data, touchpad_x, touchpad_y, NULL, &touchpad_cnt, 1);
  111. if (touchpad_pressed && touchpad_cnt > 0) {
  112. data->point.x = touchpad_x[0];
  113. data->point.y = touchpad_y[0];
  114. data->state = LV_INDEV_STATE_PRESSED;
  115. } else {
  116. data->state = LV_INDEV_STATE_RELEASED;
  117. }
  118. }
  119. #endif
  120. static void example_increase_lvgl_tick(void *arg)
  121. {
  122. /* Tell LVGL how many milliseconds has elapsed */
  123. lv_tick_inc(EXAMPLE_LVGL_TICK_PERIOD_MS);
  124. }
  125. #if CONFIG_EXAMPLE_LCD_IMAGE_FROM_FILE_SYSTEM
  126. void example_init_filesystem(void)
  127. {
  128. ESP_LOGI(TAG, "Initializing filesystem");
  129. esp_vfs_spiffs_conf_t conf = {
  130. .base_path = "/spiffs",
  131. .partition_label = "storage",
  132. .max_files = 5,
  133. .format_if_mount_failed = true
  134. };
  135. // Use settings defined above to initialize and mount SPIFFS filesystem.
  136. // Note: esp_vfs_spiffs_register is an all-in-one convenience function.
  137. esp_err_t ret = esp_vfs_spiffs_register(&conf);
  138. if (ret != ESP_OK) {
  139. if (ret == ESP_FAIL) {
  140. ESP_LOGE(TAG, "Failed to mount or format filesystem");
  141. } else if (ret == ESP_ERR_NOT_FOUND) {
  142. ESP_LOGE(TAG, "Failed to find SPIFFS partition");
  143. } else {
  144. ESP_LOGE(TAG, "Failed to initialize SPIFFS (%s)", esp_err_to_name(ret));
  145. }
  146. return;
  147. }
  148. size_t total = 0, used = 0;
  149. ret = esp_spiffs_info(conf.partition_label, &total, &used);
  150. if (ret != ESP_OK) {
  151. ESP_LOGE(TAG, "Failed to get SPIFFS partition information (%s). Formatting...", esp_err_to_name(ret));
  152. esp_spiffs_format(conf.partition_label);
  153. return;
  154. } else {
  155. ESP_LOGI(TAG, "Partition size: total: %zu, used: %zu", total, used);
  156. }
  157. }
  158. #endif // CONFIG_EXAMPLE_LCD_IMAGE_FROM_FILE_SYSTEM
  159. void example_init_i80_bus(esp_lcd_panel_io_handle_t *io_handle, void *user_ctx)
  160. {
  161. ESP_LOGI(TAG, "Initialize Intel 8080 bus");
  162. esp_lcd_i80_bus_handle_t i80_bus = NULL;
  163. esp_lcd_i80_bus_config_t bus_config = {
  164. .clk_src = LCD_CLK_SRC_DEFAULT,
  165. .dc_gpio_num = EXAMPLE_PIN_NUM_DC,
  166. .wr_gpio_num = EXAMPLE_PIN_NUM_PCLK,
  167. .data_gpio_nums = {
  168. EXAMPLE_PIN_NUM_DATA0,
  169. EXAMPLE_PIN_NUM_DATA1,
  170. EXAMPLE_PIN_NUM_DATA2,
  171. EXAMPLE_PIN_NUM_DATA3,
  172. EXAMPLE_PIN_NUM_DATA4,
  173. EXAMPLE_PIN_NUM_DATA5,
  174. EXAMPLE_PIN_NUM_DATA6,
  175. EXAMPLE_PIN_NUM_DATA7,
  176. #if CONFIG_EXAMPLE_LCD_I80_BUS_WIDTH > 8
  177. EXAMPLE_PIN_NUM_DATA8,
  178. EXAMPLE_PIN_NUM_DATA9,
  179. EXAMPLE_PIN_NUM_DATA10,
  180. EXAMPLE_PIN_NUM_DATA11,
  181. EXAMPLE_PIN_NUM_DATA12,
  182. EXAMPLE_PIN_NUM_DATA13,
  183. EXAMPLE_PIN_NUM_DATA14,
  184. EXAMPLE_PIN_NUM_DATA15,
  185. #endif
  186. },
  187. .bus_width = CONFIG_EXAMPLE_LCD_I80_BUS_WIDTH,
  188. .max_transfer_bytes = EXAMPLE_LCD_H_RES * 100 * sizeof(uint16_t),
  189. .psram_trans_align = EXAMPLE_PSRAM_DATA_ALIGNMENT,
  190. .sram_trans_align = 4,
  191. };
  192. ESP_ERROR_CHECK(esp_lcd_new_i80_bus(&bus_config, &i80_bus));
  193. esp_lcd_panel_io_i80_config_t io_config = {
  194. .cs_gpio_num = EXAMPLE_PIN_NUM_CS,
  195. .pclk_hz = EXAMPLE_LCD_PIXEL_CLOCK_HZ,
  196. .trans_queue_depth = 10,
  197. .dc_levels = {
  198. .dc_idle_level = 0,
  199. .dc_cmd_level = 0,
  200. .dc_dummy_level = 0,
  201. .dc_data_level = 1,
  202. },
  203. .flags = {
  204. .swap_color_bytes = !LV_COLOR_16_SWAP, // Swap can be done in LvGL (default) or DMA
  205. },
  206. .on_color_trans_done = example_notify_lvgl_flush_ready,
  207. .user_ctx = user_ctx,
  208. .lcd_cmd_bits = EXAMPLE_LCD_CMD_BITS,
  209. .lcd_param_bits = EXAMPLE_LCD_PARAM_BITS,
  210. };
  211. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i80(i80_bus, &io_config, io_handle));
  212. }
  213. void example_init_lcd_panel(esp_lcd_panel_io_handle_t io_handle, esp_lcd_panel_handle_t *panel)
  214. {
  215. esp_lcd_panel_handle_t panel_handle = NULL;
  216. #if CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ST7789
  217. ESP_LOGI(TAG, "Install LCD driver of st7789");
  218. esp_lcd_panel_dev_config_t panel_config = {
  219. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  220. .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_RGB,
  221. .bits_per_pixel = 16,
  222. };
  223. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
  224. esp_lcd_panel_reset(panel_handle);
  225. esp_lcd_panel_init(panel_handle);
  226. // Set inversion, x/y coordinate order, x/y mirror according to your LCD module spec
  227. // the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
  228. esp_lcd_panel_invert_color(panel_handle, true);
  229. esp_lcd_panel_set_gap(panel_handle, 0, 20);
  230. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_NT35510
  231. ESP_LOGI(TAG, "Install LCD driver of nt35510");
  232. esp_lcd_panel_dev_config_t panel_config = {
  233. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  234. .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR,
  235. .bits_per_pixel = 16,
  236. };
  237. ESP_ERROR_CHECK(esp_lcd_new_panel_nt35510(io_handle, &panel_config, &panel_handle));
  238. esp_lcd_panel_reset(panel_handle);
  239. esp_lcd_panel_init(panel_handle);
  240. // Set inversion, x/y coordinate order, x/y mirror according to your LCD module spec
  241. // the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
  242. esp_lcd_panel_swap_xy(panel_handle, true);
  243. esp_lcd_panel_mirror(panel_handle, true, false);
  244. #elif CONFIG_EXAMPLE_LCD_I80_CONTROLLER_ILI9341
  245. // ILI9341 is NOT a distinct driver, but a special case of ST7789
  246. // (essential registers are identical). A few lines further down in this code,
  247. // it's shown how to issue additional device-specific commands.
  248. ESP_LOGI(TAG, "Install LCD driver of ili9341 (st7789 compatible)");
  249. esp_lcd_panel_dev_config_t panel_config = {
  250. .reset_gpio_num = EXAMPLE_PIN_NUM_RST,
  251. .rgb_ele_order = LCD_RGB_ELEMENT_ORDER_BGR,
  252. .bits_per_pixel = 16,
  253. };
  254. ESP_ERROR_CHECK(esp_lcd_new_panel_st7789(io_handle, &panel_config, &panel_handle));
  255. esp_lcd_panel_reset(panel_handle);
  256. esp_lcd_panel_init(panel_handle);
  257. // Set inversion, x/y coordinate order, x/y mirror according to your LCD module spec
  258. // the gap is LCD panel specific, even panels with the same driver IC, can have different gap value
  259. esp_lcd_panel_swap_xy(panel_handle, true);
  260. esp_lcd_panel_invert_color(panel_handle, false);
  261. // ILI9341 is very similar to ST7789 and shares the same driver.
  262. // Anything unconventional (such as this custom gamma table) can
  263. // be issued here in user code and need not modify the driver.
  264. esp_lcd_panel_io_tx_param(io_handle, 0xF2, (uint8_t[]) {
  265. 0
  266. }, 1); // 3Gamma function disable
  267. esp_lcd_panel_io_tx_param(io_handle, 0x26, (uint8_t[]) {
  268. 1
  269. }, 1); // Gamma curve 1 selected
  270. esp_lcd_panel_io_tx_param(io_handle, 0xE0, (uint8_t[]) { // Set positive gamma
  271. 0x0F, 0x31, 0x2B, 0x0C, 0x0E, 0x08, 0x4E, 0xF1, 0x37, 0x07, 0x10, 0x03, 0x0E, 0x09, 0x00
  272. }, 15);
  273. esp_lcd_panel_io_tx_param(io_handle, 0xE1, (uint8_t[]) { // Set negative gamma
  274. 0x00, 0x0E, 0x14, 0x03, 0x11, 0x07, 0x31, 0xC1, 0x48, 0x08, 0x0F, 0x0C, 0x31, 0x36, 0x0F
  275. }, 15);
  276. #endif
  277. *panel = panel_handle;
  278. }
  279. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  280. void example_init_lcd_touch(esp_lcd_touch_handle_t *tp_handle)
  281. {
  282. esp_lcd_touch_handle_t tp = NULL;
  283. esp_lcd_panel_io_handle_t tp_io_handle = NULL;
  284. const i2c_config_t i2c_conf = {
  285. .mode = I2C_MODE_MASTER,
  286. .sda_io_num = EXAMPLE_I2C_SDA,
  287. .scl_io_num = EXAMPLE_I2C_SCL,
  288. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  289. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  290. .master.clk_speed = 400000,
  291. };
  292. /* Initialize I2C */
  293. ESP_ERROR_CHECK(i2c_param_config(EXAMPLE_I2C_NUM, &i2c_conf));
  294. ESP_ERROR_CHECK(i2c_driver_install(EXAMPLE_I2C_NUM, i2c_conf.mode, 0, 0, 0));
  295. #if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_GT911
  296. esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_GT911_CONFIG();
  297. #elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_TT21100
  298. esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_TT21100_CONFIG();
  299. #elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_FT5X06
  300. esp_lcd_panel_io_i2c_config_t tp_io_config = ESP_LCD_TOUCH_IO_I2C_FT5x06_CONFIG();
  301. #endif
  302. ESP_LOGI(TAG, "Initialize touch IO (I2C)");
  303. /* Touch IO handle */
  304. ESP_ERROR_CHECK(esp_lcd_new_panel_io_i2c((esp_lcd_i2c_bus_handle_t)EXAMPLE_I2C_NUM, &tp_io_config, &tp_io_handle));
  305. esp_lcd_touch_config_t tp_cfg = {
  306. .x_max = EXAMPLE_LCD_V_RES,
  307. .y_max = EXAMPLE_LCD_H_RES,
  308. .rst_gpio_num = -1,
  309. .int_gpio_num = -1,
  310. .flags = {
  311. .swap_xy = 1,
  312. .mirror_x = 1,
  313. .mirror_y = 0,
  314. },
  315. };
  316. /* Initialize touch */
  317. #if CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_GT911
  318. ESP_LOGI(TAG, "Initialize touch controller GT911");
  319. ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_gt911(tp_io_handle, &tp_cfg, &tp));
  320. #elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_TT21100
  321. ESP_LOGI(TAG, "Initialize touch controller TT21100");
  322. ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_tt21100(tp_io_handle, &tp_cfg, &tp));
  323. #elif CONFIG_EXAMPLE_LCD_TOUCH_CONTROLLER_FT5X06
  324. ESP_LOGI(TAG, "Initialize touch controller FT5X06");
  325. ESP_ERROR_CHECK(esp_lcd_touch_new_i2c_ft5x06(tp_io_handle, &tp_cfg, &tp));
  326. #endif
  327. *tp_handle = tp;
  328. }
  329. #endif // CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  330. void app_main(void)
  331. {
  332. static lv_disp_draw_buf_t disp_buf; // contains internal graphic buffer(s) called draw buffer(s)
  333. static lv_disp_drv_t disp_drv; // contains callback functions
  334. #if EXAMPLE_PIN_NUM_BK_LIGHT >= 0
  335. ESP_LOGI(TAG, "Turn off LCD backlight");
  336. gpio_config_t bk_gpio_config = {
  337. .mode = GPIO_MODE_OUTPUT,
  338. .pin_bit_mask = 1ULL << EXAMPLE_PIN_NUM_BK_LIGHT
  339. };
  340. ESP_ERROR_CHECK(gpio_config(&bk_gpio_config));
  341. gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_OFF_LEVEL);
  342. #endif // EXAMPLE_PIN_NUM_BK_LIGHT >= 0
  343. #if CONFIG_EXAMPLE_LCD_IMAGE_FROM_FILE_SYSTEM
  344. example_init_filesystem();
  345. #endif // CONFIG_EXAMPLE_LCD_IMAGE_FROM_FILE_SYSTEM
  346. esp_lcd_panel_io_handle_t io_handle = NULL;
  347. example_init_i80_bus(&io_handle, &disp_drv);
  348. esp_lcd_panel_handle_t panel_handle = NULL;
  349. example_init_lcd_panel(io_handle, &panel_handle);
  350. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  351. esp_lcd_touch_handle_t tp_handle = NULL;
  352. example_init_lcd_touch(&tp_handle);
  353. #endif // CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  354. // Stub: user can flush pre-defined pattern to the screen before we turn on the screen or backlight
  355. ESP_ERROR_CHECK(esp_lcd_panel_disp_on_off(panel_handle, true));
  356. #if EXAMPLE_PIN_NUM_BK_LIGHT >= 0
  357. ESP_LOGI(TAG, "Turn on LCD backlight");
  358. gpio_set_level(EXAMPLE_PIN_NUM_BK_LIGHT, EXAMPLE_LCD_BK_LIGHT_ON_LEVEL);
  359. #endif // EXAMPLE_PIN_NUM_BK_LIGHT >= 0
  360. ESP_LOGI(TAG, "Initialize LVGL library");
  361. lv_init();
  362. // alloc draw buffers used by LVGL
  363. // it's recommended to choose the size of the draw buffer(s) to be at least 1/10 screen sized
  364. lv_color_t *buf1 = NULL;
  365. lv_color_t *buf2 = NULL;
  366. #if CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  367. buf1 = heap_caps_aligned_alloc(EXAMPLE_PSRAM_DATA_ALIGNMENT, EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  368. buf2 = heap_caps_aligned_alloc(EXAMPLE_PSRAM_DATA_ALIGNMENT, EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  369. #else
  370. buf1 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  371. buf2 = heap_caps_malloc(EXAMPLE_LCD_H_RES * 100 * sizeof(lv_color_t), MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  372. #endif // CONFIG_EXAMPLE_LCD_I80_COLOR_IN_PSRAM
  373. assert(buf1);
  374. assert(buf2);
  375. ESP_LOGI(TAG, "buf1@%p, buf2@%p", buf1, buf2);
  376. // initialize LVGL draw buffers
  377. lv_disp_draw_buf_init(&disp_buf, buf1, buf2, EXAMPLE_LCD_H_RES * 100);
  378. ESP_LOGI(TAG, "Register display driver to LVGL");
  379. lv_disp_drv_init(&disp_drv);
  380. disp_drv.hor_res = EXAMPLE_LCD_H_RES;
  381. disp_drv.ver_res = EXAMPLE_LCD_V_RES;
  382. disp_drv.flush_cb = example_lvgl_flush_cb;
  383. disp_drv.draw_buf = &disp_buf;
  384. disp_drv.user_data = panel_handle;
  385. lv_disp_t *disp = lv_disp_drv_register(&disp_drv);
  386. ESP_LOGI(TAG, "Install LVGL tick timer");
  387. // Tick interface for LVGL (using esp_timer to generate 2ms periodic event)
  388. const esp_timer_create_args_t lvgl_tick_timer_args = {
  389. .callback = &example_increase_lvgl_tick,
  390. .name = "lvgl_tick"
  391. };
  392. esp_timer_handle_t lvgl_tick_timer = NULL;
  393. ESP_ERROR_CHECK(esp_timer_create(&lvgl_tick_timer_args, &lvgl_tick_timer));
  394. ESP_ERROR_CHECK(esp_timer_start_periodic(lvgl_tick_timer, EXAMPLE_LVGL_TICK_PERIOD_MS * 1000));
  395. #if CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  396. static lv_indev_drv_t indev_drv; // Input device driver (Touch)
  397. lv_indev_drv_init(&indev_drv);
  398. indev_drv.type = LV_INDEV_TYPE_POINTER;
  399. indev_drv.disp = disp;
  400. indev_drv.read_cb = example_lvgl_touch_cb;
  401. indev_drv.user_data = tp_handle;
  402. lv_indev_drv_register(&indev_drv);
  403. #endif // CONFIG_EXAMPLE_LCD_TOUCH_ENABLED
  404. ESP_LOGI(TAG, "Display LVGL animation");
  405. example_lvgl_demo_ui(disp);
  406. while (1) {
  407. // raise the task priority of LVGL and/or reduce the handler period can improve the performance
  408. vTaskDelay(pdMS_TO_TICKS(10));
  409. // The task running lv_timer_handler should have lower priority than that running `lv_tick_inc`
  410. lv_timer_handler();
  411. }
  412. }