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@1:627f26953c53, 2020-04-07 (annotated)
- Committer:
- JohnnyK
- Date:
- Tue Apr 07 08:03:55 2020 +0000
- Revision:
- 1:627f26953c53
- Parent:
- 0:10c4b83c458d
- Child:
- 2:afc050526249
Test on the latest MbedOS (5.15.1) and also a change of event_handler (according to the LvGL doc) for better demonstration what can be read from button event.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JohnnyK | 0:10c4b83c458d | 1 | //#pragma -O0 |
JohnnyK | 0:10c4b83c458d | 2 | #include "mbed.h" |
JohnnyK | 0:10c4b83c458d | 3 | #include "lvgl/lvgl.h" |
JohnnyK | 0:10c4b83c458d | 4 | #include "lv_port_disp.h" |
JohnnyK | 0:10c4b83c458d | 5 | #include "lv_port_indev.h" |
JohnnyK | 1:627f26953c53 | 6 | //#include "demo.h" //Comment/uncomment will switch between LittlevGL demo and Hello word example |
JohnnyK | 0:10c4b83c458d | 7 | |
JohnnyK | 0:10c4b83c458d | 8 | #define LVGL_TICK 5 //Time tick value for lvgl in ms (1-10msa) |
JohnnyK | 0:10c4b83c458d | 9 | #define TICKER_TIME 0.001 * LVGL_TICK //modified to miliseconds |
JohnnyK | 0:10c4b83c458d | 10 | #define WAIT_TIME 1000 * LVGL_TICK //modified to microseconds |
JohnnyK | 0:10c4b83c458d | 11 | |
JohnnyK | 0:10c4b83c458d | 12 | Ticker ticker; //Ticker for lvgl |
JohnnyK | 0:10c4b83c458d | 13 | |
JohnnyK | 0:10c4b83c458d | 14 | //Callback function for lvgl timing. |
JohnnyK | 0:10c4b83c458d | 15 | void lv_ticker_func(){ |
JohnnyK | 0:10c4b83c458d | 16 | |
JohnnyK | 0:10c4b83c458d | 17 | lv_tick_inc(LVGL_TICK); |
JohnnyK | 0:10c4b83c458d | 18 | //Call lv_tick_inc(x) every x milliseconds in a Timer or Task (x should be between 1 and 10). |
JohnnyK | 0:10c4b83c458d | 19 | //It is required for the internal timing of LittlevGL. |
JohnnyK | 0:10c4b83c458d | 20 | lv_task_handler(); |
JohnnyK | 0:10c4b83c458d | 21 | //Call lv_task_handler() periodically every few milliseconds. |
JohnnyK | 0:10c4b83c458d | 22 | //It will redraw the screen if required, handle input devices etc. |
JohnnyK | 0:10c4b83c458d | 23 | } |
JohnnyK | 0:10c4b83c458d | 24 | |
JohnnyK | 1:627f26953c53 | 25 | #ifndef DEMO_H |
JohnnyK | 1:627f26953c53 | 26 | //lvgl buttons object event handler |
JohnnyK | 1:627f26953c53 | 27 | static void event_handler(lv_obj_t * obj, lv_event_t event){ |
JohnnyK | 1:627f26953c53 | 28 | switch(event) { |
JohnnyK | 1:627f26953c53 | 29 | case LV_EVENT_PRESSED: |
JohnnyK | 1:627f26953c53 | 30 | printf("Pressed\n"); |
JohnnyK | 1:627f26953c53 | 31 | break; |
JohnnyK | 1:627f26953c53 | 32 | |
JohnnyK | 1:627f26953c53 | 33 | case LV_EVENT_SHORT_CLICKED: |
JohnnyK | 1:627f26953c53 | 34 | printf("Short clicked\n"); |
JohnnyK | 1:627f26953c53 | 35 | break; |
JohnnyK | 1:627f26953c53 | 36 | |
JohnnyK | 1:627f26953c53 | 37 | case LV_EVENT_CLICKED: |
JohnnyK | 1:627f26953c53 | 38 | printf("Clicked\n"); |
JohnnyK | 1:627f26953c53 | 39 | break; |
JohnnyK | 1:627f26953c53 | 40 | |
JohnnyK | 1:627f26953c53 | 41 | case LV_EVENT_VALUE_CHANGED: |
JohnnyK | 1:627f26953c53 | 42 | printf("Toggled\n"); |
JohnnyK | 1:627f26953c53 | 43 | break; |
JohnnyK | 1:627f26953c53 | 44 | |
JohnnyK | 1:627f26953c53 | 45 | case LV_EVENT_LONG_PRESSED: |
JohnnyK | 1:627f26953c53 | 46 | printf("Long press\n"); |
JohnnyK | 1:627f26953c53 | 47 | break; |
JohnnyK | 1:627f26953c53 | 48 | |
JohnnyK | 1:627f26953c53 | 49 | case LV_EVENT_LONG_PRESSED_REPEAT: |
JohnnyK | 1:627f26953c53 | 50 | printf("Long press repeat\n"); |
JohnnyK | 1:627f26953c53 | 51 | break; |
JohnnyK | 1:627f26953c53 | 52 | |
JohnnyK | 1:627f26953c53 | 53 | case LV_EVENT_RELEASED: |
JohnnyK | 1:627f26953c53 | 54 | printf("Released\n"); |
JohnnyK | 1:627f26953c53 | 55 | break; |
JohnnyK | 0:10c4b83c458d | 56 | } |
JohnnyK | 1:627f26953c53 | 57 | } |
JohnnyK | 1:627f26953c53 | 58 | #endif |
JohnnyK | 0:10c4b83c458d | 59 | |
JohnnyK | 0:10c4b83c458d | 60 | int main() |
JohnnyK | 0:10c4b83c458d | 61 | { |
JohnnyK | 0:10c4b83c458d | 62 | printf("LittlevGL GUI-"); |
JohnnyK | 0:10c4b83c458d | 63 | lv_init(); //Initialize the LittlevGL |
JohnnyK | 0:10c4b83c458d | 64 | lv_port_disp_init(); //Initialize diplay |
JohnnyK | 0:10c4b83c458d | 65 | lv_port_indev_init(); //Initialize touchpad |
JohnnyK | 0:10c4b83c458d | 66 | ticker.attach(callback(&lv_ticker_func),TICKER_TIME); //Attach callback to ticker |
JohnnyK | 0:10c4b83c458d | 67 | |
JohnnyK | 0:10c4b83c458d | 68 | #ifndef DEMO_H |
JohnnyK | 0:10c4b83c458d | 69 | printf("Hello word\n"); |
JohnnyK | 0:10c4b83c458d | 70 | lv_obj_t * label_h = lv_label_create(lv_scr_act(), NULL); |
JohnnyK | 0:10c4b83c458d | 71 | static lv_style_t st; |
JohnnyK | 0:10c4b83c458d | 72 | lv_style_copy( &st, &lv_style_plain ); |
JohnnyK | 0:10c4b83c458d | 73 | st.text.font = &lv_font_roboto_28; |
JohnnyK | 0:10c4b83c458d | 74 | lv_obj_set_style( label_h, &st ); |
JohnnyK | 0:10c4b83c458d | 75 | lv_obj_set_size(label_h, 50, 30); |
JohnnyK | 0:10c4b83c458d | 76 | lv_label_set_recolor(label_h, true); |
JohnnyK | 0:10c4b83c458d | 77 | lv_label_set_text(label_h, "#ff0000 Hello word#"); |
JohnnyK | 0:10c4b83c458d | 78 | lv_obj_align(label_h, NULL, LV_ALIGN_IN_TOP_MID, 0, 0); |
JohnnyK | 0:10c4b83c458d | 79 | |
JohnnyK | 0:10c4b83c458d | 80 | lv_obj_t * label; |
JohnnyK | 0:10c4b83c458d | 81 | lv_obj_t * btn1 = lv_btn_create(lv_scr_act(), NULL); |
JohnnyK | 0:10c4b83c458d | 82 | lv_obj_set_event_cb(btn1, event_handler); |
JohnnyK | 0:10c4b83c458d | 83 | lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, -40); |
JohnnyK | 0:10c4b83c458d | 84 | label = lv_label_create(btn1, NULL); |
JohnnyK | 0:10c4b83c458d | 85 | lv_label_set_text(label, "Button"); |
JohnnyK | 0:10c4b83c458d | 86 | lv_obj_t *btn2 = lv_btn_create(lv_scr_act(), NULL); |
JohnnyK | 0:10c4b83c458d | 87 | lv_obj_set_event_cb(btn2, event_handler); |
JohnnyK | 0:10c4b83c458d | 88 | lv_obj_align(btn2, NULL, LV_ALIGN_CENTER, 0, 40); |
JohnnyK | 0:10c4b83c458d | 89 | lv_btn_set_toggle(btn2, true); |
JohnnyK | 0:10c4b83c458d | 90 | lv_btn_toggle(btn2); |
JohnnyK | 0:10c4b83c458d | 91 | lv_btn_set_fit2(btn2, LV_FIT_NONE, LV_FIT_TIGHT); |
JohnnyK | 0:10c4b83c458d | 92 | label = lv_label_create(btn2, NULL); |
JohnnyK | 0:10c4b83c458d | 93 | lv_label_set_text(label, "Toggled"); |
JohnnyK | 0:10c4b83c458d | 94 | #else |
JohnnyK | 0:10c4b83c458d | 95 | printf("Demo\n"); |
JohnnyK | 0:10c4b83c458d | 96 | demo_create(); // Demo from lv_examples |
JohnnyK | 0:10c4b83c458d | 97 | #endif |
JohnnyK | 0:10c4b83c458d | 98 | |
JohnnyK | 0:10c4b83c458d | 99 | while(1) { |
JohnnyK | 1:627f26953c53 | 100 | //do something |
JohnnyK | 1:627f26953c53 | 101 | //thread_sleep_for(WAIT_TIME); |
JohnnyK | 0:10c4b83c458d | 102 | } |
JohnnyK | 0:10c4b83c458d | 103 | } |