-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts_test.c
383 lines (333 loc) · 7.91 KB
/
ts_test.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
* tslib/src/ts_test.c
*
* Copyright (C) 2001 Russell King.
*
* This file is placed under the GPL. Please see the file
* COPYING for more details.
*
* $Id: ts_test.c,v 1.6 2004/10/19 22:01:27 dlowder Exp $
*
* Basic test program for touchscreen library.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/time.h>
#include "tslib.h"
#include "fbutils.h"
double opVal, value;
int opSort;
/*is caculate*/
int isCaculate = 0;
static int palette [] =
{
0x000000, 0xffe080, 0xffffff, 0xe0c0a0, 0x304050, 0x80b8c0
};
#define NR_COLORS (sizeof (palette) / sizeof (palette [0]))
struct ts_button {
int x, y, w, h;
char *text;
int flags;
#define BUTTON_ACTIVE 0x00000001
};
/* [inactive] border fill text [active] border fill text */
static int button_palette [6] =
{
1, 4, 2,
1, 5, 0
};
#define NR_BUTTONS 18
static struct ts_button buttons [NR_BUTTONS];
static void sig(int sig)
{
close_framebuffer();
fflush(stderr);
printf("signal %d caught\n", sig);
fflush(stdout);
exit(1);
}
static void button_draw (struct ts_button *button)
{
int s = (button->flags & BUTTON_ACTIVE) ? 3 : 0;
rect (button->x, button->y, button->x + button->w - 1,
button->y + button->h - 1, button_palette [s]);
fillrect (button->x + 1, button->y + 1,
button->x + button->w - 2,
button->y + button->h - 2, button_palette [s + 1]);
put_string_center (button->x + button->w / 2,
button->y + button->h / 2,
button->text, button_palette [s + 2]);
}
static void refresh_screen ()
{
int i;
/*draw a rectangle*/
fillrect (0, 0, xres - 1, yres - 1, 0);
//put_string_center (xres/2, yres/4, "TSLIB test program", 1);
//put_string_center (xres/2, yres/4+20,"Touch screen to move crosshair", 2);
for (i = 0; i < NR_BUTTONS; i++)
button_draw (&buttons [i]);
}
double caculate(int op){
switch(op){
case 1:
opVal += value;
break;
case 2:
opVal -= value;
break;
case 3:
opVal *= value;
break;
case 4:
opVal /= value;
break;
}
value = opVal;
opSort = 0;
}
static int button_handle (struct ts_button *button, struct ts_sample *samp)
{
int inside = (samp->x >= button->x) && (samp->y >= button->y) &&
(samp->x < button->x + button->w) &&
(samp->y < button->y + button->h);
if (samp->pressure > 0) {
if (inside) {
if (!(button->flags & BUTTON_ACTIVE)) {
button->flags |= BUTTON_ACTIVE;
button_draw (button);
/*Calculator Process*/
if(button->text[0] == 'C'){ // Clear
opVal = 0;
value = 0;
isCaculate = 0;
}
else if(button->text[0] == "D"){ // DELete
opVal = 0;
value /= 10;
isCaculate = 0;
}
else if(button->text[0] == '+'){
if(opSort)
caculate(opSort);
opVal = value;
value = 0;
opSort = 1;
isCaculate = 0;
}
else if(button->text[0] == '-'){
if(opSort)
caculate(opSort);
opVal = value;
value = 0;
opSort = 2;
isCaculate = 0;
}
else if(button->text[0] == '*'){
if(opSort)
caculate(opSort);
opVal = value;
value = 0;
opSort = 3;
isCaculate = 0;
}
else if(button->text[0] == '/'){
if(opSort)
caculate(opSort);
opVal = value;
value = 0;
opSort = 4;
isCaculate = 0;
}
else if(button->text[0] == '='){
switch(opSort){
case 1:
opVal += value;
break;
case 2:
opVal -= value;
break;
case 3:
opVal *= value;
break;
case 4:
opVal /= value;
break;
}
value = opVal;
isCaculate = 1;
opSort = 0;
}
else{ //number
if(isCaculate){
isCaculate = 0;
value = 0;
}
value = 10*value + button->text[0] - '0';//char->int
}
refresh_screen();
char str_value[50];//int -> char for display
sprintf(str_value,"%lf",value);
put_string_center(xres/2,7*yres/50,str_value,1);
}
} else if (button->flags & BUTTON_ACTIVE) {
button->flags &= ~BUTTON_ACTIVE;
button_draw (button);
}
} else if (button->flags & BUTTON_ACTIVE) {
button->flags &= ~BUTTON_ACTIVE;
button_draw (button);
return 1;
}
return 0;
}
int main()
{
struct tsdev *ts;
int x, y;
unsigned int i;
unsigned int mode = 0;
char *tsdevice=NULL;
signal(SIGSEGV, sig);
signal(SIGINT, sig);
signal(SIGTERM, sig);
if ((tsdevice = getenv("TSLIB_TSDEVICE")) == NULL) {
#ifdef USE_INPUT_API
tsdevice = strdup ("/dev/input/event0");
#else
tsdevice = strdup ("/dev/touchscreen/ucb1x00");
#endif /* USE_INPUT_API */
}
ts = ts_open (tsdevice, 0);
if (!ts) {
perror (tsdevice);
exit(1);
}
if (ts_config(ts)) {
perror("ts_config");
exit(1);
}
if (open_framebuffer()) {
close_framebuffer();
exit(1);
}
x = xres/2;
y = yres/2;
for (i = 0; i < NR_COLORS; i++)
setcolor (i, palette [i]);
/* Initialize buttons */
memset (&buttons, 0, sizeof (buttons));
/*buttons [0].w = buttons [1].w = xres / 4;
buttons [0].h = buttons [1].h = 20;
buttons [0].x = xres / 4 - buttons [0].w / 2;
buttons [1].x = (3 * xres) / 4 - buttons [0].w / 2;
buttons [0].y = buttons [1].y = 10;
buttons [0].text = "Drag";
buttons [1].text = "Draw";
buttons [0].*/
/*weight height*/
for(i=1; i<17; i++){
buttons [i].w = xres / 7;
buttons [i].h = yres / 5;
}
buttons [0].w = (2*xres/7) + (xres/49);
buttons [0].h = yres / 5;
/*result a half of xres and position is center*/
buttons [17].w = xres / 2;
buttons [17].h = yres / 5;
buttons [17].text = "";
/*value*/
buttons [0].text = "0";
buttons [1].text = "1";
buttons [2].text = "2";
buttons [3].text = "3";
buttons [4].text = "4";
buttons [5].text = "5";
buttons [6].text = "6";
buttons [7].text = "7";
buttons [8].text = "8";
buttons [9].text = "9";
buttons [10].text = "+";
buttons [11].text = "-";
buttons [12].text = "*";
buttons [13].text = "/";
buttons [14].text = "=";
buttons [15].text = "C";
buttons [16].text = "D";
/*
*800 * 480
*
*such design: the y-blank is yres/25 = 96px, x-blank is xres/49 = 114px;
* */
/*
*
* 17
*15 16 10 11 12 13
*1 2 3 4 5 14
*6 7 8 9 0
*
* */
/*the x position of buttons*/
buttons [15].x = buttons [1].x = buttons [6].x = xres / 49;
buttons [16].x = buttons [2].x = buttons [7].x = (xres/7) + (2*xres/49);
buttons [10].x = buttons [3].x = buttons [8].x = (2*xres/7) + (3*xres/49);
buttons [11].x = buttons [4].x = buttons [9].x = (3*xres/7) + (4*xres/49);
buttons [12].x = buttons [5].x = buttons [0].x = (4*xres/7) + (5*xres/49);
buttons [13].x = buttons [14].x = (5*xres/7) + (6*xres/49);
/*the y position of buttons*/
buttons [15].y =buttons [16].y =buttons [10].y =buttons [11].y =buttons [12].y = buttons [13].y = (yres/5) + (2*yres/25);
buttons [1].y =buttons [2].y =buttons [3].y =buttons [4].y =buttons [5].y = buttons [14].y = (2*yres/5) + (3*yres/25);
buttons [6].y =buttons [7].y =buttons [8].y =buttons [9].y =buttons [0].y = (3*yres/5) + (4*yres/25);
/*result center*/
buttons [17].x = xres / 4;
buttons [17].y = yres / 25;
/*refresh*/
refresh_screen ();
while (1) {
struct ts_sample samp;
int ret;
// Show the cross
if ((mode & 15) != 1)
put_cross(x, y, 2 | XORMODE);
ret = ts_read(ts, &samp, 1);
// Hide it
if ((mode & 15) != 1)
put_cross(x, y, 2 | XORMODE);
if (ret < 0) {
perror("ts_read");
close_framebuffer();
exit(1);
}
if (ret != 1)
continue;
for (i = 0; i < NR_BUTTONS; i++)
if (button_handle (&buttons [i], &samp))
switch (i) {
case 0:
mode = 0;
refresh_screen ();
break;
case 1:
mode = 1;
refresh_screen ();
break;
}
printf("%ld.%06ld: %6d %6d %6d\n", samp.tv.tv_sec, samp.tv.tv_usec,
samp.x, samp.y, samp.pressure);
if (samp.pressure > 0) {
if (mode == 0x80000001)
line (x, y, samp.x, samp.y, 2);
x = samp.x;
y = samp.y;
mode |= 0x80000000;
} else
mode &= ~0x80000000;
}
close_framebuffer();
}