-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path07_blocking_WaitForJoystick.cpp
More file actions
90 lines (79 loc) · 2.51 KB
/
07_blocking_WaitForJoystick.cpp
File metadata and controls
90 lines (79 loc) · 2.51 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
/* File: 07_WaitForJoystick.cpp
* Author: Philippe Latu
* Source: https://github.com/platu/libsensehat-cpp
*
* This example program illustrates the senseWaitForJoystick() function.
*
* Function prototypes:
*
* stick_t senseWaitForJoystick()
* ^- struct returned
*
* The stick_t struct has three members
* timestamp seconds and microseconds float number
* action KEY_ENTER, KEY_UP, KEY_LEFT, KEY_RIGHT, KEY_DOWN
* state KEY_RELEASED, KEY_PRESSED, KEY_HELD
*
* This program shows that there are many events for a single action.
* The use of this blocking function requires to evaluate a combination of the
* two members of the type stick_t: action and state
*/
#include <iostream>
#include <iomanip>
#include <termios.h>
#include <assert.h>
#include <console_io.h>
#include <sensehat.h>
using namespace std;
int main() {
int event_count;
stick_t joystick;
if (senseInit()) {
cout << "-------------------------------" << endl
<< "Sense Hat initialization Ok." << endl;
senseClear();
cout << "Waiting for 60 joystick events" << endl;
for (event_count = 0; event_count < 60; event_count++) {
// blocking function call
joystick = senseWaitForJoystick();
cout << "Event number " << event_count << " -> ";
// Identify action on stick
switch (joystick.action) {
case KEY_ENTER:
cout << "push ";
break;
case KEY_UP:
cout << "up ";
break;
case KEY_LEFT:
cout << "left ";
break;
case KEY_RIGHT:
cout << "right ";
break;
case KEY_DOWN:
cout << "down ";
break;
}
// Identify state of stick
switch (joystick.state) {
case KEY_RELEASED:
cout << "\treleased";
break;
case KEY_PRESSED:
cout << "\tpressed";
break;
case KEY_HELD:
cout << "\theld";
break;
}
cout << endl;
}
cout << endl << "Waiting for keypress." << endl;
getch();
senseShutdown();
cout << "-------------------------------" << endl
<< "Sense Hat shut down." << endl;
}
return EXIT_SUCCESS;
}