-
Notifications
You must be signed in to change notification settings - Fork 286
/
mraa_gpio.cpp
78 lines (61 loc) · 1.25 KB
/
mraa_gpio.cpp
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
/*
* gpio example in c++ using mraa
*
* Author: Manivannan Sadhasivam <[email protected]>
*
* Usage: Toggles GPIO 23 and 24
*
* Compilation: g++ mraa_gpio.cpp -o bin -lmraa
*
*/
/* standard headers */
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/* mraa header */
#include "mraa.hpp"
/* gpio starts from 23 as per LS header pinout */
#define GPIO_1 23
#define GPIO_2 24
int flag = 1;
using namespace mraa;
void sig_handler(int signum)
{
if (signum == SIGINT) {
fprintf(stdout, "Program interrupted\n");
flag = 0;
}
}
int main(void)
{
Result res;
/* install signal handler */
signal(SIGINT, sig_handler);
/* initialize gpio 23 */
Gpio gpio_1(GPIO_1);
/* initialize gpio 24 */
Gpio gpio_2(GPIO_2);
/* set gpio 23 to output */
res = gpio_1.dir(DIR_OUT);
if (res != SUCCESS) {
printError(res);
return -1;
}
/* set gpio 24 to output */
res = gpio_2.dir(DIR_OUT);
if (res != SUCCESS) {
printError(res);
return -1;
}
/* toggle both gpio's */
while (flag) {
res = gpio_1.write(1);
res = gpio_2.write(0);
sleep(1);
res = gpio_1.write(0);
res = gpio_2.write(1);
sleep(1);
}
return res;
}