Skip to content
This repository was archived by the owner on Dec 4, 2020. It is now read-only.

Commit 1893719

Browse files
authored
Merge pull request #26 from elgerg/master
Added arduino example using bodmer tft_espi touch
2 parents 69a2582 + 053c749 commit 1893719

File tree

5 files changed

+255
-0
lines changed

5 files changed

+255
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
void slider_event_cb(lv_obj_t * slider, lv_event_t event)
3+
{
4+
5+
printEvent("Slider", event);
6+
7+
if(event == LV_EVENT_VALUE_CHANGED) {
8+
static char buf[4]; /* max 3 bytes for number plus 1 null terminating byte */
9+
snprintf(buf, 4, "%u", lv_slider_get_value(slider));
10+
lv_label_set_text(slider_label, buf); /*Refresh the text*/
11+
}
12+
}
13+
14+
void printEvent(String Event, lv_event_t event)
15+
{
16+
17+
Serial.print(Event);
18+
printf(" ");
19+
20+
switch(event) {
21+
case LV_EVENT_PRESSED:
22+
printf("Pressed\n");
23+
break;
24+
25+
case LV_EVENT_SHORT_CLICKED:
26+
printf("Short clicked\n");
27+
break;
28+
29+
case LV_EVENT_CLICKED:
30+
printf("Clicked\n");
31+
break;
32+
33+
case LV_EVENT_LONG_PRESSED:
34+
printf("Long press\n");
35+
break;
36+
37+
case LV_EVENT_LONG_PRESSED_REPEAT:
38+
printf("Long press repeat\n");
39+
break;
40+
41+
case LV_EVENT_RELEASED:
42+
printf("Released\n");
43+
break;
44+
}
45+
}
46+
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#include <lvgl.h>
2+
#include <Ticker.h>
3+
#include <TFT_eSPI.h>
4+
5+
#define LVGL_TICK_PERIOD 60
6+
7+
Ticker tick; /* timer for interrupt handler */
8+
TFT_eSPI tft = TFT_eSPI(); /* TFT instance */
9+
static lv_disp_buf_t disp_buf;
10+
static lv_color_t buf[LV_HOR_RES_MAX * 10];
11+
12+
lv_obj_t * slider_label;
13+
int screenWidth = 480;
14+
int screenHeight = 320;
15+
16+
#if USE_LV_LOG != 0
17+
/* Serial debugging */
18+
void my_print(lv_log_level_t level, const char * file, uint32_t line, const char * dsc)
19+
{
20+
21+
Serial.printf("%s@%d->%s\r\n", file, line, dsc);
22+
delay(100);
23+
}
24+
#endif
25+
26+
/* Display flushing */
27+
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
28+
{
29+
uint16_t c;
30+
31+
tft.startWrite(); /* Start new TFT transaction */
32+
tft.setAddrWindow(area->x1, area->y1, (area->x2 - area->x1 + 1), (area->y2 - area->y1 + 1)); /* set the working window */
33+
for (int y = area->y1; y <= area->y2; y++) {
34+
for (int x = area->x1; x <= area->x2; x++) {
35+
c = color_p->full;
36+
tft.writeColor(c, 1);
37+
color_p++;
38+
}
39+
}
40+
tft.endWrite(); /* terminate TFT transaction */
41+
lv_disp_flush_ready(disp); /* tell lvgl that flushing is done */
42+
}
43+
44+
bool 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+
return false;
53+
}
54+
55+
if(touchX>screenWidth || touchY > screenHeight)
56+
{
57+
Serial.println("Y or y outside of expected parameters..");
58+
Serial.print("y:");
59+
Serial.print(touchX);
60+
Serial.print(" x:");
61+
Serial.print(touchY);
62+
}
63+
else
64+
{
65+
66+
data->state = touched ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
67+
68+
/*Save the state and save the pressed coordinate*/
69+
//if(data->state == LV_INDEV_STATE_PR) touchpad_get_xy(&last_x, &last_y);
70+
71+
/*Set the coordinates (if released use the last pressed coordinates)*/
72+
data->point.x = touchX;
73+
data->point.y = touchY;
74+
75+
Serial.print("Data x");
76+
Serial.println(touchX);
77+
78+
Serial.print("Data y");
79+
Serial.println(touchY);
80+
81+
}
82+
83+
return false; /*Return `false` because we are not buffering and no more data to read*/
84+
}
85+
86+
/* Interrupt driven periodic handler */
87+
static void lv_tick_handler(void)
88+
{
89+
90+
lv_tick_inc(LVGL_TICK_PERIOD);
91+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
void loop() {
2+
3+
lv_task_handler(); /* let the GUI do its work */
4+
delay(5);
5+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Example for lv_arduino using a slider
2+
3+
This example has the screen set to 320x480 (ILI9488), change this by altering the following lines in the main ino file:
4+
5+
int screenWidth = 480;
6+
int screenHeight = 320;
7+
8+
## Backlight
9+
10+
Change pin 32 to your preferred backlight pin using a PNP transistor (2N3906) or remove the following code and connect directly to +ve:
11+
12+
ledcSetup(10, 5000/*freq*/, 10 /*resolution*/);
13+
ledcAttachPin(32, 10);
14+
analogReadResolution(10);
15+
ledcWrite(10,768);
16+
17+
## Theme selection
18+
19+
Change the following to change the theme:
20+
21+
lv_theme_t * th = lv_theme_night_init(210, NULL); //Set a HUE value and a Font for the Night Theme
22+
lv_theme_set_current(th);
23+
24+
## Calibration
25+
26+
This is using the bodmer tft_espi driver for touch. To correctly set the calibration load the calibration sketch and replace the following with your values:
27+
28+
uint16_t calData[5] = { 275, 3620, 264, 3532, 1 };
29+
30+
## Scrreen rotation
31+
32+
Check the following if you need to alter your screen rotation:
33+
34+
tft.setRotation(3);
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
void setup() {
2+
3+
ledcSetup(10, 5000/*freq*/, 10 /*resolution*/);
4+
ledcAttachPin(32, 10);
5+
analogReadResolution(10);
6+
ledcWrite(10,768);
7+
8+
Serial.begin(115200); /* prepare for possible serial debug */
9+
10+
lv_init();
11+
12+
#if USE_LV_LOG != 0
13+
lv_log_register_print(my_print); /* register print function for debugging */
14+
#endif
15+
16+
tft.begin(); /* TFT init */
17+
tft.setRotation(3);
18+
19+
uint16_t calData[5] = { 275, 3620, 264, 3532, 1 };
20+
tft.setTouch(calData);
21+
22+
lv_disp_buf_init(&disp_buf, buf, NULL, LV_HOR_RES_MAX * 10);
23+
24+
/*Initialize the display*/
25+
lv_disp_drv_t disp_drv;
26+
lv_disp_drv_init(&disp_drv);
27+
disp_drv.hor_res = screenWidth;
28+
disp_drv.ver_res = screenHeight;
29+
disp_drv.flush_cb = my_disp_flush;
30+
disp_drv.buffer = &disp_buf;
31+
lv_disp_drv_register(&disp_drv);
32+
33+
lv_indev_drv_t indev_drv;
34+
lv_indev_drv_init(&indev_drv); /*Descriptor of a input device driver*/
35+
indev_drv.type = LV_INDEV_TYPE_POINTER; /*Touch pad is a pointer-like device*/
36+
indev_drv.read_cb = my_touchpad_read; /*Set your driver function*/
37+
lv_indev_drv_register(&indev_drv); /*Finally register the driver*/
38+
39+
40+
/*Initialize the touch pad*/
41+
//lv_indev_drv_t indev_drv;
42+
//lv_indev_drv_init(&indev_drv);
43+
//indev_drv.type = LV_INDEV_TYPE_ENCODER;
44+
//indev_drv.read_cb = read_encoder;
45+
//lv_indev_drv_register(&indev_drv);
46+
47+
/*Initialize the graphics library's tick*/
48+
tick.attach_ms(LVGL_TICK_PERIOD, lv_tick_handler);
49+
50+
//Set the theme..
51+
lv_theme_t * th = lv_theme_night_init(210, NULL); //Set a HUE value and a Font for the Night Theme
52+
lv_theme_set_current(th);
53+
54+
lv_obj_t * scr = lv_cont_create(NULL, NULL);
55+
lv_disp_load_scr(scr);
56+
57+
//lv_obj_t * tv = lv_tabview_create(scr, NULL);
58+
//lv_obj_set_size(tv, lv_disp_get_hor_res(NULL), lv_disp_get_ver_res(NULL));
59+
60+
/* Create simple label */
61+
lv_obj_t *label = lv_label_create(lv_scr_act(), NULL);
62+
lv_label_set_text(label, "Hello Arduino! (V6.0)");
63+
lv_obj_align(label, NULL, LV_ALIGN_CENTER, 0, -50);
64+
65+
/* Create a slider in the center of the display */
66+
lv_obj_t * slider = lv_slider_create(lv_scr_act(), NULL);
67+
lv_obj_set_width(slider, screenWidth-50); /*Set the width*/
68+
lv_obj_set_height(slider, 50);
69+
lv_obj_align(slider, NULL, LV_ALIGN_CENTER, 0, 0); /*Align to the center of the parent (screen)*/
70+
lv_obj_set_event_cb(slider, slider_event_cb); /*Assign an event function*/
71+
72+
/* Create a label below the slider */
73+
slider_label = lv_label_create(lv_scr_act(), NULL);
74+
lv_label_set_text(slider_label, "0");
75+
lv_obj_set_auto_realign(slider, true);
76+
lv_obj_align(slider_label, slider, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);
77+
78+
}
79+

0 commit comments

Comments
 (0)