-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path08_gpioInput.cpp
More file actions
73 lines (63 loc) · 1.93 KB
/
08_gpioInput.cpp
File metadata and controls
73 lines (63 loc) · 1.93 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
/* File: 08_gpioInput.cpp
* Author: Philippe Latu
* Source: https://github.com/platu/libsensehat-cpp
*
* This example program illustrates the gpioSetOutput() function which sets a
* GPIO pin on or off.
*
* Function prototypes:
*
* bool gpioSetConfig(unsigned int pin, gpio_dir_t direction);
* GPIO pin number -^ in/out -^
*
* int gpioGetInput(unsigned int pin);
* ^- read val GPIO pin number -^
*
* The program counts 10 events from input pin number
* Available GPIO pin numbers: 5, 6, 16, 17, 22, 26, 27
*
* _GPIO_PIN_----_push_button_----_4.7k_resistor_----> 3.3V
*
*/
#include <iostream>
#include <iomanip>
#include <chrono>
#include <thread>
#include <sensehat.h>
#define NB_EV 10
using namespace std;
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // system_clock, milliseconds
int main(int argc, char **argv) {
int opt, prev, val;
char *eptr;
unsigned int count, pin = 5;
// command line arguments: -p 27 for pin 27
while ((opt = getopt(argc, argv, "p:")) != -1) {
if (opt == 'p')
pin = strtoul(optarg, &eptr, 10);
else
cerr << "Usage: " << argv[0] << " [-p] GPIO pin number." << endl;
}
if (senseInit()) {
cout << "-------------------------------" << endl
<< "Sense Hat initialization Ok." << endl;
if (gpioSetConfig(pin, in)) {
prev = 1;
count = 0;
do {
val = gpioGetInput(pin);
if (val != prev) {
cout << "GPIO pin: " << pin << " -> " << val << endl;
count++;
}
sleep_for(milliseconds(20));
prev = val;
} while (count < NB_EV);
}
senseShutdown();
cout << "-------------------------------" << endl
<< "Sense Hat shut down." << endl;
}
return EXIT_SUCCESS;
}