forked from wtlyu/GPArrayController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGPIOController.cpp
More file actions
147 lines (118 loc) · 3.92 KB
/
Copy pathGPIOController.cpp
File metadata and controls
147 lines (118 loc) · 3.92 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// 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;
jetsonTX1GPIONumber IO1 = gpio37 ;
jetsonTX1GPIONumber IO2 = gpio36 ;
gpioExport(IO2) ;
gpioExport(IO1) ;
gpioSetDirection(IO1,outputPin) ;
gpioSetDirection(IO2,outputPin) ;
/* cout << "Power On" << endl;
gpioSetValue(IO2, on);
usleep(3000000); // sleep for a millisecond
gpioSetValue(IO2, low);
cout << "Mode" << endl;
usleep(1500000); // sleep for a millisecond
gpioSetValue(IO2, on);
usleep(1500000); // sleep for a millisecond
gpioSetValue(IO2, low);
cout << "Enter" << endl;
usleep(1500000); // sleep for a millisecond
gpioSetValue(IO1, on);
usleep(1500000); // sleep for a millisecond
gpioSetValue(IO1, low);
while(getkey() != 27) {
usleep(1000); // sleep for a millisecond
gpioSetValue(IO1, off);
}
*/
char t;
while((t = getkey()) != 27) {
if (t == ' '){
gpioSetValue(IO1, on);
usleep(1500000); // sleep for a millisecond
gpioSetValue(IO1, low);
}
usleep(1000); // sleep for a millisecond
}
cout << "GPIO example finished." << endl;
gpioUnexport(IO1); // unexport the LED
gpioUnexport(IO2); // unexport the push button
/*
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;
}