-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path01_getRGBpixel.cpp
More file actions
129 lines (111 loc) · 4.16 KB
/
01_getRGBpixel.cpp
File metadata and controls
129 lines (111 loc) · 4.16 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
/* File: 01_getRGBpixel.cpp
* Author: Philippe Latu
* Source: https://github.com/platu/libsensehat-cpp
*
* This example program illustrates the senseGetRGBpixel() function that
* reads one single pixel encoded in an array of three 8 bit integers.
* The three integers represent the R(ed), G(reen), and B(lue) colors.
* Here we use a dedicated type named rgb_pixel_t.
*
* Function prototype:
*
* rgb_pixel_t senserGetRGBpixel(int, int);
* ^-- pixel color x -^ y -^
*
* The program starts by filling colors on the Sense Hat LED matrix,
* then the user is asked to choose one row and one column to read the color
* value of this pixel.
*/
#include <assert.h>
#include <termios.h>
#include <iomanip>
#include <iostream>
#include <limits>
#include <sensehat.h>
using namespace std;
// Function to capture a single keypress from the user
int getch() {
int c = 0;
struct termios org_opts, new_opts;
int res = 0;
//----- store current settings -------------
res = tcgetattr(STDIN_FILENO, &org_opts);
assert(res == 0);
//----- set new terminal parameters --------
memcpy(&new_opts, &org_opts, sizeof(new_opts));
new_opts.c_lflag &= (tcflag_t) ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL |
ECHOPRT | ECHOKE | ICRNL);
tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
//------ wait for a single key -------------
c = getchar();
//------ restore current settings- ---------
res = tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);
assert(res == 0);
return c;
}
// Function to check if the entered coordinate is valid
bool isValidCoordinate(int coord) {
bool valid = false;
if (coord >= 0 && coord < 8) {
valid = true;
}
return valid;
}
int main() {
rgb565_pixel_t color, mask = 0xf800;
rgb_pixel_t readpx;
int x, y;
int row, col;
if (senseInit()) {
cout << "-------------------------------" << endl
<< "Sense Hat initialization Ok." << endl;
senseClear();
// Based on the initial color value of mask, we fill the LED matrix
for (y = 0; y < 8; y++) {
color = mask;
for (x = 0; x < 8; x++) {
senseSetRGB565pixel(x, y, color);
// We shift color to get the next color in RGB565 format
color <<= 1;
}
mask >>= 2;
}
// Next we ask the user to enter pixel row and column
do {
cout << "Enter both the row and column numbers (0-7) of the pixel "
"to be read: ";
cin >> row >> col;
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (!isValidCoordinate(row)) {
cout << "Invalid row number. Please enter a value between 0 "
"and 7."
<< endl;
}
if (!isValidCoordinate(col)) {
cout << "Invalid column number. Please enter a value between 0 "
"and 7."
<< endl;
}
} while (!isValidCoordinate(row) || !isValidCoordinate(col));
// Read the pixel in RGB format
readpx = senseGetRGBpixel(col, row);
cout << "Here is the color encoded in RGB format." << endl
<< "Hexadecimal:\t0x" << hex << setw(2) << setfill('0')
<< (uint16_t)readpx.color[_R] << ", 0x" << setw(2) << setfill('0')
<< (uint16_t)readpx.color[_G] << ", 0x" << setw(2) << setfill('0')
<< (uint16_t)readpx.color[_B] << endl
<< "Decimal:\t" << dec << setw(3) << (uint16_t)readpx.color[_R]
<< ", " << setw(3) << (uint16_t)readpx.color[_G] << ", " << setw(3)
<< (uint16_t)readpx.color[_B] << endl;
senseClear();
// We set back the read pixel to verify
senseSetRGBpixel(col, row, readpx.color[_R], readpx.color[_G],
readpx.color[_B]);
cout << endl << "Waiting for keypress." << endl;
getch();
senseShutdown();
cout << "-------------------------------" << endl
<< "Sense Hat shut down." << endl;
}
return EXIT_SUCCESS;
}