1/*Using LVGL with Arduino requires some extra steps:
2 *Be sure to read the docs here: https://docs.lvgl.io/master/get-started/platforms/arduino.html  */
3
4#include <lvgl.h>
5#include <TFT_eSPI.h>
6
7/*To use the built-in examples and demos of LVGL uncomment the includes below respectively.
8 *You also need to copy `lvgl/examples` to `lvgl/src/examples`. Similarly for the demos `lvgl/demos` to `lvgl/src/demos`.
9 Note that the `lv_examples` library is for LVGL v7 and you shouldn't install it for this version (since LVGL v8)
10 as the examples and demos are now part of the main LVGL library. */
11
12/*Change to your screen resolution*/
13static const uint16_t screenWidth  = 480;
14static const uint16_t screenHeight = 320;
15
16static lv_disp_draw_buf_t draw_buf;
17static lv_color_t buf[ screenWidth * 10 ];
18
19TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight); /* TFT instance */
20
21#if LV_USE_LOG != 0
22/* Serial debugging */
23void my_print(const char * buf)
24{
25    Serial.printf(buf);
26    Serial.flush();
27}
28#endif
29
30/* Display flushing */
31void my_disp_flush( lv_disp_t *disp, const lv_area_t *area, lv_color_t *color_p )
32{
33    uint32_t w = ( area->x2 - area->x1 + 1 );
34    uint32_t h = ( area->y2 - area->y1 + 1 );
35
36    tft.startWrite();
37    tft.setAddrWindow( area->x1, area->y1, w, h );
38    tft.pushColors( ( uint16_t * )&color_p->full, w * h, true );
39    tft.endWrite();
40
41    lv_disp_flush_ready( disp );
42}
43
44/*Read the touchpad*/
45void my_touchpad_read( lv_indev_t * indev_driver, lv_indev_data_t * data )
46{
47    uint16_t touchX, touchY;
48
49    bool touched = tft.getTouch( &touchX, &touchY, 600 );
50
51    if( !touched )
52    {
53        data->state = LV_INDEV_STATE_REL;
54    }
55    else
56    {
57        data->state = LV_INDEV_STATE_PR;
58
59        /*Set the coordinates*/
60        data->point.x = touchX;
61        data->point.y = touchY;
62
63        Serial.print( "Data x " );
64        Serial.println( touchX );
65
66        Serial.print( "Data y " );
67        Serial.println( touchY );
68    }
69}
70
71void setup()
72{
73    Serial.begin( 115200 ); /* prepare for possible serial debug */
74
75    String LVGL_Arduino = "Hello Arduino! ";
76    LVGL_Arduino += String('V') + lv_version_major() + "." + lv_version_minor() + "." + lv_version_patch();
77
78    Serial.println( LVGL_Arduino );
79    Serial.println( "I am LVGL_Arduino" );
80
81    lv_init();
82
83#if LV_USE_LOG != 0
84    lv_log_register_print_cb( my_print ); /* register print function for debugging */
85#endif
86
87    tft.begin();          /* TFT init */
88    tft.setRotation( 3 ); /* Landscape orientation, flipped */
89
90    /*Set the touchscreen calibration data,
91     the actual data for your display can be acquired using
92     the Generic -> Touch_calibrate example from the TFT_eSPI library*/
93    uint16_t calData[5] = { 275, 3620, 264, 3532, 1 };
94    tft.setTouch( calData );
95
96    lv_disp_draw_buf_init( &draw_buf, buf, NULL, screenWidth * 10 );
97
98    /*Initialize the display*/
99    static lv_disp_t disp_drv;
100    lv_disp_drv_init( &disp_drv );
101    /*Change the following line to your display resolution*/
102    disp_drv.hor_res = screenWidth;
103    disp_drv.ver_res = screenHeight;
104    disp_drv.flush_cb = my_disp_flush;
105    disp_drv.draw_buf = &draw_buf;
106    lv_disp_drv_register( &disp_drv );
107
108    /*Initialize the (dummy) input device driver*/
109    static lv_indev_t indev_drv;
110    lv_indev_drv_init( &indev_drv );
111    indev_drv.type = LV_INDEV_TYPE_POINTER;
112    indev_drv.read_cb = my_touchpad_read;
113    lv_indev_drv_register( &indev_drv );
114
115#if 0
116    /* Create simple label */
117    lv_obj_t *label = lv_label_create( lv_scr_act() );
118    lv_label_set_text( label, LVGL_Arduino.c_str() );
119    lv_obj_align( label, LV_ALIGN_CENTER, 0, 0 );
120#else
121    /* Try an example from the lv_examples Arduino library
122       make sure to include it as written above.
123    lv_example_btn_1();
124   */
125
126    // uncomment one of these demos
127    lv_demo_widgets();            // OK
128    // lv_demo_benchmark();          // OK
129    // lv_demo_keypad_encoder();     // works, but I haven't an encoder
130    // lv_demo_music();              // NOK
131    // lv_demo_printer();
132    // lv_demo_stress();             // seems to be OK
133#endif
134    Serial.println( "Setup done" );
135}
136
137void loop()
138{
139    lv_timer_handler(); /* let the GUI do its work */
140    delay( 5 );
141}
142