Example of using the LVGL (8.3.4) with the MbedOS (6.16 - bare metal) on the Disco-F746NG board
Dependencies: BSP_DISCO_F746NG
main.cpp@6:778736ecceb0, 15 months ago (annotated)
- Committer:
- JohnnyK
- Date:
- Sat Feb 04 21:01:46 2023 +0000
- Revision:
- 6:778736ecceb0
- Parent:
- 5:071136c3eefa
Update MbedOS to 6.16, update LVGL to 8.3.4 and update Notebook URL
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JohnnyK | 5:071136c3eefa | 1 | /** |
JohnnyK | 6:778736ecceb0 | 2 | * Notebook page https://os.mbed.com/users/JohnnyK/notebook/how-start-with-the-LVGL/ |
JohnnyK | 5:071136c3eefa | 3 | */ |
JohnnyK | 5:071136c3eefa | 4 | |
JohnnyK | 0:10c4b83c458d | 5 | #include "mbed.h" |
JohnnyK | 6:778736ecceb0 | 6 | #include "lvgl.h" |
JohnnyK | 6:778736ecceb0 | 7 | #include "demos/lv_demos.h" /*Comment/uncomment will switch between LVGL demos and Hello word example*/ |
JohnnyK | 3:4f5dc253eb7b | 8 | #include "hal_stm_lvgl/tft/tft.h" |
JohnnyK | 3:4f5dc253eb7b | 9 | #include "hal_stm_lvgl/touchpad/touchpad.h" |
JohnnyK | 0:10c4b83c458d | 10 | |
JohnnyK | 3:4f5dc253eb7b | 11 | #define LVGL_TICK 10 //Time tick value for lvgl in ms (1-10msa) |
JohnnyK | 3:4f5dc253eb7b | 12 | #define TICKER_TIME 10ms //modified to miliseconds |
JohnnyK | 0:10c4b83c458d | 13 | |
JohnnyK | 3:4f5dc253eb7b | 14 | Ticker ticker; //Ticker for lvgl |
JohnnyK | 3:4f5dc253eb7b | 15 | |
JohnnyK | 5:071136c3eefa | 16 | /* |
JohnnyK | 5:071136c3eefa | 17 | * Callback function for lvgl timing. |
JohnnyK | 5:071136c3eefa | 18 | * Call lv_tick_inc(x) every x milliseconds in a Timer or Task (x should be between 1 and 10). |
JohnnyK | 5:071136c3eefa | 19 | * It is required for the internal timing of LittlevGL. |
JohnnyK | 5:071136c3eefa | 20 | */ |
JohnnyK | 0:10c4b83c458d | 21 | void lv_ticker_func(){ |
JohnnyK | 0:10c4b83c458d | 22 | lv_tick_inc(LVGL_TICK); |
JohnnyK | 0:10c4b83c458d | 23 | } |
JohnnyK | 0:10c4b83c458d | 24 | |
JohnnyK | 6:778736ecceb0 | 25 | #ifndef LV_DEMOS_H |
JohnnyK | 5:071136c3eefa | 26 | static void event_handler(lv_event_t* event) |
JohnnyK | 3:4f5dc253eb7b | 27 | { |
JohnnyK | 5:071136c3eefa | 28 | lv_event_code_t code = lv_event_get_code(event); |
JohnnyK | 5:071136c3eefa | 29 | if(code == LV_EVENT_CLICKED) { |
JohnnyK | 3:4f5dc253eb7b | 30 | printf("Clicked\n"); |
JohnnyK | 3:4f5dc253eb7b | 31 | } |
JohnnyK | 5:071136c3eefa | 32 | else if(code == LV_EVENT_VALUE_CHANGED) { |
JohnnyK | 3:4f5dc253eb7b | 33 | printf("Toggled\n"); |
JohnnyK | 0:10c4b83c458d | 34 | } |
JohnnyK | 1:627f26953c53 | 35 | } |
JohnnyK | 3:4f5dc253eb7b | 36 | #endif |
JohnnyK | 3:4f5dc253eb7b | 37 | // main() runs in its own thread in the OS |
JohnnyK | 0:10c4b83c458d | 38 | int main() |
JohnnyK | 0:10c4b83c458d | 39 | { |
JohnnyK | 3:4f5dc253eb7b | 40 | printf("LVGL-"); |
JohnnyK | 3:4f5dc253eb7b | 41 | lv_init(); //Initialize the LVGL |
JohnnyK | 3:4f5dc253eb7b | 42 | tft_init(); //Initialize diplay |
JohnnyK | 3:4f5dc253eb7b | 43 | touchpad_init(); //Initialize touchpad |
JohnnyK | 3:4f5dc253eb7b | 44 | ticker.attach(callback(&lv_ticker_func),TICKER_TIME); //Attach callback to ticker |
JohnnyK | 3:4f5dc253eb7b | 45 | |
JohnnyK | 6:778736ecceb0 | 46 | #ifdef LV_DEMOS_H |
JohnnyK | 3:4f5dc253eb7b | 47 | printf("Demo\n"); |
JohnnyK | 3:4f5dc253eb7b | 48 | lv_demo_widgets(); |
JohnnyK | 3:4f5dc253eb7b | 49 | #else |
JohnnyK | 3:4f5dc253eb7b | 50 | printf("Hello world\n"); |
JohnnyK | 5:071136c3eefa | 51 | lv_obj_t * label1 = lv_label_create(lv_scr_act()); |
JohnnyK | 5:071136c3eefa | 52 | lv_label_set_long_mode(label1, LV_LABEL_LONG_WRAP); /*Break the long lines*/ |
JohnnyK | 3:4f5dc253eb7b | 53 | lv_label_set_recolor(label1, true); /*Enable re-coloring by commands in the text*/ |
JohnnyK | 3:4f5dc253eb7b | 54 | lv_label_set_text(label1, "#0000ff Hello# #ff00ff world# #ff0000 - the LVGL and MbedOS#"); |
JohnnyK | 3:4f5dc253eb7b | 55 | lv_obj_set_width(label1, 150); |
JohnnyK | 5:071136c3eefa | 56 | lv_obj_align(label1, LV_ALIGN_TOP_MID, 0, 20); |
JohnnyK | 3:4f5dc253eb7b | 57 | |
JohnnyK | 5:071136c3eefa | 58 | lv_obj_t * label2 = lv_label_create(lv_scr_act()); |
JohnnyK | 5:071136c3eefa | 59 | lv_label_set_long_mode(label2, LV_LABEL_LONG_SCROLL_CIRCULAR); /*Circular scroll*/ |
JohnnyK | 3:4f5dc253eb7b | 60 | lv_obj_set_width(label2, 150); |
JohnnyK | 3:4f5dc253eb7b | 61 | lv_label_set_text(label2, "It is a circularly scrolling text. "); |
JohnnyK | 5:071136c3eefa | 62 | lv_obj_align(label2, LV_ALIGN_BOTTOM_MID, 0, 0); |
JohnnyK | 0:10c4b83c458d | 63 | |
JohnnyK | 0:10c4b83c458d | 64 | lv_obj_t * label; |
JohnnyK | 5:071136c3eefa | 65 | lv_obj_t * btn1 = lv_btn_create(lv_scr_act()); |
JohnnyK | 5:071136c3eefa | 66 | lv_obj_add_event_cb(btn1, event_handler, LV_EVENT_ALL, NULL); |
JohnnyK | 5:071136c3eefa | 67 | lv_obj_align(btn1, LV_ALIGN_CENTER, 0, -25); |
JohnnyK | 5:071136c3eefa | 68 | label = lv_label_create(btn1); |
JohnnyK | 0:10c4b83c458d | 69 | lv_label_set_text(label, "Button"); |
JohnnyK | 5:071136c3eefa | 70 | lv_obj_t * btn2 = lv_btn_create(lv_scr_act()); |
JohnnyK | 5:071136c3eefa | 71 | lv_obj_add_event_cb(btn2, event_handler,LV_EVENT_ALL, NULL); |
JohnnyK | 5:071136c3eefa | 72 | lv_obj_align(btn2, LV_ALIGN_CENTER, 0, 25); |
JohnnyK | 5:071136c3eefa | 73 | lv_obj_add_flag(btn2, LV_OBJ_FLAG_CHECKABLE); |
JohnnyK | 5:071136c3eefa | 74 | label = lv_label_create(btn2); |
JohnnyK | 0:10c4b83c458d | 75 | lv_label_set_text(label, "Toggled"); |
JohnnyK | 3:4f5dc253eb7b | 76 | #endif |
JohnnyK | 0:10c4b83c458d | 77 | |
JohnnyK | 3:4f5dc253eb7b | 78 | while (true){ |
JohnnyK | 3:4f5dc253eb7b | 79 | lv_task_handler(); |
JohnnyK | 3:4f5dc253eb7b | 80 | //Call lv_task_handler() periodically every few milliseconds. |
JohnnyK | 3:4f5dc253eb7b | 81 | //It will redraw the screen if required, handle input devices etc. |
JohnnyK | 3:4f5dc253eb7b | 82 | thread_sleep_for(LVGL_TICK); |
JohnnyK | 0:10c4b83c458d | 83 | } |
JohnnyK | 3:4f5dc253eb7b | 84 | } |