-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyword_test.c
103 lines (92 loc) · 2.73 KB
/
keyword_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
/****************************************************************************
* A small program that enables the keypad input without pressing enter which
* can be used in a smaller robotic projects for wireless interface and motion
* with keypad.
-Rahul Sharma
-Inkers Technology Pvt Ltd
****************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<termios.h>
#include<unistd.h>
struct termios oldtermios;
//Important commands referenced from http://man7.org/linux/man-pages/man3/termios.3.html
int ttyraw(int fd)
{
struct termios newtermios;
if(tcgetattr(fd, &oldtermios) < 0)
return(-1);
newtermios = oldtermios;
newtermios.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
newtermios.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
newtermios.c_cflag &= ~(CSIZE | PARENB);
newtermios.c_cflag |= CS8;
/* Set 8 bits per character. */
newtermios.c_oflag &= ~(OPOST);
/* This includes things like expanding tabs to spaces. */
newtermios.c_cc[VMIN] = 1;
newtermios.c_cc[VTIME] = 0;
/* The change occurs after all output written to the file descriptor has been transmitted, and all input so far received but not read is discarded before the change is made. */
if(tcsetattr(fd, TCSAFLUSH, &newtermios) < 0)
return(-1);
return(0);
}
int ttyreset(int fd)
{
if(tcsetattr(fd, TCSAFLUSH, &oldtermios) < 0)
return(-1);
return(0);
}
void sigcatch(int sig)
{
ttyreset(0);
exit(0);
}
int i;
char c;
int main()
{
if(ttyraw(0) < 0)
{
fprintf(stderr,"Can't go to raw mode.\n");
exit(1);
}
while(1){
//keypad input
if((i=read(0, &c, 1)) == 1){
if((c&=255)==3){
break;
}
else if((c&=255)=='a'){
printf("Automode Enabled\n\r");
}
else if((c&=255)=='m'){
printf("Manual Mode Enabled\n\r");
}
else if((c&=255)==65){ //up arrow key
printf("Forward\n\r");
}
else if((c&=255)==66){ //back arrow key
printf("backword\n\r");
}
else if((c&=255)==68){ //left arrow key
printf("Left\n\r");
}
else if((c&=255)==67){ //right arrow key
printf("Right\n\r");
}
}
}
if(ttyreset(0) < 0)
{
fprintf(stderr, "Cannot reset terminal!\n");
exit(-1);
}
if( i < 0)
{
fprintf(stderr,"Read error.\n");
exit(-1);
}
printf("Exit Keyboard interface\n");
return 0;
}