-
Notifications
You must be signed in to change notification settings - Fork 5
/
gpioRead.c
76 lines (59 loc) · 1.24 KB
/
gpioRead.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <ugpio/ugpio.h>
int main(int argc, char **argv, char **envp)
{
int i;
int rq, rv;
int gpio;
int value;
if (argc < 2) {
printf("Usage: gpioRead <gpio>\n\n");
printf("Reads the input value of the specified GPIO pin once a second for 20 seconds\n");
exit(-1);
}
gpio = atoi(argv[1]);
// check if gpio is already exported
if ((rq = gpio_is_requested(gpio)) < 0)
{
perror("gpio_is_requested");
return EXIT_FAILURE;
}
// export the gpio
if (!rq) {
printf("> exporting gpio\n");
if ((rv = gpio_request(gpio, NULL)) < 0)
{
perror("gpio_request");
return EXIT_FAILURE;
}
}
// set to input direction
printf("> setting to input\n");
if ((rv = gpio_direction_input(gpio)) < 0)
{
perror("gpio_direction_input");
}
// read the gpio 20 times
printf("> begin reading GPIO%d\n",gpio);
for (i = 0; i < 20; i++)
{
// read the gpio
value = gpio_get_value(gpio);
printf(" > Read GPIO%d: value '%d'\n", gpio, value);
// pause between each read
sleep(1);
}
// unexport the gpio
if (!rq) {
printf("> unexporting gpio\n");
if (gpio_free(gpio) < 0)
{
perror("gpio_free");
}
}
return 0;
}