-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path11_arrowKeys.cpp
More file actions
134 lines (112 loc) · 4.35 KB
/
11_arrowKeys.cpp
File metadata and controls
134 lines (112 loc) · 4.35 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
/* File: 11_arrowKeys.cpp
* Author: Philippe Latu
* Source: https://github.com/platu/libsensehat-cpp
*
* This example program illustrates the use of the console arrow keys.
*
* These functions are not part of the Sense Hat library. They are intended to
* be used by students who wish to drive a robot with the arrow keys of the
* console.
*/
#include <iostream>
#include <thread> // sleep_for, sleep_until
#include <sensehat.h>
#include <console_io.h>
using namespace std;
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono; // system_clock, milliseconds
// Define display zones
const int STATUS_ROW = 10;
const int ACTION_ROW = 12;
// Main program
int main() {
bool stop = false;
int key_count = 0;
char c;
if (senseInit()) {
std::cout << "-------------------------------" << std::endl
<< "Sense Hat initialization Ok." << std::endl;
// Console initialization
clearConsole();
// Display header and instructions
gotoxy(1, 1);
std::cout << "========================================" << std::endl;
std::cout << " Arrow Keys Detection Demo " << std::endl;
std::cout << "========================================" << std::endl;
gotoxy(1, 5);
std::cout << "Instructions:" << std::endl;
std::cout << " - Use arrow keys to navigate" << std::endl;
std::cout << " - Press 'Q' to quit" << std::endl;
std::cout << "----------------------------------------" << std::endl;
// Display initial status
gotoxy(1, STATUS_ROW);
std::cout << "Status: Waiting for input...";
gotoxy(1, ACTION_ROW);
std::cout << "Action: ";
std::cout << std::flush;
// Main task
do {
// Get the number of keys in the keyboard buffer
key_count = keypressed();
// Print the key ascii code if a single key is pressed
if (key_count == 1) {
c = std::cin.get();
// Clear and update status line
gotoxy(1, STATUS_ROW);
clearEOL();
std::cout << "Status: Key pressed";
// Clear and display action
gotoxy(1, ACTION_ROW);
clearEOL();
std::cout << "Action: Key = [" << c
<< "] (ASCII: " << static_cast<int>(c) << ")";
std::cout << std::flush;
// Stop the program if 'q' is pressed
if (toupper(c) == 'Q') {
stop = true;
gotoxy(1, ACTION_ROW);
clearEOL();
std::cout << "Action: Quitting..." << std::flush;
}
}
// Detect arrow keys if the key count is greater than 1
else if (key_count > 1) {
// Clear and update status line
gotoxy(1, STATUS_ROW);
clearEOL();
std::cout << "Status: Arrow key detected";
// Clear and display action
gotoxy(1, ACTION_ROW);
clearEOL();
std::cout << "Action: ";
switch (getArrowKey()) {
case UP:
std::cout << "↑ UP - Accelerate";
break;
case DOWN:
std::cout << "↓ DOWN - Slow down";
break;
case RIGHT:
std::cout << "→ RIGHT - Turn right";
break;
case LEFT:
std::cout << "← LEFT - Turn left";
break;
default:
std::cout << "? Unknown key sequence";
break;
}
std::cout << std::flush;
}
sleep_until(system_clock::now() + milliseconds(20));
} while (!stop);
// Clean exit display
gotoxy(1, 14);
std::cout << "========================================" << std::endl;
senseShutdown();
std::cout << "Sense Hat shut down." << std::endl;
std::cout << "========================================" << std::endl;
gotoxy(1, 17); // Position cursor after output
}
return EXIT_SUCCESS;
}