lv_port_disp.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-12-17 Wayne The first version
  9. */
  10. #include <lvgl.h>
  11. #define LOG_TAG "lvgl.disp"
  12. #define DBG_ENABLE
  13. #define DBG_SECTION_NAME LOG_TAG
  14. #define DBG_LEVEL DBG_ERROR
  15. #define DBG_COLOR
  16. #include <rtdbg.h>
  17. /*A static or global variable to store the buffers*/
  18. static lv_disp_draw_buf_t disp_buf;
  19. static lv_disp_drv_t disp_drv; /*Descriptor of a display driver*/
  20. static rt_device_t lcd_device = 0;
  21. static struct rt_device_graphic_info info;
  22. static void lcd_fb_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
  23. {
  24. /* Rendering */
  25. struct rt_device_rect_info rect;
  26. rect.x = area->x1;
  27. rect.y = area->y1;
  28. rect.width = area->x2 - area->x1 + 1;
  29. rect.height = area->y2 - area->y1 + 1;
  30. rt_device_control(lcd_device, RTGRAPHIC_CTRL_RECT_UPDATE, &rect);
  31. lv_disp_flush_ready(disp_drv);
  32. }
  33. void lcd_perf_monitor(struct _lv_disp_drv_t *disp_drv, uint32_t time, uint32_t px)
  34. {
  35. rt_kprintf("Elapsed: %dms, Pixel: %d, Bytes:%d\n", time, px, px * sizeof(lv_color_t));
  36. }
  37. void lv_port_disp_init(void)
  38. {
  39. rt_err_t result;
  40. void *buf1 = RT_NULL;
  41. lcd_device = rt_device_find("lcd");
  42. if (lcd_device == 0)
  43. {
  44. LOG_E("error!");
  45. return;
  46. }
  47. /* get framebuffer address */
  48. result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
  49. if (result != RT_EOK && info.framebuffer == RT_NULL)
  50. {
  51. LOG_E("error!");
  52. /* get device information failed */
  53. return;
  54. }
  55. RT_ASSERT(info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
  56. info.bits_per_pixel == 24 || info.bits_per_pixel == 32);
  57. buf1 = (void *)info.framebuffer;
  58. rt_kprintf("LVGL: Use one buffers - buf1@%08x, size: %d bytes\n", buf1, info.smem_len);
  59. /*Initialize `disp_buf` with the buffer(s).*/
  60. lv_disp_draw_buf_init(&disp_buf, buf1, RT_NULL, info.smem_len / (info.bits_per_pixel / 8));
  61. result = rt_device_open(lcd_device, 0);
  62. if (result != RT_EOK)
  63. {
  64. LOG_E("error!");
  65. return;
  66. }
  67. lv_disp_drv_init(&disp_drv); /*Basic initialization*/
  68. /*Set the resolution of the display*/
  69. disp_drv.hor_res = info.width;
  70. disp_drv.ver_res = info.height;
  71. /*Set a display buffer*/
  72. disp_drv.draw_buf = &disp_buf;
  73. /*Write the internal buffer (draw_buf) to the display*/
  74. disp_drv.flush_cb = lcd_fb_flush;
  75. /* Called after every refresh cycle to tell the rendering and flushing time + the number of flushed pixels */
  76. //disp_drv.monitor_cb = lcd_perf_monitor;
  77. /*Finally register the driver*/
  78. lv_disp_drv_register(&disp_drv);
  79. }