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