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