-
Notifications
You must be signed in to change notification settings - Fork 0
/
pin_tester.c
57 lines (57 loc) · 1.37 KB
/
pin_tester.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
#include <stdio.h>
#include <stdlib.h>
//TODO -- add reader, ls gpio, ...
int main(int argc, char** argv) {
FILE *f;
char s[256], tmp[256];
//Loop
while (1) {
printf("Command: ");
scanf("%s", s);
if (strcmp(s, "quit") == 0) {
break;
} else if (strcmp(s, "open") == 0) {
//open port
f = fopen("/sys/class/gpio/export", "w");
if (f != NULL) {
scanf("%s", s); //read in port number
fputs(s, f); //exports pin
fclose(f);
printf("success\n");
}
} else if (strcmp(s, "close") == 0) {
//close port
f = fopen("/sys/class/gpio/unexport", "w");
if (f != NULL) {
scanf("%s", s); //read in port number
fputs(s, f); //unexports pin
fclose(f);
printf("success\n");
}
} else if (strcmp(s, "dir") == 0) {
//set direction of port
scanf("%s", s); //get port
sprintf(tmp, "/sys/class/gpio/gpio%s/direction", s);
f = fopen(tmp, "w");
if (f != NULL) {
scanf("%s", s);
fputs(s, f); //set direction of pin
fclose(f);
printf("success\n");
}
} else if (strcmp(s, "val") == 0) {
//set value of port
scanf("%s", s);
sprintf(tmp, "/sys/class/gpio/gpio%s/value", s);
f = fopen(tmp, "w");
if (f != NULL) {
scanf("%s", s);
fputs(s, f); //set value of pin
fclose(f);
printf("success\n");
}
}
}
printf("exiting\n");
return 0;
}