lv_port_disp.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. static void color_to16_maybe(lv_color16_t *dst, lv_color_t *src)
  18. {
  19. #if (LV_COLOR_DEPTH == 16)
  20. dst->full = src->full;
  21. #else
  22. dst->ch.blue = src->ch.blue;
  23. dst->ch.green = src->ch.green;
  24. dst->ch.red = src->ch.red;
  25. #endif
  26. }
  27. /*Flush the content of the internal buffer the specific area on the display
  28. *You can use DMA or any hardware acceleration to do this operation in the background but
  29. *'lv_disp_flush_ready()' has to be called when finished.*/
  30. static void lcd_fb_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
  31. {
  32. int x1, x2, y1, y2;
  33. x1 = area->x1;
  34. x2 = area->x2;
  35. y1 = area->y1;
  36. y2 = area->y2;
  37. /*Return if the area is out the screen*/
  38. if (x2 < 0)
  39. return;
  40. if (y2 < 0)
  41. return;
  42. if (x1 > info.width - 1)
  43. return;
  44. if (y1 > info.height - 1)
  45. return;
  46. /*Truncate the area to the screen*/
  47. int32_t act_x1 = x1 < 0 ? 0 : x1;
  48. int32_t act_y1 = y1 < 0 ? 0 : y1;
  49. int32_t act_x2 = x2 > info.width - 1 ? info.width - 1 : x2;
  50. int32_t act_y2 = y2 > info.height - 1 ? info.height - 1 : y2;
  51. uint32_t x;
  52. uint32_t y;
  53. long int location = 0;
  54. /* 16 bit per pixel */
  55. lv_color16_t *fbp16 = (lv_color16_t *)info.framebuffer;
  56. for (y = act_y1; y <= act_y2; y++)
  57. {
  58. for (x = act_x1; x <= act_x2; x++)
  59. {
  60. location = (x) + (y)*info.width;
  61. color_to16_maybe(&fbp16[location], color_p);
  62. color_p++;
  63. }
  64. color_p += x2 - act_x2;
  65. }
  66. struct rt_device_rect_info rect_info;
  67. rect_info.x = x1;
  68. rect_info.y = y1;
  69. rect_info.width = x2 - x1 + 1;
  70. rect_info.height = y2 - y1 + 1;
  71. rt_device_control(lcd_device, RTGRAPHIC_CTRL_RECT_UPDATE, &rect_info);
  72. lv_disp_flush_ready(disp_drv);
  73. }
  74. void lv_port_disp_init(void)
  75. {
  76. rt_err_t result;
  77. lv_color_t *fbuf1, *fbuf2;
  78. lcd_device = rt_device_find("lcd");
  79. if (lcd_device == 0)
  80. {
  81. rt_kprintf("error!\n");
  82. return;
  83. }
  84. result = rt_device_open(lcd_device, 0);
  85. if (result != RT_EOK)
  86. {
  87. rt_kprintf("error!\n");
  88. return;
  89. }
  90. /* get framebuffer address */
  91. result = rt_device_control(lcd_device, RTGRAPHIC_CTRL_GET_INFO, &info);
  92. if (result != RT_EOK)
  93. {
  94. rt_kprintf("error!\n");
  95. /* get device information failed */
  96. return;
  97. }
  98. RT_ASSERT(info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
  99. info.bits_per_pixel == 24 || info.bits_per_pixel == 32);
  100. fbuf1 = rt_malloc(info.width * info.height * sizeof(lv_color_t));
  101. if (fbuf1 == RT_NULL)
  102. {
  103. rt_kprintf("Error: alloc disp buf fail\n");
  104. return;
  105. }
  106. fbuf2 = rt_malloc(info.width * info.height * sizeof(lv_color_t));
  107. if (fbuf2 == RT_NULL)
  108. {
  109. rt_kprintf("Error: alloc disp buf fail\n");
  110. rt_free(fbuf1);
  111. return;
  112. }
  113. /*Initialize `disp_buf` with the buffer(s). With only one buffer use NULL instead buf_2 */
  114. lv_disp_draw_buf_init(&disp_buf, fbuf1, fbuf2, info.width * info.height);
  115. lv_disp_drv_init(&disp_drv); /*Basic initialization*/
  116. /*Set the resolution of the display*/
  117. disp_drv.hor_res = info.width;
  118. disp_drv.ver_res = info.height;
  119. /*Set a display buffer*/
  120. disp_drv.draw_buf = &disp_buf;
  121. /*Used to copy the buffer's content to the display*/
  122. disp_drv.flush_cb = lcd_fb_flush;
  123. /*Finally register the driver*/
  124. lv_disp_drv_register(&disp_drv);
  125. }