-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVKeys.cpp
More file actions
169 lines (142 loc) · 5.48 KB
/
VKeys.cpp
File metadata and controls
169 lines (142 loc) · 5.48 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "VKeys.h"
#include <Elegoo_GFX.h> // Core graphics library
#include <Elegoo_TFTLCD.h> // Hardware-specific library
#include <TouchScreen.h>
/**
* @brief Construct a new VKeys::VKeys object
*
* @param style Style of the keyboard. Possible Configurations: QWERTZ, QWERTY, ABCDE
* @param keyColor Color of the keys.
* @param textColor Color of the text on the keys
* @param tScreen Pointer to the touchscreen to be used
*/
VKeys::VKeys (String style, uint16_t keyColor, uint16_t textColor, Elegoo_TFTLCD *tScreen) {
_style = style;
_screen = tScreen;
_keyColor = keyColor;
_textColor = textColor;
_textSize = 4;
_special = false;
// Set rows to passed configuration
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 10; j++) {
if (_style == "QWERTZ") _rows[i][j] = _QWERTZ[i][j];
else if (_style == "QWERTY") _rows[i][j] = _QWERTY[i][j];
else if (_style == "ABCDE") _rows[i][j] = _ABCDE[i][j];
}
}
}
/**
* @brief
* Initializes and draws the keyboard on the touchscreen
*
*/
void VKeys::init (void) {
pinMode(A2, OUTPUT);
pinMode(A3, OUTPUT);
_screen->setTextColor(_textColor);
_screen->setTextSize(_textSize - 1);
// Spacebar, Done and Back
_screen->fillRect(0, _screen->height() - (_kHeight - 1), (_screen->width() / 4) - 1, _kHeight - 1, _keyColor);
_screen->fillRect((_screen->width() / 4), _screen->height() - (_kHeight - 1), 2*(_screen->width() / 4) - 1, _kHeight - 1, _keyColor);
_screen->fillRect(3*(_screen->width() / 4), _screen->height() - (_kHeight - 1), (_screen->width() / 4) - 1, _kHeight - 1, _keyColor);
_screen->setCursor((int16_t)(_screen->width() / 4), (int16_t)(_screen->height() - (_kHeight - 4)));
_screen->setCursor((_screen->width() / 4) + 15, 291);
_screen->print((char*)"SPACE");
_screen->setTextSize(_textSize - 2);
_screen->setCursor(3*(_screen->width() / 4) + 8, 291);
_screen->print((char*)"Send");
_screen->setCursor(8, 291);
_screen->print((char*)"Back");
// Draw the keyboard
_screen->setTextSize(_textSize);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 10; j++) {
// Draw the button
_screen->fillRect(j*(_kWidth + 1), (int) _screen->height() - (4-i)*(_kHeight + 1), _kWidth, _kHeight, _keyColor);
// Set cursor and draw character into current button
_screen->setCursor((int16_t)(j*(_kWidth + 1) +2), (int16_t)((int) _screen->height() - (4-i)*(_kHeight + 1) + 2));
// If special is selected, draw the special characters, otherwise draw alphabetic characters from current config
if (_special) {
if (_specialChars[i][j] != '\0') {
_screen->print(_specialChars[i][j]);
}
} else {
_screen->print(_rows[i][j]);
}
}
}
}
/**
* @brief
* Resets virtual keyboard. Useful if you want to change the color of the keys.
*/
void VKeys::reset (void) {
// Default is QWERTZ
if (_style == "QWERTY") {
_rows[0][5] = 'Y';
_rows[2][1] = 'Z';
} else {
_rows[0][5] = 'Z';
_rows[2][1] = 'Y';
}
}
/**
* @brief
*
* @param point
* @return char
*/
String VKeys::getInputChar(TSPoint point) {
int16_t x,y;
x = point.x;
y = point.y;
return String(getCharFromCoords(x, y));
}
void VKeys::setStyle (String style) {
if (style == "\0") return;
_style = style;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 10; j++) {
if (_style == "QWERTZ") _rows[i][j] = _QWERTZ[i][j];
else if (_style == "QWERTY") _rows[i][j] = _QWERTY[i][j];
else if (_style == "ABCDE") _rows[i][j] = _ABCDE[i][j];
}
}
}
void VKeys::switchKeys() {
_special = !_special;
init();
}
/**************************************************** PRIVATE **************************************************/
/**
* @brief
* Returns selected char by calculating, which column and row was pressed from passed x and y coords
*
* @param x X coordinate from touchscreen selection
* @param y Y coordinate from touchscreen selection
* @return String Selected character parsed to a string, BACK if back button was selected and DONE, if done button was selected
*/
String VKeys::getCharFromCoords(int16_t x, int16_t y) {
//TODO Check, what the hell is returned in this method and why. For Some reason, a fucking 2 is returned at one point.
int row, column;
// Complete Y: [70,920], range 850, keyboard [70,475], keyboard range of 405, 4 rows so 405/4 = 101.25
if (y > 475) return String('\0');
row = (int) ((y - 70)/ 101.25);
// row 0 is spacebar, therefore return escaped space immediately, as nothing else is there.
if (row == 0) {
int tempX = (int)((x - 120) / 3.5);
if (tempX < (_screen->width() / 4)) {
return String("BACK");
} else if (tempX > 3*(_screen->width() / 4)) {
return String("DONE");
}
return String(" ");
}
if (row > 3) return String('2');
// X Range ca. 120 - 920 for 10 columns, therefore 800 / 10 = 80
column = (int) ((x - 120)/ 80);
if (column < 11 && column >= 0 && row < 4 && row >0) return String(_special ? _specialChars[(3-row)][column] : _rows[(3-row)][column]);
// if (column < 11 && column >= 0 && row < 4 && row >0) return String((_special ? _specialChars : _rows)[(3-row)][column]);
return String('-');
}