forked from jetsonhacks/jetsonTX1GPIO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpio.cpp
106 lines (87 loc) · 3.02 KB
/
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// exampleApp.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <sys/time.h>
#include <iostream>
#include <string>
#include <unistd.h>
#include "jetsonGPIO.h"
using namespace std;
int getkey() {
int character;
struct termios orig_term_attr;
struct termios new_term_attr;
/* set the terminal to raw mode */
tcgetattr(fileno(stdin), &orig_term_attr);
memcpy(&new_term_attr, &orig_term_attr, sizeof(struct termios));
new_term_attr.c_lflag &= ~(ECHO|ICANON);
new_term_attr.c_cc[VTIME] = 0;
new_term_attr.c_cc[VMIN] = 0;
tcsetattr(fileno(stdin), TCSANOW, &new_term_attr);
/* read a character from the stdin stream without blocking */
/* returns EOF (-1) if no character is available */
character = fgetc(stdin);
/* restore the original terminal attributes */
tcsetattr(fileno(stdin), TCSANOW, &orig_term_attr);
return character;
}
int main(int argc, char *argv[]){
cout << "Testing the GPIO Pins" << endl;
jetsonTX2GPIONumber J21_pin37 = gpio388; // Ouput
jetsonTX2GPIONumber J21_pin31 = gpio298; // Ouput
while (1) {
gpioSetValue(J21_pin37, on);
gpioSetValue(J21_pin31, on);
gpioSetValue(J21_pin37, off);
gpioSetValue(J21_pin31, off);
}
/*jetsonTX1GPIONumber redLED = gpio219 ; // Ouput
jetsonTX1GPIONumber pushButton = gpio38 ; // Input
// 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! ESC key quits the program" << endl;
unsigned int value = low;
int ledValue = low ;
// Turn off the LED
gpioSetValue(redLED,low) ;
while(getkey() != 27) {
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
// gpioUnexport(pushButton); // unexport the push button
return 0;
}