-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.c
450 lines (409 loc) · 12.4 KB
/
input.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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* tab:8
*
* input.c - source file for input control to maze game
*
* "Copyright (c) 2004-2011 by Steven S. Lumetta."
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF ILLINOIS BE LIABLE TO
* ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
* DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
* EVEN IF THE AUTHOR AND/OR THE UNIVERSITY OF ILLINOIS HAS BEEN ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE AUTHOR AND THE UNIVERSITY OF ILLINOIS SPECIFICALLY DISCLAIM ANY
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
* PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND NEITHER THE AUTHOR NOR
* THE UNIVERSITY OF ILLINOIS HAS ANY OBLIGATION TO PROVIDE MAINTENANCE,
* SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Author: Steve Lumetta
* Version: 7
* Creation Date: Thu Sep 9 22:25:48 2004
* Filename: input.c
* History:
* SL 1 Thu Sep 9 22:25:48 2004
* First written.
* SL 2 Sat Sep 12 14:34:19 2009
* Integrated original release back into main code base.
* SL 3 Sun Sep 13 03:51:23 2009
* Replaced parallel port with Tux controller code for demo.
* SL 4 Sun Sep 13 12:49:02 2009
* Changed init_input order slightly to avoid leaving keyboard
* in odd state on failure.
* SL 5 Sun Sep 13 16:30:32 2009
* Added a reasonably robust direct Tux control for demo mode.
* SL 6 Wed Sep 14 02:06:41 2011
* Updated input control and test driver for adventure game.
* SL 7 Wed Sep 14 17:07:38 2011
* Added keyboard input support when using Tux kernel mode.
*/
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/io.h>
#include <termio.h>
#include <termios.h>
#include <unistd.h>
#include "assert.h"
#include "input.h"
#include "module/tuxctl-ioctl.h"
/* set to 1 and compile this file by itself to test functionality */
#define TEST_INPUT_DRIVER 0
/* set to 1 to use tux controller; otherwise, uses keyboard input */
#define USE_TUX_CONTROLLER 0
/* stores original terminal settings */
static struct termios tio_orig;
int fd;
/*
* init_input
* DESCRIPTION: Initializes the input controller. As both keyboard and
* Tux controller control modes use the keyboard for the quit
* command, this function puts stdin into character mode
* rather than the usual terminal mode.
* INPUTS: none
* OUTPUTS: none
* RETURN VALUE: 0 on success, -1 on failure
* SIDE EFFECTS: changes terminal settings on stdin; prints an error
* message on failure
*/
int
init_input ()
{
struct termios tio_new;
/*
* Set non-blocking mode so that stdin can be read without blocking
* when no new keystrokes are available.
*/
if (fcntl (fileno (stdin), F_SETFL, O_NONBLOCK) != 0) {
perror ("fcntl to make stdin non-blocking");
return -1;
}
/*
* Save current terminal attributes for stdin.
*/
if (tcgetattr (fileno (stdin), &tio_orig) != 0) {
perror ("tcgetattr to read stdin terminal settings");
return -1;
}
/*
* Turn off canonical (line-buffered) mode and echoing of keystrokes
* to the monitor. Set minimal character and timing parameters so as
* to prevent delays in delivery of keystrokes to the program.
*/
tio_new = tio_orig;
tio_new.c_lflag &= ~(ICANON | ECHO);
tio_new.c_cc[VMIN] = 1;
tio_new.c_cc[VTIME] = 0;
if (tcsetattr (fileno (stdin), TCSANOW, &tio_new) != 0) {
perror ("tcsetattr to set stdin terminal settings");
return -1;
}
/* Return success. */
tux_initial();
return 0;
}
void tux_initial(){
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
int ldsic_num = N_MOUSE;
ioctl(fd, TIOCSETD, &ldsic_num);
ioctl(fd, TUX_INIT);
}
static char typing[MAX_TYPED_LEN + 1] = {'\0'};
const char*
get_typed_command ()
{
return typing;
}
void
reset_typed_command ()
{
typing[0] = '\0';
}
static int32_t
valid_typing (char c)
{
/* Valid typing include letters, numbers, space, and backspace/delete. */
return (isalpha (c) || isdigit (c) || ' ' == c || 8 == c || 127 == c);
}
static void
typed_a_char (char c)
{
int32_t len = strlen (typing);
if (8 == c || 127 == c) {
if (0 < len) {
typing[len - 1] = '\0';
}
} else if (MAX_TYPED_LEN > len) {
typing[len] = c;
typing[len + 1] = '\0';
}
}
/*
* get_command
* DESCRIPTION: Reads a command from the input controller. As some
* controllers provide only absolute input (e.g., go
* right), the current direction is needed as an input
* to this routine.
* INPUTS: cur_dir -- current direction of motion
* OUTPUTS: none
* RETURN VALUE: command issued by the input controller
* SIDE EFFECTS: drains any keyboard input
*/
cmd_t
get_command ()
{
#if (USE_TUX_CONTROLLER == 0) /* use keyboard control with arrow keys */
static int state = 0; /* small FSM for arrow keys */
#endif
static cmd_t command = CMD_NONE;
cmd_t pushed = CMD_NONE;
int ch;
/* Read all characters from stdin. */
while ((ch = getc (stdin)) != EOF) {
/* Backquote is used to quit the game. */
if (ch == '`')
return CMD_QUIT;
#if (USE_TUX_CONTROLLER == 0) /* use keyboard control with arrow keys */
/*
* Arrow keys deliver the byte sequence 27, 91, and 'A' to 'D';
* we use a small finite state machine to identify them.
*
* Insert, home, and page up keys deliver 27, 91, '2'/'1'/'5' and
* then a tilde. We recognize the digits and don't check for the
* tilde.
*/
switch (state) {
case 0:
if (27 == ch) {
state = 1;
} else if (valid_typing (ch)) {
typed_a_char (ch);
} else if (10 == ch || 13 == ch) {
pushed = CMD_TYPED;
}
break;
case 1:
if (91 == ch) {
state = 2;
} else {
state = 0;
if (valid_typing (ch)) {
/*
* Note that we may be discarding an ESC (27), but
* we don't use that as typed input anyway.
*/
typed_a_char (ch);
} else if (10 == ch || 13 == ch) {
pushed = CMD_TYPED;
}
}
break;
case 2:
if (ch >= 'A' && ch <= 'D') {
switch (ch) {
case 'A': pushed = CMD_UP; break;
case 'B': pushed = CMD_DOWN; break;
case 'C': pushed = CMD_RIGHT; break;
case 'D': pushed = CMD_LEFT; break;
}
state = 0;
} else if (ch == '1' || ch == '2' || ch == '5') {
switch (ch) {
case '2': pushed = CMD_MOVE_LEFT; break;
case '1': pushed = CMD_ENTER; break;
case '5': pushed = CMD_MOVE_RIGHT; break;
}
state = 3; /* Consume a '~'. */
} else {
state = 0;
if (valid_typing (ch)) {
/*
* Note that we may be discarding an ESC (27) and
* a bracket (91), but we don't use either as
* typed input anyway.
*/
typed_a_char (ch);
} else if (10 == ch || 13 == ch) {
pushed = CMD_TYPED;
}
}
break;
case 3:
state = 0;
if ('~' == ch) {
/* Consume it silently. */
} else if (valid_typing (ch)) {
typed_a_char (ch);
} else if (10 == ch || 13 == ch) {
pushed = CMD_TYPED;
}
break;
}
#else /* USE_TUX_CONTROLLER */
/* Tux controller mode; still need to support typed commands. */
if (valid_typing (ch)) {
typed_a_char (ch);
} else if (10 == ch || 13 == ch) {
pushed = CMD_TYPED;
}
#endif /* USE_TUX_CONTROLLER */
}
/*
* Once a direction is pushed, that command remains active
* until a turn is taken.
*/
if (pushed == CMD_NONE) {
command = CMD_NONE;
}
return pushed;
}
/*
* get_tux_command
* DESCRIPTION: Reads a command from the tux controller. As some
* controllers provide only absolute input (e.g., go
* right), the current direction is needed as an input
* to this routine.
* INPUTS: cur_dir -- current direction of motion
* OUTPUTS: none
* RETURN VALUE: command issued by the tux controller
* SIDE EFFECTS: drains any tux input
*/
cmd_t
get_tux_command ()
{
unsigned char arg;
static cmd_t command = CMD_NONE;
static cmd_t prev_command = CMD_NONE;
cmd_t return_val = CMD_NONE;
int ch;
/* Read all characters from stdin. */
while ((ch = getc (stdin)) != EOF) {
/* Backquote is used to quit the game. */
if (ch == '`')
return CMD_QUIT;
}
// poll driver
ioctl(fd, TUX_BUTTONS, &arg);
//if go right
if ((~arg) & right_mask){ return_val = CMD_RIGHT;
}
//if go left
else if ((~arg) & left_mask){ return_val = CMD_LEFT;
}
//if go up
else if ((~arg) & up_mask){ return_val = CMD_UP;
}
//if go down
else if ((~arg) & down_mask){ return_val = CMD_DOWN;
}
//if move left
else if ((~arg) & move_left_mask){ return_val = CMD_MOVE_LEFT;
}
//if move right
else if ((~arg) & move_right_mask){ return_val = CMD_MOVE_RIGHT;
}
//if enter
else if ((~arg) & enter_mask){ return_val = CMD_ENTER;
}
else { return_val = CMD_NONE;
}
/*
* Once a direction is pushed, that command remains active
* until a turn is taken.
*/
if ((return_val == CMD_MOVE_RIGHT) |(return_val == CMD_ENTER) |(return_val == CMD_MOVE_LEFT)) {
if(return_val == prev_command){
command = CMD_NONE;
}
else{
command = return_val;
}
}
else{
command = return_val;
}
prev_command = return_val;
return command;
}
/*
* shutdown_input
* DESCRIPTION: Cleans up state associated with input control. Restores
* original terminal settings.
* INPUTS: none
* OUTPUTS: none
* RETURN VALUE: none
* SIDE EFFECTS: restores original terminal settings
*/
void
shutdown_input ()
{
(void)tcsetattr (fileno (stdin), TCSANOW, &tio_orig);
}
/*
* display_time_on_tux
* DESCRIPTION: Show number of elapsed seconds as minutes:seconds
* on the Tux controller's 7-segment displays.
* INPUTS: num_seconds -- total seconds elapsed so far
* OUTPUTS: none
* RETURN VALUE: none
* SIDE EFFECTS: changes state of controller's display
*/
void
display_time_on_tux (int num_seconds)
{
unsigned int min_2, min_1, sec_2, sec_1;
unsigned long arg;
//Initialize value
min_2 = 0;
min_1 = 0;
sec_2 = 0;
sec_1 = 0;
arg = 0;
//calculate first place on minite
min_1 = ((num_seconds / 60) % 10);
//calculate tenth place on minite
min_2 = ((num_seconds / 60) / 10);
//calculate first place on second
sec_1 = ((num_seconds % 60) % 10);
//calculate tenth place on second
sec_2 = ((num_seconds % 60) / 10);
//create the argument and push to led
arg = (min_2 << Minite_tenth_offset) | (min_1 << Minite_fisrt_offset) | (sec_2 << Second_tenth_offset) | (sec_1) | LED_display_mask;
ioctl(fd, TUX_SET_LED, arg);
}
#if (TEST_INPUT_DRIVER == 1)
int
main ()
{
cmd_t last_cmd = CMD_NONE;
cmd_t cmd;
static const char* const cmd_name[NUM_COMMANDS] = {
"none", "right", "left", "up", "down",
"move left", "enter", "move right", "typed command", "quit"
};
/* Grant ourselves permission to use ports 0-1023 */
if (ioperm (0, 1024, 1) == -1) {
perror ("ioperm");
return 3;
}
init_input ();
while (1) {
while ((cmd = get_tux_command ()) == last_cmd);
last_cmd = cmd;
printf ("command issued: %s\n", cmd_name[cmd]);
if (cmd == CMD_QUIT)
break;
display_time_on_tux (83);
}
shutdown_input ();
return 0;
}
#endif