-
Notifications
You must be signed in to change notification settings - Fork 0
/
touch.c
244 lines (215 loc) · 5.89 KB
/
touch.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//Touchscreen interface code
//Copyright 2014 Andrew Palardy
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include "touch.h"
#include "palscreen.h"
//File globals
int ev_fd;
//Current cursor position
unsigned char key_down;
int cur_x;
int cur_y;
int cur_prs;
int cur_gain_x;
int cur_offs_x;
int cur_gain_y;
int cur_offs_y;
unsigned char kal_done;
void touch_init()
{
ev_fd = open("/dev/input/touchscreen", O_RDONLY|O_NONBLOCK);
//One gain
cur_gain_x = 0x00FF;
cur_gain_y = 0x00FF;
//Zero offset
cur_offs_x = 0;
cur_offs_y = 0;
//Kal not done
kal_done = 0;
//Read all of the stuff from the kernel buffer??
touch_get_events();
}
//Read calibration from kal file
//Note: We can't do this in init since we also use this code for kalibrate
void touch_read_kal()
{
//Fopen the kal file
FILE *kal = fopen("screen.kal","r");
int rtn = 0;
int gain_temp = 0;
int offs_temp = 0;
char buf[64];
printf("TOUCH: Opened kal file screen.kal\n");
//Fscanf in a while loop to get meaningful data
while(1)
{
//Read the next line in the file
rtn = fgets(&buf,64,kal);
//Did we reach the end of file?
if(feof(kal))
{
printf("TOUCH: Kal reached end of file\n");
break;
}
//Try scanning the X line
rtn = sscanf(buf,"LINE X gain=%d offs=%d\n",&gain_temp,&offs_temp);
//Did we get it?
if(rtn != 0)
{
//Got it! Continue
cur_gain_x = gain_temp;
cur_offs_x = offs_temp;
printf("TOUCH: Got X gain=%d offs=%d\n",cur_gain_x,cur_offs_x);
kal_done = 1;
continue;
}
//Nope, maybe try Y line
rtn = sscanf(buf,"LINE Y gain=%d offs=%d\n",&gain_temp,&offs_temp);
//Did we get it?
if(rtn != 0)
{
//Got it! Continue
cur_gain_y = gain_temp;
cur_offs_y = offs_temp;
printf("TOUCH: Got Y gain=%d offs=%d\n",cur_gain_y,cur_offs_y);
kal_done = 1;
continue;
}
//Neither but the line was long enough
printf("TOUCH: Kal found a bad line\n");
}
fclose(kal);
}
unsigned char touch_down()
{
return key_down;
}
int touch_get_x()
{
//Return scaled x - note that touchscreen is inverted
if(!kal_done)
{
return cur_y;
}
//Use cals
#ifndef ROTATE_SCREEN
int temp = cur_y - cur_offs_x;
temp = (temp * cur_gain_x) >> 16;
#else
int temp = cur_x - cur_offs_y;
temp = (temp * cur_gain_y) >> 16;
#endif
return temp;
}
int touch_get_y()
{
//Return scaled y - note that touchscreen is inverted
if(!kal_done)
{
return cur_x;
}
//Use cals
#ifndef ROTATE_SCREEN
int temp = cur_x - cur_offs_y;
temp = (temp * cur_gain_y) >> 16;
#else
int temp = cur_y - cur_offs_x;
temp = (temp * cur_gain_x) >> 16;
#endif
return temp;
}
int touch_get_prs()
{
//Return raw pressure
return cur_prs;
}
void touch_get_events()
{
int len = 0;
struct input_event ev_temp;
//Get all of the recent touch events from the kernel
while(1)
{
//Infinite loops are cooler
len = read(ev_fd, &ev_temp, sizeof(struct input_event));
if (len < 0)
{
return;//End of queue
}
else if (len > 0 && len % sizeof(struct input_event) != 0)
{
printf("TOUCH: Bad read from touch dev\n");
return;//Bad stuffs
}
else if (len > 0)
{
//Number of events
//int nev = len/sizeof(struct input_event);
//Do stuff with this???
//struct input_event {
// struct timeval time;
// __u16 type;
// __u16 code;
// __s32 value;
// };
//For each type of event
if(ev_temp.type == EV_SYN)
{
//Event is a sync event, print it
//printf("TOUCH: Got a Sync event, X=%d, Y=%d, PRS=%d\n",cur_x,cur_y,cur_prs);
//At SYNC events, check pressure=0 for key up/down
if(cur_prs)
{
//Key is down
key_down = 1;
}
else
{
//Key must be up
key_down = 0;
}
}
else if(ev_temp.type == EV_KEY)
{
//Event is a KEY event, print it
//Is it a Touch event?
if(ev_temp.code == BTN_TOUCH)
{
//printf("TOUCH: Got a Key event type TOUCH\n");
}
else
{
//printf("TOUCH: Got a Key event, code %d\n",ev_temp.code);
}
}
else if(ev_temp.type == EV_ABS)
{
//Event is an ABS event, print it
if(ev_temp.code == ABS_X)
{
//printf("TOUCH: X position: %d\n",ev_temp.value);
cur_x = ev_temp.value;
}
else if(ev_temp.code == ABS_Y)
{
//printf("TOUCH: Y position: %d\n",ev_temp.value);
cur_y = ev_temp.value;
}
else if(ev_temp.code == ABS_PRESSURE)
{
//printf("TOUCH: PRESSURE %d\n",ev_temp.value);
cur_prs = ev_temp.value;
}
else
{
//printf("TOUCH: Got an unknown ABS event, code %d, value %d\n",ev_temp.code,ev_temp.value);
}
}
}
}
}