lv_port_disp.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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-10-18 Meco Man The first version
  9. */
  10. #include <lvgl.h>
  11. #include <drv_clcd.h>
  12. /*A static or global variable to store the buffers*/
  13. static lv_disp_draw_buf_t disp_buf;
  14. static rt_device_t lcd_device = 0;
  15. static struct rt_device_graphic_info info;
  16. static lv_disp_drv_t disp_drv; /*Descriptor of a display driver*/
  17. /*Flush the content of the internal buffer the specific area on the display
  18. *You can use DMA or any hardware acceleration to do this operation in the background but
  19. *'lv_disp_flush_ready()' has to be called when finished.*/
  20. static void lcd_fb_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
  21. {
  22. uint32_t x;
  23. uint32_t y;
  24. uint32_t location = 0;
  25. /* 16 bit per pixel */
  26. lv_color16_t *fbp16 = (lv_color16_t *)info.framebuffer;
  27. for (y = area->y1; y <area->y2 + 1; y++)
  28. {
  29. for (x = area->x1; x <area->x2 + 1; x++)
  30. {
  31. location = x + y * info.width;
  32. fbp16[location].full = color_p->full;
  33. color_p++;
  34. }
  35. }
  36. lv_disp_flush_ready(disp_drv);
  37. }
  38. void lv_port_disp_init(void)
  39. {
  40. rt_err_t result;
  41. lv_color_t *fbuf1, *fbuf2;
  42. lcd_device = rt_device_find("lcd");
  43. if (lcd_device == 0)
  44. {
  45. rt_kprintf("error!\n");
  46. return;
  47. }
  48. result = rt_device_open(lcd_device, 0);
  49. if (result != RT_EOK)
  50. {
  51. rt_kprintf("error!\n");
  52. return;
  53. }
  54. /* get framebuffer address */
  55. result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
  56. if (result != RT_EOK)
  57. {
  58. rt_kprintf("error!\n");
  59. /* get device information failed */
  60. return;
  61. }
  62. RT_ASSERT(info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
  63. info.bits_per_pixel == 24 || info.bits_per_pixel == 32);
  64. fbuf1 = rt_malloc(info.width * info.height * sizeof(lv_color_t));
  65. if (fbuf1 == RT_NULL)
  66. {
  67. rt_kprintf("Error: alloc disp buf fail\n");
  68. return;
  69. }
  70. fbuf2 = rt_malloc(info.width * info.height * sizeof(lv_color_t));
  71. if (fbuf2 == RT_NULL)
  72. {
  73. rt_kprintf("Error: alloc disp buf fail\n");
  74. rt_free(fbuf1);
  75. return;
  76. }
  77. /*Initialize `disp_buf` with the buffer(s). With only one buffer use NULL instead buf_2 */
  78. lv_disp_draw_buf_init(&disp_buf, fbuf1, fbuf2, info.width * info.height);
  79. lv_disp_drv_init(&disp_drv); /*Basic initialization*/
  80. /*Set the resolution of the display*/
  81. disp_drv.hor_res = info.width;
  82. disp_drv.ver_res = info.height;
  83. /*Set a display buffer*/
  84. disp_drv.draw_buf = &disp_buf;
  85. /*Used to copy the buffer's content to the display*/
  86. disp_drv.flush_cb = lcd_fb_flush;
  87. /*Finally register the driver*/
  88. lv_disp_drv_register(&disp_drv);
  89. }