lvgl_demo.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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-17 Meco Man First version
  9. */
  10. #include <rtthread.h>
  11. #include <lvgl.h>
  12. #include <drv_lcd.h>
  13. #define DBG_TAG "LVGL"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. #ifndef LV_THREAD_STACK_SIZE
  17. #define LV_THREAD_STACK_SIZE 2048
  18. #endif
  19. #ifndef LV_THREAD_PRIO
  20. #define LV_THREAD_PRIO (RT_THREAD_PRIORITY_MAX*2/3)
  21. #endif
  22. static void lv_example_get_started_1(void);
  23. static void lv_example_get_started_3(void);
  24. #define MY_DISP_HOR_RES 240 /* 240*240 */
  25. /*A static or global variable to store the buffers*/
  26. static lv_disp_draw_buf_t disp_buf;
  27. /*Static or global buffer(s). The second buffer is optional*/
  28. static lv_color_t buf_1[MY_DISP_HOR_RES * 10];
  29. static lv_color_t buf_2[MY_DISP_HOR_RES * 10];
  30. static lv_disp_drv_t disp_drv; /*Descriptor of a display driver*/
  31. /*Flush the content of the internal buffer the specific area on the display
  32. *You can use DMA or any hardware acceleration to do this operation in the background but
  33. *'lv_disp_flush_ready()' has to be called when finished.*/
  34. static void disp_flush(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p)
  35. {
  36. lcd_fill_array(area->x1, area->y1, area->x2, area->y2, color_p);
  37. /*IMPORTANT!!!
  38. *Inform the graphics library that you are ready with the flushing*/
  39. lv_disp_flush_ready(disp_drv);
  40. }
  41. static void lvgl_thread(void *parameter)
  42. {
  43. lv_init();
  44. /*Initialize `disp_buf` with the buffer(s). With only one buffer use NULL instead buf_2 */
  45. lv_disp_draw_buf_init(&disp_buf, buf_1, buf_2, MY_DISP_HOR_RES*10);
  46. lv_disp_drv_init(&disp_drv); /*Basic initialization*/
  47. /*Set the resolution of the display*/
  48. disp_drv.hor_res = MY_DISP_HOR_RES;
  49. disp_drv.ver_res = MY_DISP_HOR_RES;
  50. /*Set a display buffer*/
  51. disp_drv.draw_buf = &disp_buf;
  52. /*Used to copy the buffer's content to the display*/
  53. disp_drv.flush_cb = disp_flush;
  54. /*Finally register the driver*/
  55. lv_disp_drv_register(&disp_drv);
  56. lv_example_get_started_1();
  57. lv_example_get_started_3();
  58. while(1)
  59. {
  60. lv_task_handler();
  61. rt_thread_mdelay(10);
  62. }
  63. }
  64. static int lvgl_demo_init(void)
  65. {
  66. rt_thread_t tid;
  67. tid = rt_thread_create("lvgl", lvgl_thread, RT_NULL, LV_THREAD_STACK_SIZE, LV_THREAD_PRIO, 0);
  68. rt_thread_startup(tid);
  69. return 0;
  70. }
  71. INIT_COMPONENT_EXPORT(lvgl_demo_init);
  72. /* ------------------- demo1 ----------------------- */
  73. static void btn_event_cb(lv_event_t * e)
  74. {
  75. lv_event_code_t code = lv_event_get_code(e);
  76. lv_obj_t * btn = lv_event_get_target(e);
  77. if(code == LV_EVENT_CLICKED) {
  78. static uint8_t cnt = 0;
  79. cnt++;
  80. /*Get the first child of the button which is the label and change its text*/
  81. lv_obj_t * label = lv_obj_get_child(btn, 0);
  82. lv_label_set_text_fmt(label, "Button: %d", cnt);
  83. }
  84. }
  85. /**
  86. * Create a button with a label and react on click event.
  87. */
  88. static void lv_example_get_started_1(void)
  89. {
  90. lv_obj_t * btn = lv_btn_create(lv_scr_act()); /*Add a button the current screen*/
  91. lv_obj_set_pos(btn, 10, 10); /*Set its position*/
  92. lv_obj_set_size(btn, 120, 50); /*Set its size*/
  93. lv_obj_add_event_cb(btn, btn_event_cb, LV_EVENT_ALL, NULL); /*Assign a callback to the button*/
  94. lv_obj_t * label = lv_label_create(btn); /*Add a label to the button*/
  95. lv_label_set_text(label, "Button"); /*Set the labels text*/
  96. lv_obj_center(label);
  97. }
  98. /* ------------------- demo3 ----------------------- */
  99. static lv_obj_t * label;
  100. static void slider_event_cb(lv_event_t * e)
  101. {
  102. lv_obj_t * slider = lv_event_get_target(e);
  103. /*Refresh the text*/
  104. lv_label_set_text_fmt(label, "%d", (int)lv_slider_get_value(slider));
  105. lv_obj_align_to(label, slider, LV_ALIGN_OUT_TOP_MID, 0, -15); /*Align top of the slider*/
  106. }
  107. /**
  108. * Create a slider and write its value on a label.
  109. */
  110. static void lv_example_get_started_3(void)
  111. {
  112. /*Create a slider in the center of the display*/
  113. lv_obj_t * slider = lv_slider_create(lv_scr_act());
  114. lv_obj_set_width(slider, 200); /*Set the width*/
  115. lv_obj_center(slider); /*Align to the center of the parent (screen)*/
  116. lv_obj_add_event_cb(slider, slider_event_cb, LV_EVENT_VALUE_CHANGED, NULL); /*Assign an event function*/
  117. /*Create a label below the slider*/
  118. label = lv_label_create(lv_scr_act());
  119. lv_label_set_text(label, "0");
  120. lv_obj_align_to(label, slider, LV_ALIGN_OUT_TOP_MID, 0, -15); /*Align top of the slider*/
  121. }