1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
/**
* @file lv_nuttx_touchscreen.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_nuttx_touchscreen.h"
#if LV_USE_NUTTX
#if LV_USE_NUTTX_TOUCHSCREEN
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <debug.h>
#include <errno.h>
#include <fcntl.h>
#include <nuttx/input/touchscreen.h>
#include "../../lvgl_private.h"
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
typedef struct {
/* fd should be defined at the beginning */
int fd;
lv_indev_state_t last_state;
lv_indev_t * indev_drv;
} lv_nuttx_touchscreen_t;
/**********************
* STATIC PROTOTYPES
**********************/
static void touchscreen_read(lv_indev_t * drv, lv_indev_data_t * data);
static void touchscreen_delete_cb(lv_event_t * e);
static lv_indev_t * touchscreen_init(int fd);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
lv_indev_t * lv_nuttx_touchscreen_create(const char * dev_path)
{
lv_indev_t * indev;
int fd;
LV_ASSERT_NULL(dev_path);
LV_LOG_USER("touchscreen %s opening", dev_path);
fd = open(dev_path, O_RDONLY | O_NONBLOCK);
if(fd < 0) {
perror("Error: cannot open touchscreen device");
return NULL;
}
LV_LOG_USER("touchscreen %s open success", dev_path);
indev = touchscreen_init(fd);
if(indev == NULL) {
close(fd);
}
return indev;
}
/**********************
* STATIC FUNCTIONS
**********************/
static void touchscreen_read(lv_indev_t * drv, lv_indev_data_t * data)
{
lv_nuttx_touchscreen_t * touchscreen = drv->driver_data;
struct touch_sample_s sample;
/* Read one sample */
int nbytes = read(touchscreen->fd, &sample,
sizeof(struct touch_sample_s));
/* Handle unexpected return values */
if(nbytes == sizeof(struct touch_sample_s)) {
uint8_t touch_flags = sample.point[0].flags;
if(touch_flags & TOUCH_DOWN || touch_flags & TOUCH_MOVE) {
const lv_display_t * disp_drv = drv->disp;
int32_t ver_max = disp_drv->ver_res - 1;
int32_t hor_max = disp_drv->hor_res - 1;
data->point.x = LV_CLAMP(0, sample.point[0].x, hor_max);
data->point.y = LV_CLAMP(0, sample.point[0].y, ver_max);
touchscreen->last_state = LV_INDEV_STATE_PRESSED;
}
else if(touch_flags & TOUCH_UP) {
touchscreen->last_state = LV_INDEV_STATE_RELEASED;
}
/* Read until the last point */
data->continue_reading = true;
}
data->state = touchscreen->last_state;
}
static void touchscreen_delete_cb(lv_event_t * e)
{
lv_indev_t * indev = (lv_indev_t *) lv_event_get_user_data(e);
lv_nuttx_touchscreen_t * touchscreen = lv_indev_get_driver_data(indev);
if(touchscreen) {
lv_indev_set_driver_data(indev, NULL);
lv_indev_set_read_cb(indev, NULL);
if(touchscreen->fd >= 0) {
close(touchscreen->fd);
touchscreen->fd = -1;
}
lv_free(touchscreen);
LV_LOG_USER("done");
}
}
static lv_indev_t * touchscreen_init(int fd)
{
lv_nuttx_touchscreen_t * touchscreen;
lv_indev_t * indev = NULL;
touchscreen = lv_malloc_zeroed(sizeof(lv_nuttx_touchscreen_t));
if(touchscreen == NULL) {
LV_LOG_ERROR("touchscreen_s malloc failed");
return NULL;
}
touchscreen->fd = fd;
touchscreen->last_state = LV_INDEV_STATE_RELEASED;
touchscreen->indev_drv = indev = lv_indev_create();
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
lv_indev_set_read_cb(indev, touchscreen_read);
lv_indev_set_driver_data(indev, touchscreen);
lv_indev_add_event_cb(indev, touchscreen_delete_cb, LV_EVENT_DELETE, indev);
return indev;
}
#endif /*LV_USE_NUTTX_TOUCHSCREEN*/
#endif /* LV_USE_NUTTX*/
|