lv_port_disp.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2006-2022, 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. #if !defined(NU_PKG_LVGL_RENDERING_LAYER)
  18. #define NU_PKG_LVGL_RENDERING_LAYER "lcd"
  19. #endif
  20. /*A static or global variable to store the buffers*/
  21. static lv_disp_draw_buf_t disp_buf;
  22. static lv_disp_drv_t disp_drv; /*Descriptor of a display driver*/
  23. static rt_device_t lcd_device = 0;
  24. static struct rt_device_graphic_info info;
  25. static void lcd_fb_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
  26. {
  27. /* Rendering */
  28. struct rt_device_rect_info rect;
  29. rect.x = area->x1;
  30. rect.y = area->y1;
  31. rect.width = area->x2 - area->x1 + 1;
  32. rect.height = area->y2 - area->y1 + 1;
  33. rt_device_control(lcd_device, RTGRAPHIC_CTRL_RECT_UPDATE, &rect);
  34. lv_disp_flush_ready(disp_drv);
  35. }
  36. void lcd_perf_monitor(struct _lv_disp_drv_t *disp_drv, uint32_t time, uint32_t px)
  37. {
  38. rt_kprintf("Elapsed: %dms, Pixel: %d, Bytes:%d\n", time, px, px * sizeof(lv_color_t));
  39. }
  40. void lv_port_disp_init(void)
  41. {
  42. rt_err_t result;
  43. void *buf1 = RT_NULL;
  44. lcd_device = rt_device_find(NU_PKG_LVGL_RENDERING_LAYER);
  45. if (lcd_device == 0)
  46. {
  47. LOG_E("error!");
  48. return;
  49. }
  50. /* get framebuffer address */
  51. result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
  52. if (result != RT_EOK && info.framebuffer == RT_NULL)
  53. {
  54. LOG_E("error!");
  55. /* get device information failed */
  56. return;
  57. }
  58. RT_ASSERT(info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
  59. info.bits_per_pixel == 24 || info.bits_per_pixel == 32);
  60. buf1 = (void *)info.framebuffer;
  61. rt_kprintf("LVGL: Use one buffers - buf1@%08x, size: %d bytes\n", buf1, info.smem_len);
  62. /*Initialize `disp_buf` with the buffer(s).*/
  63. lv_disp_draw_buf_init(&disp_buf, buf1, RT_NULL, info.smem_len / (info.bits_per_pixel / 8));
  64. result = rt_device_open(lcd_device, 0);
  65. if (result != RT_EOK)
  66. {
  67. LOG_E("error!");
  68. return;
  69. }
  70. lv_disp_drv_init(&disp_drv); /*Basic initialization*/
  71. /*Set the resolution of the display*/
  72. disp_drv.hor_res = info.width;
  73. disp_drv.ver_res = info.height;
  74. /*Set a display buffer*/
  75. disp_drv.draw_buf = &disp_buf;
  76. /*Write the internal buffer (draw_buf) to the display*/
  77. disp_drv.flush_cb = lcd_fb_flush;
  78. /* Called after every refresh cycle to tell the rendering and flushing time + the number of flushed pixels */
  79. //disp_drv.monitor_cb = lcd_perf_monitor;
  80. /*Finally register the driver*/
  81. lv_disp_drv_register(&disp_drv);
  82. }