summaryrefslogtreecommitdiffstats
path: root/main/common/tsi.c
blob: dd5a53ed040b30004deb395e835f753fcde8494a (plain) (blame)
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
/**************************************************************************
 *
 * Copyright (c) 2013 Alcatel-Lucent
 * 
 * Alcatel Lucent licenses this file to You under the Apache License,
 * Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License.  A copy of the License is contained the
 * file LICENSE at the top level of this repository.
 * You may also obtain a copy of the License at:
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 **************************************************************************
 *
 * tsi.c:
 *
 * Touch Screen Interface...
 *
 * Original author:     Ed Sutter (ed.sutter@alcatel-lucent.com)
 *
 */
#include "config.h"
#if INCLUDE_TSI & INCLUDE_FBI
#include "genlib.h"
#include "cli.h"
#include "fbi.h"
#include "tsi.h"
#include "timer.h"


int
tsi_scribble(int width, long color)
{
	int x, y, xx, yy, done, last_x, last_y;
	static int xadjust, yadjust;

	printf("Entering 'scribble' mode, hit any key to terminate...\n");

	last_x = last_y = done = 0;
	while (!done) {
		/* Use 'a', 'd', 'w' and 'x' as special input to adjust the
		 * coordinates between touch and frame buffer.
		 */
		if (gotachar()) {
			switch(getchar()) {
				case 'a':		// LEFT
					xadjust--;
					break;
				case 'd':		// RIGHT
					xadjust++;
					break;
				case 'w':		// UP
					yadjust--;
					break;
				case 'x':		// DOWN
					yadjust++;
					break;
				default:
					done = 1;
					break;
			}
		}
		if (tsi_active()) {
			x = tsi_getx();
			y = tsi_gety();

			/* If incoming coordinate is ever more than 'PEN_MOVE_FILTER'
			 * pixels away from the previous position, then assume it
			 * to be noise and don't display it.
			 */
			if ((x < (last_x + PEN_MOVE_FILTER)) &&
				(x > (last_x - PEN_MOVE_FILTER)) &&
			    (y < (last_y + PEN_MOVE_FILTER)) &&
				(y > (last_y - PEN_MOVE_FILTER))) 
			{
				x += xadjust;
				y += yadjust;
				for(xx = x-width;xx <= x+width;xx++) {
					for(yy = y-width;yy <= y+width;yy++)
						fbi_setpixel(xx,yy,color);
				}
			}
			last_x = x;
			last_y = y;
		}
	}
	if ((xadjust != 0) || (yadjust != 0))
		printf("Xadjust: %d, Yadjust: %d\n",xadjust, yadjust);
	return(0);
}

#define WFNT_FILTER 200

char *TsiHelp[] = {
	"Touch screen interface",
	"-[v] {cmd} [args...]",
#if INCLUDE_VERBOSEHELP
	"Options:",
	" -v           additive verbosity",
	"",
	"Commands:",
	" scribble {width} {color}    # draw on the frame buffer",
	" wft [msec-timeout]          # wait for touch",
	" wfnt [msec-timeout]         # wait for no touch",
	"",
	"Notes:",
	" Shell variable 'TSI_X' and 'TSI_Y' are populated by 'wft'.",
#endif
	0,
};

int
TsiCmd(int argc,char *argv[])
{
	static int tsiInitialized;
	int	opt, verbose;
	char *cmd, *arg1;
    static struct elapsed_tmr tmr;

	// Make sure the touch-screen interface is initialized...
	if (!tsiInitialized) {
		if (tsi_init() == -1) {
			printf("Error intializing touch screen!\n");
			return(CMD_FAILURE);
		}
		tsiInitialized = 1;
	}
	verbose = 0;
	while((opt=getopt(argc,argv,"v")) != -1) {
		switch(opt) {
		case 'v':
			verbose++;
			break;
		default:
			return(CMD_PARAM_ERROR);
		}
	}
	if (argc < optind + 1)
		return(CMD_FAILURE);

	cmd = argv[optind];
	arg1 = argv[optind+1];

	if (strcmp(cmd,"scribble") == 0) {
		int width;
		long color;

		if (argc != optind+3)
			return(CMD_PARAM_ERROR);
		
		width = strtol(arg1,0,0);
		color = strtol(argv[optind+2],0,0);
		tsi_scribble(width,color);
	}
	else if (strcmp(cmd,"wfnt") == 0) {
		int x, y;
		int filter, ftot;

		if (argc == optind+2) {
			int timeout = strtol(arg1,0,0);

    		startElapsedTimer(&tmr,timeout);
			while(1) {
				x = tsi_getx();
				y = tsi_gety();
				if (!tsi_active()) {
					for(ftot=0,filter=0;filter<WFNT_FILTER;filter++)
						ftot += tsi_active() ? 0 : 1;
					if (ftot == WFNT_FILTER)
						break;
				}
   				if(msecElapsed(&tmr)) {
					x = y = -1;
					break;
				}
			}
		}
		else {
			while(1) {
				x = tsi_getx();
				y = tsi_gety();
				if (!tsi_active()) {
					for(ftot=0,filter=0;filter<WFNT_FILTER;filter++)
						ftot += tsi_active() ? 0 : 1;
					if (ftot == WFNT_FILTER)
						break;
				}
			}
		}
		shell_sprintf("TSI_X","%d",x);
		shell_sprintf("TSI_Y","%d",y);
	}
	else if (strcmp(cmd,"wft") == 0) {
		int x, y;

		if (argc == optind+2) {
			int timeout = strtol(arg1,0,0);

    		startElapsedTimer(&tmr,timeout);
			while(1) {
				if (tsi_active()) {
					x = tsi_getx();
					y = tsi_gety();
					break;
				}
   				if(msecElapsed(&tmr)) {
					x = y = -1;
					break;
				}
			}
		}
		else {
			while(!tsi_active());
			x = tsi_getx();
			y = tsi_gety();
		}
		shell_sprintf("TSI_X","%d",x);
		shell_sprintf("TSI_Y","%d",y);
	}
	else
		return(CMD_PARAM_ERROR);

	return(CMD_SUCCESS);
}
#endif