Example of using the LVGL (8.3.4) with the MbedOS (6.16 - bare metal) on the Disco-F746NG board

Dependencies:   BSP_DISCO_F746NG

Revision:
3:4f5dc253eb7b
Parent:
2:afc050526249
Child:
5:071136c3eefa
--- a/main.cpp	Tue Apr 07 08:06:45 2020 +0000
+++ b/main.cpp	Sat Apr 24 19:08:28 2021 +0000
@@ -1,103 +1,81 @@
 //Notebook page https://os.mbed.com/users/JohnnyK/notebook/how-start-with-the-littlevgl/
 #include "mbed.h"
 #include "lvgl/lvgl.h"
-#include "lv_port_disp.h"
-#include "lv_port_indev.h"
-#include "demo.h" //Comment/uncomment will switch between LittlevGL demo and Hello word example
+#include "hal_stm_lvgl/tft/tft.h"
+#include "hal_stm_lvgl/touchpad/touchpad.h" 
+#include "lv_examples.h"    /*Comment/uncomment will switch between LVGL demo and Hello word example*/
 
-#define LVGL_TICK 5                             //Time tick value for lvgl in ms (1-10msa)
-#define TICKER_TIME 0.001 * LVGL_TICK           //modified to miliseconds
-#define WAIT_TIME 1000 * LVGL_TICK              //modified to microseconds
+#define LVGL_TICK   10                               //Time tick value for lvgl in ms (1-10msa)
+#define TICKER_TIME 10ms                             //modified to miliseconds
 
-Ticker ticker;                                  //Ticker for lvgl                    
-
+Ticker ticker;                                       //Ticker for lvgl                    
+ 
 //Callback function for lvgl timing.
 void lv_ticker_func(){
-    
     lv_tick_inc(LVGL_TICK); 
     //Call lv_tick_inc(x) every x milliseconds in a Timer or Task (x should be between 1 and 10). 
     //It is required for the internal timing of LittlevGL.
-    lv_task_handler(); 
-    //Call lv_task_handler() periodically every few milliseconds. 
-    //It will redraw the screen if required, handle input devices etc.
 }
 
-#ifndef DEMO_H
-//lvgl buttons object event handler
-static void event_handler(lv_obj_t * obj, lv_event_t event){
-    switch(event) {
-        case LV_EVENT_PRESSED:
-            printf("Pressed\n");
-            break;
-
-        case LV_EVENT_SHORT_CLICKED:
-            printf("Short clicked\n");
-            break;
-
-        case LV_EVENT_CLICKED:
-            printf("Clicked\n");
-            break;
-        
-        case LV_EVENT_VALUE_CHANGED:
-            printf("Toggled\n");
-            break;
-
-        case LV_EVENT_LONG_PRESSED:
-            printf("Long press\n");
-            break;
-
-        case LV_EVENT_LONG_PRESSED_REPEAT:
-            printf("Long press repeat\n");
-            break;
-
-        case LV_EVENT_RELEASED:
-            printf("Released\n");
-            break;
+#ifndef LV_EXAMPLES_H
+static void event_handler(lv_obj_t * obj, lv_event_t event)
+{
+    if(event == LV_EVENT_CLICKED) {
+        printf("Clicked\n");
+    }
+    else if(event == LV_EVENT_VALUE_CHANGED) {
+        printf("Toggled\n");
     }
 }
-#endif   
-
+#endif
+// main() runs in its own thread in the OS
 int main()
 {
-    printf("LittlevGL GUI-"); 
-    lv_init();                                  //Initialize the LittlevGL
-    lv_port_disp_init();                        //Initialize diplay 
-    lv_port_indev_init();                       //Initialize touchpad
-    ticker.attach(callback(&lv_ticker_func),TICKER_TIME); //Attach callback to ticker
-    
-#ifndef DEMO_H
-    printf("Hello word\n");
-    lv_obj_t * label_h = lv_label_create(lv_scr_act(), NULL);
-    static lv_style_t st;
-    lv_style_copy( &st, &lv_style_plain );
-    st.text.font = &lv_font_roboto_28;
-    lv_obj_set_style( label_h, &st );
-    lv_obj_set_size(label_h, 50, 30);
-    lv_label_set_recolor(label_h, true);
-    lv_label_set_text(label_h, "#ff0000 Hello word#");
-    lv_obj_align(label_h, NULL, LV_ALIGN_IN_TOP_MID, 0, 0);
+    printf("LVGL-"); 
+    lv_init();                                              //Initialize the LVGL
+    tft_init();                                             //Initialize diplay 
+    touchpad_init();                                        //Initialize touchpad
+    ticker.attach(callback(&lv_ticker_func),TICKER_TIME);   //Attach callback to ticker
+
+#ifdef LV_EXAMPLES_H
+    printf("Demo\n"); 
+    lv_demo_widgets();
+#else
+    printf("Hello world\n");
+    lv_obj_t * label1 = lv_label_create(lv_scr_act(), NULL);
+    lv_label_set_long_mode(label1, LV_LABEL_LONG_BREAK);     /*Break the long lines*/
+    lv_label_set_recolor(label1, true);                      /*Enable re-coloring by commands in the text*/
+    lv_label_set_align(label1, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/
+    lv_label_set_text(label1, "#0000ff Hello# #ff00ff world# #ff0000 - the LVGL and MbedOS#");
+    lv_obj_set_width(label1, 150);
+    lv_obj_align(label1, NULL, LV_ALIGN_IN_TOP_MID, 0, 20);
+
+    lv_obj_t * label2 = lv_label_create(lv_scr_act(), NULL);
+    lv_label_set_long_mode(label2, LV_LABEL_LONG_SROLL_CIRC);     /*Circular scroll*/
+    lv_obj_set_width(label2, 150);
+    lv_label_set_text(label2, "It is a circularly scrolling text. ");
+    lv_obj_align(label2, NULL, LV_ALIGN_IN_BOTTOM_MID, 0, 0);
     
     lv_obj_t * label;
     lv_obj_t * btn1 = lv_btn_create(lv_scr_act(), NULL);
     lv_obj_set_event_cb(btn1, event_handler);
-    lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, -40);
+    lv_obj_align(btn1, NULL, LV_ALIGN_CENTER, 0, -25);
     label = lv_label_create(btn1, NULL);
     lv_label_set_text(label, "Button");
-    lv_obj_t *btn2 = lv_btn_create(lv_scr_act(), NULL);
+    lv_obj_t * btn2 = lv_btn_create(lv_scr_act(), NULL);
     lv_obj_set_event_cb(btn2, event_handler);
-    lv_obj_align(btn2, NULL, LV_ALIGN_CENTER, 0, 40);
-    lv_btn_set_toggle(btn2, true);
+    lv_obj_align(btn2, NULL, LV_ALIGN_CENTER, 0, 25);
+    lv_btn_set_checkable(btn2, true);
     lv_btn_toggle(btn2);
     lv_btn_set_fit2(btn2, LV_FIT_NONE, LV_FIT_TIGHT);
     label = lv_label_create(btn2, NULL);
     lv_label_set_text(label, "Toggled");
-#else   
-    printf("Demo\n");
-    demo_create(); // Demo from lv_examples
-#endif   
+#endif
 
-    while(1) {
-        //do something   
-        //thread_sleep_for(WAIT_TIME);
+    while (true){
+        lv_task_handler(); 
+        //Call lv_task_handler() periodically every few milliseconds. 
+        //It will redraw the screen if required, handle input devices etc.
+        thread_sleep_for(LVGL_TICK); 
     }
-}
+}
\ No newline at end of file