-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path07_printJoystickActions.cpp
122 lines (104 loc) · 3.91 KB
/
07_printJoystickActions.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* 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 <console_io.h>
#include <sensehat.h>
using namespace std;
int main() {
const rgb_pixel_t red = {.color = {255, 0, 0}};
const rgb_pixel_t orange = {.color = {255, 128, 0}};
const rgb_pixel_t yellow = {.color = {255, 255, 0}};
const rgb_pixel_t green = {.color = {0, 255, 0}};
const rgb_pixel_t cyan = {.color = {0, 255, 255}};
const rgb_pixel_t blue = {.color = {0, 0, 255}};
const rgb_pixel_t purple = {.color = {255, 0, 255}};
const rgb_pixel_t pink = {.color = {255, 128, 128}};
const rgb_pixel_t rainbow[8] = {red, orange, yellow, green,
cyan, blue, purple, pink};
rgb_pixel_t pix;
int row, col, index;
int event_count, click;
stick_t joystick;
if (senseInit()) {
cout << "-------------------------------" << endl
<< "Sense Hat initialization Ok." << endl;
senseClear();
// light up first pixel
row = col = index = 3;
pix = rainbow[index];
senseSetPixel(row, col, pix.color[_R], pix.color[_G], pix.color[_B]);
click = 0;
cout << "Waiting for 60 joystick events" << endl;
event_count = 0;
do {
// blocking function call
joystick = senseWaitForJoystick();
click++;
if (click % 4 == 0) {
cout << "Event number " << event_count + 1 << endl;
event_count++;
// Turn off previous pixel
senseSetPixel(row, col, 0, 0, 0);
// Push to change color index
if (joystick.action == KEY_ENTER &&
joystick.state == KEY_RELEASED) {
index++;
if (index == SENSE_LED_WIDTH) index = 0;
pix = rainbow[index];
}
// Activate the pixel above
if (joystick.action == KEY_UP &&
joystick.state == KEY_RELEASED) {
row--;
if (row == 0) row = 7;
}
// Activate the pixel below
if (joystick.action == KEY_DOWN &&
joystick.state == KEY_RELEASED) {
row++;
if (row == SENSE_LED_WIDTH) row = 0;
}
// Activate the pixel on the left
if (joystick.action == KEY_LEFT &&
joystick.state == KEY_RELEASED) {
col--;
if (col == 0) col = 7;
}
// Activate the pixel on the right
if (joystick.action == KEY_RIGHT &&
joystick.state == KEY_RELEASED) {
col++;
if (col == SENSE_LED_WIDTH) col = 0;
}
// Turn on new pixel
senseSetPixel(row, col, pix.color[_R], pix.color[_G],
pix.color[_B]);
}
} while (event_count < 60);
cout << endl << "Waiting for keypress." << endl;
getch();
senseShutdown();
cout << "-------------------------------" << endl
<< "Sense Hat shut down." << endl;
}
return EXIT_SUCCESS;
}