-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyKbdRptParser.cpp
307 lines (244 loc) · 7.51 KB
/
MyKbdRptParser.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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
Copyright 2021, 2024 m4mmon
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "MyKbdRptParser.h"
// -----------------------------------------------------------------------------
MyKbdRptParser::MyKbdRptParser():
KeyboardReportParser(),
_nConsumerKeySent(0),
_nErrLEDAnimation(0),
_nAnimStartTime(0),
_nComposeBlink(0)
{
kbdLockingKeys.bLeds = -1;
}
// -----------------------------------------------------------------------------
void MyKbdRptParser::Parse(USBHID * ipHid, bool bIsRptId, uint8_t inLen, uint8_t * ipData)
{
#if KBD_INPUT_TRACE
Serial.print("## PHYSICAL KEYBOARD");
for (int idx = 0; idx < inLen; idx++)
{
Serial.print(" ");
PrintHex<uint8_t>(ipData[idx], 0x80);
}
Serial.println();
#endif
// finish Consumer transaction if any
SendConsumerReleaseReportToComputer();
// On error - return
if (ipData[2] == 1)
{
Serial.println("buffer error\r\n");
StartErrorAnim();
return;
}
if (inLen != 8)
{
Serial.println("unhandled length");
StartErrorAnim();
return;
}
// if no manipulation occurred
if (!HandleSpecialSingleKeyPressed(ipHid, inLen, ipData))
{
// send report to computer
SendKeyboardReportToComputer(ipData, inLen);
}
else
{
StartComposeAnim();
}
#if KBD_INPUT_TRACE
Serial.print("## SENDING ===>");
for (int idx = 0; idx < inLen; idx++)
{
Serial.print(" ");
PrintHex<uint8_t>(ipData[idx], 0x80);
}
Serial.println();
#endif
SendKeyboardReportToComputer(ipData, inLen);
}
// -----------------------------------------------------------------------------
uint8_t MyKbdRptParser::InitLedStates(USBHID* ipHid, uint8_t inLeds)
{
// default, indicate locking LEDs state
kbdLockingKeys.bLeds = inLeds;
// error animation
if (_nErrLEDAnimation)
{
// get elapsed time since beginning of animation
unsigned long delta = millis() - _nAnimStartTime;
// stop after 200ms
if (delta > 200)
{
EndErrorAnim();
}
else if (delta > 150)
{
inLeds = 0x00;
}
else if (delta > 100)
{
inLeds = 0xFF;
}
else if (delta > 50)
{
inLeds = 0x00;
}
else
{
inLeds = 0xFF;
}
}
// compose blink animation
else if (_nComposeBlink)
{
// get elapsed time since beginning of animation
unsigned long delta = millis() - _nAnimStartTime;
// stop after 0ms
if (delta > 50)
{
EndComposeAnim();
}
else
{
// switch on compose LED
inLeds = kbdLockingKeys.bLeds | 0x08;
}
}
// update LEDs
return SetKeyboardLeds(ipHid, inLeds);
}
// -----------------------------------------------------------------------------
int MyKbdRptParser::HandleSpecialSingleKeyPressed(USBHID * ipHid, uint8_t inLen, uint8_t * ipData)
{
int nHandled = 0;
uint8_t nCode = 0;
int nIdx = 0;
// to avoid having to manage multiple reports to send etc., just perform
// handling if a single (non-modifier) key is pressed.
if (IsSingleKeyPressed(inLen, ipData, nCode, nIdx))
{
// assume we handle it
nHandled = 1;
switch (nCode)
{
case KEY_POWER: // 0x66: // Keyboard Power
SendLock();
break;
case KEY_EXECUTE: // 0x74: // Open
SendExec();
break;
case KEY_HELP: // 0x75: // Help
SendConsumerReportToComputer(
(IsShift(ipData[0])) ? CONSUMER_EMAIL_READER : CONSUMER_BROWSER_HOME
);
break;
case KEY_MENU2: // 0x76: // Props
SendProps();
break;
case KEY_SELECT: // 0x77: // Front
SendMinMax();
break;
case KEY_STOP: // 0x78: // Stop
SendTaskMgr();
break;
case KEY_AGAIN: // 0x79: // Again
SendRedo();
break;
case KEY_UNDO: // 0x7a: // Undo
SendUndo();
break;
case KEY_CUT: // 0x7b: // Cut
SendCut();
break;
case KEY_COPY: // 0x7c: // Copy
SendCopy();
break;
case KEY_PASTE: // 0x7d: // Paste
SendPaste();
break;
case KEY_FIND: // 0x7e: // Find
SendFind();
break;
case KEY_MUTE: // 0x7f: // Keyboard Mute
SendConsumerReportToComputer(MEDIA_VOLUME_MUTE);
break;
case KEY_VOLUME_UP: // 0x80: // Keyboard Volume Up / shift Brightness Up
SendConsumerReportToComputer(
(IsShift(ipData[0])) ? CONSUMER_BRIGHTNESS_UP : MEDIA_VOLUME_UP
);
break;
case KEY_VOLUME_DOWN: // 0x81: // Keyboard Volume Down / shift Brightness Down
SendConsumerReportToComputer(
(IsShift(ipData[0])) ? CONSUMER_BRIGHTNESS_DOWN : MEDIA_VOLUME_DOWN
);
break;
default:
// oops, not handled
nHandled = 0;
}
}
if (!nHandled)
{
// for each key "slot"
for (int idx = 2; idx <= 7; idx++)
{
// read code
uint8_t nCode = ipData[idx];
if (nCode == KEY_APPLICATION)
{
//Serial.println("Special processing for context menu key\r\n");
ipData[0] |= 0x10;
ipData[idx] = 0;
break;
}
}
}
return nHandled;
}
// -----------------------------------------------------------------------------
int MyKbdRptParser::IsSingleKeyPressed(uint8_t inLen, uint8_t * ipData, uint8_t & onCode, int & onIdx)
{
onCode = 0;
onIdx = 0;
int isSingle = 0;
// if buffer has correct length and first key code does not indicate error
// has been checked in ::Parse
// for each key "slot"
for (int idx = 2; idx <= 7; idx++)
{
// read code
uint8_t nCode = ipData[idx];
// some data
if (nCode != 0)
{
// another key code was previously detected
if (isSingle != 0)
{
// not single then
isSingle = 0;
// forget previously found key info
onCode = 0;
onIdx = 0;
// and don't bother looping again
break;
}
// let's be optimistic
else
{
isSingle = 1;
onCode = nCode;
onIdx = idx;
}
}
}
Serial.print("## IsSingleKeyPressed="); PrintHex<uint8_t>(isSingle, 0x80); Serial.println();
return isSingle;
}
// -----------------------------------------------------------------------------