1#include <lvgl.h>
2#include <TFT_eSPI.h>
3
4TFT_eSPI tft = TFT_eSPI(); /* TFT instance */
5static lv_disp_buf_t disp_buf;
6static lv_color_t buf[LV_HOR_RES_MAX * 10];
7
8#if USE_LV_LOG != 0
9/* Serial debugging */
10void my_print(lv_log_level_t level, const char * file, uint32_t line, const char * dsc)
11{
12
13    Serial.printf("%s@%d->%s\r\n", file, line, dsc);
14    Serial.flush();
15}
16#endif
17
18/* Display flushing */
19void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
20{
21    uint32_t w = (area->x2 - area->x1 + 1);
22    uint32_t h = (area->y2 - area->y1 + 1);
23
24    tft.startWrite();
25    tft.setAddrWindow(area->x1, area->y1, w, h);
26    tft.pushColors(&color_p->full, w * h, true);
27    tft.endWrite();
28
29    lv_disp_flush_ready(disp);
30}
31
32/*Read the touchpad*/
33bool my_touchpad_read(lv_indev_drv_t * indev_driver, lv_indev_data_t * data)
34{
35    uint16_t touchX, touchY;
36
37    bool touched = tft.getTouch(&touchX, &touchY, 600);
38
39    if(!touched)
40    {
41      data->state = LV_INDEV_STATE_REL;
42      return false;
43    }
44    else
45    {
46      data->state = LV_INDEV_STATE_PR;
47    }
48
49    if(touchX>screenWidth || touchY > screenHeight)
50    {
51      Serial.println("Y or y outside of expected parameters..");
52      Serial.print("y:");
53      Serial.print(touchX);
54      Serial.print(" x:");
55      Serial.print(touchY);
56    }
57    else
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
71    return false; /*Return `false` because we are not buffering and no more data to read*/
72}
73
74void setup()
75{
76    Serial.begin(115200); /* prepare for possible serial debug */
77
78    lv_init();
79
80#if USE_LV_LOG != 0
81    lv_log_register_print_cb(my_print); /* register print function for debugging */
82#endif
83
84    tft.begin(); /* TFT init */
85    tft.setRotation(1); /* Landscape orientation */
86
87    uint16_t calData[5] = { 275, 3620, 264, 3532, 1 };
88    tft.setTouch(calData);
89
90    lv_disp_buf_init(&disp_buf, buf, NULL, LV_HOR_RES_MAX * 10);
91
92    /*Initialize the display*/
93    lv_disp_drv_t disp_drv;
94    lv_disp_drv_init(&disp_drv);
95    disp_drv.hor_res = 320;
96    disp_drv.ver_res = 240;
97    disp_drv.flush_cb = my_disp_flush;
98    disp_drv.buffer = &disp_buf;
99    lv_disp_drv_register(&disp_drv);
100
101    /*Initialize the (dummy) input device driver*/
102    lv_indev_drv_t indev_drv;
103    lv_indev_drv_init(&indev_drv);
104    indev_drv.type = LV_INDEV_TYPE_POINTER;
105    indev_drv.read_cb = my_touchpad_read;
106    lv_indev_drv_register(&indev_drv);
107
108		/* Try an example from the lv_examples repository
109		 * https://github.com/lvgl/lv_examples*/
110		lv_ex_btn_1();
111}
112
113
114void loop()
115{
116
117    lv_task_handler(); /* let the GUI do its work */
118    delay(5);
119}
120