forked from jetsonhacks/jetsonGPIO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexampleGPIOApp.cpp
67 lines (55 loc) · 1.87 KB
/
exampleGPIOApp.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
// exampleApp.c
#include <iostream>
#include <string>
#include <unistd.h>
#include "jetsonGPIO.h"
using namespace std;
int main(int argc, char *argv[]){
cout << "Testing the GPIO Pins" << endl;
jetsonGPIO redLED = gpio165 ;
jetsonGPIO pushButton = gpio166 ;
// Make the button and led available in user space
gpioExport(pushButton) ;
gpioExport(redLED) ;
gpioSetDirection(pushButton,inputPin) ;
gpioSetDirection(redLED,outputPin) ;
// Reverse the button wiring; this is for when the button is wired
// with a pull up resistor
// gpioActiveLow(pushButton, true);
// Flash the LED 5 times
for(int i=0; i<5; i++){
cout << "Setting the LED on" << endl;
gpioSetValue(redLED, on);
usleep(200000); // on for 200ms
cout << "Setting the LED off" << endl;
gpioSetValue(redLED, off);
usleep(200000); // off for 200ms
}
// Wait for the push button to be pressed
cout << "Please press the button!" << endl;
unsigned int value = low;
int ledValue = low ;
// Turn off the LED
gpioSetValue(redLED,low) ;
for (int i = 0 ; i < 10000 ; i++) {
gpioGetValue(pushButton, &value) ;
// Useful for debugging
// cout << "Button " << value << endl;
if (value==high && ledValue != high) {
// button is pressed ; turn the LED on
ledValue = high ;
gpioSetValue(redLED,on) ;
} else {
// button is *not* pressed ; turn the LED off
if (ledValue != low) {
ledValue = low ;
gpioSetValue(redLED,off) ;
}
}
usleep(1000); // sleep for a millisecond
}
cout << "GPIO example finished." << endl;
gpioUnexport(redLED); // unexport the LED
gpioExport(pushButton); // unexport the push button
return 0;
}