-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpiotest.c
52 lines (49 loc) · 1.25 KB
/
gpiotest.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
#include "gpioapi.h"
#include <ctype.h>
int main(int argc, char *argv[]){
gpio node;
int value;
int pin;
int retval;
if (argc < 3){
fprintf(stderr, "Usage: %s [gpio number] [direction] <0,1>\n", argv[0]);
return -1;
}
pin = atoi(argv[1]);
if ((retval = initGPIO(0, pin, &node))){
fprintf(stderr, "error initing - %d\n", retval);
return -1;
}
if (tolower(argv[2][0]) == 'i'){
if ((retval = setGPIO(&node, IN, 0))){
fprintf(stderr, "error setting - %d\n", retval);
return -1;
}
}
else if (tolower(argv[2][0]) == 'o'){
if (argc != 4){
fprintf(stderr, "Error: need a level to set output as\n");
fprintf(stderr, "Usage: %s [gpio number] [direction] <0,1>\n", argv[0]);
return -1;
}
value = atoi(argv[3]);
if ((retval = setGPIO(&node, OUT, value))){
fprintf(stderr, "error setting - %d\n", retval);
return -1;
}
}
else{
fprintf(stderr, "invalid direction: %s\n", argv[2]);
return -1;
}
if ((value = readGPIO(&node)) < 0){
fprintf(stderr, "error reading - %d\n", value);
return -1;
}
printf("%c\n", value);
if ((freeGPIO(&node))){
fprintf(stderr, "error letting go\n");
return -1;
}
return 0;
}