-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKeyboard.cpp
More file actions
229 lines (201 loc) · 5.37 KB
/
Keyboard.cpp
File metadata and controls
229 lines (201 loc) · 5.37 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <Arduino.h>
#include "Graphics.h"
#include "Lights.h"
#include "Keyboard.h"
// the buttons
Keyboard keyboard;
static const char Keyboard::_labels[] PROGMEM =
"VERB\0"
"NOUN\0"
"+\0"
"-\0"
"0\0"
"7\0"
"4\0"
"1\0"
"8\0"
"5\0"
"2\0"
"9\0"
"6\0"
"3\0"
"CLR\0"
"PRO\0"
"KEY\nREL\0"
"ENTR\0"
"RSET\0\0";
void Keyboard::Init()
{
m_iPrevReading = lastButton;
m_iPrevState = noButton;
m_iTransitionTimeMS = millis();
}
void Keyboard::Draw()
{
// draw all the buttons
for (int btn = keyboard.firstButton; btn < keyboard.lastButton; btn++)
DrawButton(btn);
}
void Keyboard::GetButtonXY(int btn, int& x, int& y)
{
// get the top-left corner coords of the given button
if (btn > NOUN) btn++;
// col 0 is leftmost, row 0 is bottom
// col 0 & 6 are special, buttons at rows 0 & 1 but up half a row
int col = btn / 3;
int row = 2 - (btn % 3);
x = Config::ButtonOffsetLeft + col*(Config::ButtonWidth + Config::ButtonGapX);
y = Config::DisplayHeight - (Config::ButtonOffsetBottom + (row + 1)*Config::ButtonHeight + row*Config::ButtonGapY);
if (!(col % (Config::ButtonsAcross - 1)))
{
y += (Config::ButtonHeight + Config::ButtonGapY)/2;
}
}
void Keyboard::DrawButton(int btn, bool pressed)
{
// draw just the given button, including label
int x, y, w = Config::ButtonWidth, h = Config::ButtonHeight;
GetButtonXY(btn, x, y);
int offset = 1;
if (pressed)
{
// flash it
if (Config::_InputFlags & Config::MaskFlash)
{
lcd.fillColour(lcd.beginFill(x, y, w, h), Config::BackgroundColour);
delay(25);
}
else
{
// clear any pixels from un-pressed image
lcd.fillColour(lcd.beginFill(x, y, offset + 1, h), Config::BackgroundColour);
lcd.fillColour(lcd.beginFill(x, y, w, offset + 1), Config::BackgroundColour);
}
// draw btn offset left and down
x += offset;
y += offset;
}
else
{
// clear any pixels from pressed image
lcd.fillColour(lcd.beginFill(x + w - offset, y, offset + 1, h), Config::BackgroundColour);
lcd.fillColour(lcd.beginFill(x, y + h - offset, w, offset + 1), Config::BackgroundColour);
}
unsigned long fill = lcd.beginFill(x, y, w, h);
// rounded corners
lcd.fillColour(1, Config::BackgroundColour);
lcd.fillByte(w - 2, 0x00);
lcd.fillColour(1, Config::BackgroundColour);
lcd.fillByte(fill - 2*w, 0x00);
lcd.fillColour(1, Config::BackgroundColour);
lcd.fillByte(w - 2, 0x00);
lcd.fillColour(1, Config::BackgroundColour);
char* pLabel = graphics.LookupLabel(btn, _labels);
graphics.DrawLabel(x + w/2, y + h/2, pLabel, Config::KeyColour, Config::ForegroundColour, (strlen(pLabel) == 1)?2:1);
}
int Keyboard::TouchedButton()
{
// get the btn currently touched, or noButton
int tx, ty;
if (lcd.getTouch(tx, ty))
{
if (lights.Touched(tx, ty) == Lights::SystemAccess)
return Keyboard::SystemAccess;
for (int btn = keyboard.firstButton; btn < keyboard.lastButton; btn++)
{
int bx1, by1, bx2, by2;
GetButtonXY(btn, bx1, by1);
bx2 = bx1 + Config::ButtonWidth;
by2 = by1 + Config::ButtonHeight;
if (bx1 <= tx && tx <= bx2 && by1 <= ty && ty <= by2)
return btn;
}
}
return noButton;
}
void Keyboard::WaitForDown(int& tx, int& ty)
{
// wait for touch down, with semi-debounce
int tx1, ty1;
while (true)
{
while (!lcd.getTouch(tx1, ty1))
;
delay(10);
while (!lcd.getTouch(tx, ty))
;
if (abs(tx-tx1) < 5 && abs(ty-ty1) < 5)
break;
}
}
void Keyboard::WaitForUp()
{
int tx, ty;
while (lcd.getTouch(tx, ty))
;
delay(10);
while (lcd.getTouch(tx, ty))
;
}
#define HOLD_TIME_MS 50L
bool Keyboard::CheckButtonPress(int& btn)
{
// get a debounced btn press, true if touching, btn filled in
if (m_UnGotButton != noButton)
{
btn = m_UnGotButton;
m_UnGotButton = noButton;
return true;
}
// debounced button, true if button pressed
int ThisReading = TouchedButton();
if (ThisReading != m_iPrevReading)
{
// state change, reset the timer
m_iPrevReading = ThisReading;
m_iTransitionTimeMS = millis();
}
else if (ThisReading != m_iPrevState &&
(millis() - m_iTransitionTimeMS) >= HOLD_TIME_MS)
{
// a state other than the last one and held for long enough
btn = m_iPrevState = ThisReading;
if (btn < lastButton)
{
// yes, pressed
DrawButton(btn, true);
WaitForUp();
DrawButton(btn, false);
return true;
}
else if (btn == SystemAccess)
{
// special case, clicking on a lamp to get system access
lights.DrawIndicator(Lights::SystemAccess, true);
WaitForUp();
lights.DrawIndicator(Lights::SystemAccess, false);
return true;
}
}
return false;
}
static const char* _charMap = " +-0741852963 ";
bool Keyboard::IsDigit(int btn)
{
return ZERO <= btn && btn <= THREE;
}
bool Keyboard::AsDigit(int btn, char& digit)
{
// returns the digit corresponding to the btn
if (IsDigit(btn))
{
digit = _charMap[btn];
return true;
}
return false;
}
void Keyboard::UnGetButton(int btn)
{
// "push" the btn so it will be processed next
m_UnGotButton = btn;
}