-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrinketHidCombo.cpp
More file actions
328 lines (300 loc) · 7.34 KB
/
TrinketHidCombo.cpp
File metadata and controls
328 lines (300 loc) · 7.34 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
This is the part of the TrinketHidCombo code that is exposed to the user
See the header file for more comments on the functions
Copyright (c) 2013 Adafruit Industries
All rights reserved.
TrinketHidCombo is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
TrinketHidCombo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with TrinketHidCombo. If not, see
<http://www.gnu.org/licenses/>.
*/
#include "cmdline_defs.h"
#include "TrinketHidCombo.h"
#include "TrinketHidComboC.h"
#include <stdint.h>
#include <avr/interrupt.h>
#include <util/delay.h>
// create an instance that the user can use
Trinket_Hid_Combo TrinketHidCombo;
// empty constructor
Trinket_Hid_Combo::Trinket_Hid_Combo()
{
// nothing to do
}
// starts the USB driver, causes re-enumeration
void Trinket_Hid_Combo::begin()
{
usbBegin();
}
// this must be called at least once every 10ms
void Trinket_Hid_Combo::poll()
{
usbPollWrapper();
}
// makes a mouse movement
void Trinket_Hid_Combo::mouseMove(signed char x, signed char y, uint8_t buttonMask)
{
signed char * signed_ptr = (signed char *)report_buffer; // this converts signed to unsigned
// format the report structure
signed_ptr[2] = x;
signed_ptr[3] = y;
report_buffer[1] = buttonMask;
report_buffer[0] = REPID_MOUSE;
usbReportSend(REPSIZE_MOUSE);
}
void Trinket_Hid_Combo::pressKey(uint8_t modifiers, uint8_t keycode1)
{
pressKey(modifiers, keycode1, 0, 0, 0, 0);
}
void Trinket_Hid_Combo::pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2)
{
pressKey(modifiers, keycode1, keycode2, 0, 0, 0);
}
void Trinket_Hid_Combo::pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3)
{
pressKey(modifiers, keycode1, keycode2, keycode3, 0, 0);
}
void Trinket_Hid_Combo::pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4)
{
pressKey(modifiers, keycode1, keycode2, keycode3, keycode4, 0);
}
void Trinket_Hid_Combo::pressKey(uint8_t modifiers, uint8_t keycode1, uint8_t keycode2, uint8_t keycode3, uint8_t keycode4, uint8_t keycode5)
{
// construct the report, follow the standard format as described
// this format is compatible with "boot protocol"
report_buffer[1] = modifiers;
report_buffer[2] = 0; // reserved
report_buffer[3] = keycode1;
report_buffer[4] = keycode2;
report_buffer[5] = keycode3;
report_buffer[6] = keycode4;
report_buffer[7] = keycode5;
report_buffer[0] = REPID_KEYBOARD;
usbReportSend(REPSIZE_KEYBOARD);
}
// presses a list of keys, do not exceed 5 keys
void Trinket_Hid_Combo::pressKeys(uint8_t modifiers, uint8_t* keycodes, uint8_t sz)
{
report_buffer[0] = REPID_KEYBOARD;
report_buffer[1] = modifiers;
report_buffer[2] = 0; // reserved
uint8_t i;
for (i = 0; i < sz; i++)
{
report_buffer[3 + i] = keycodes[i];
}
usbReportSend(REPSIZE_KEYBOARD);
}
void Trinket_Hid_Combo::typeChar(uint8_t ascii)
{
uint8_t modifier, keycode;
ASCII_to_keycode(ascii, getLEDstate(), &modifier, &keycode);
pressKey(modifier, keycode);
pressKey(0, 0); // immediately release the key after
}
size_t Trinket_Hid_Combo::write(uint8_t ascii)
{
typeChar(ascii);
return 1;
}
void Trinket_Hid_Combo::pressMultimediaKey(uint8_t key)
{
report_buffer[0] = REPID_MMKEY;
report_buffer[1] = key;
report_buffer[2] = 0;
usbReportSend(REPSIZE_MMKEY);
// immediate release
report_buffer[0] = REPID_MMKEY;
report_buffer[1] = 0;
report_buffer[2] = 0;
usbReportSend(REPSIZE_MMKEY);
}
void Trinket_Hid_Combo::pressSystemCtrlKey(uint8_t key)
{
report_buffer[0] = REPID_SYSCTRLKEY;
report_buffer[1] = key;
usbReportSend(REPSIZE_SYSCTRLKEY);
// immediate release
report_buffer[0] = REPID_SYSCTRLKEY;
report_buffer[1] = key;
usbReportSend(REPSIZE_SYSCTRLKEY);
}
void ASCII_to_keycode(uint8_t ascii, uint8_t ledState, uint8_t* modifier, uint8_t* keycode)
{
*keycode = 0x00;
*modifier = 0x00;
// see scancode.doc appendix C
if (ascii >= 'A' && ascii <= 'Z')
{
*keycode = 4 + ascii - 'A'; // set letter
if (bit_is_set(ledState, 1)) // if caps is on
{
*modifier = 0x00; // no shift
}
else
{
*modifier = _BV(1); // hold shift // hold shift
}
}
else if (ascii >= 'a' && ascii <= 'z')
{
*keycode = 4 + ascii - 'a'; // set letter
if (bit_is_set(ledState, 1)) // if caps is on
{
*modifier = _BV(1); // hold shift // hold shift
}
else
{
*modifier = 0x00; // no shift
}
}
else if (ascii >= '0' && ascii <= '9')
{
*modifier = 0x00;
if (ascii == '0')
{
*keycode = 0x27;
}
else
{
*keycode = 30 + ascii - '1';
}
}
else
{
switch (ascii) // convert ascii to keycode according to documentation
{
case '!':
*modifier = _BV(1); // hold shift
*keycode = 29 + 1;
break;
case '@':
*modifier = _BV(1); // hold shift
*keycode = 29 + 2;
break;
case '#':
*modifier = _BV(1); // hold shift
*keycode = 29 + 3;
break;
case '$':
*modifier = _BV(1); // hold shift
*keycode = 29 + 4;
break;
case '%':
*modifier = _BV(1); // hold shift
*keycode = 29 + 5;
break;
case '^':
*modifier = _BV(1); // hold shift
*keycode = 29 + 6;
break;
case '&':
*modifier = _BV(1); // hold shift
*keycode = 29 + 7;
break;
case '*':
*modifier = _BV(1); // hold shift
*keycode = 29 + 8;
break;
case '(':
*modifier = _BV(1); // hold shift
*keycode = 29 + 9;
break;
case ')':
*modifier = _BV(1); // hold shift
*keycode = 0x27;
break;
case '~':
*modifier = _BV(1); // hold shift
// fall through
case '`':
*keycode = 0x35;
break;
case '_':
*modifier = _BV(1); // hold shift
// fall through
case '-':
*keycode = 0x2D;
break;
case '+':
*modifier = _BV(1); // hold shift
// fall through
case '=':
*keycode = 0x2E;
break;
case '{':
*modifier = _BV(1); // hold shift
// fall through
case '[':
*keycode = 0x2F;
break;
case '}':
*modifier = _BV(1); // hold shift
// fall through
case ']':
*keycode = 0x30;
break;
case '|':
*modifier = _BV(1); // hold shift
// fall through
case '\\':
*keycode = 0x31;
break;
case ':':
*modifier = _BV(1); // hold shift
// fall through
case ';':
*keycode = 0x33;
break;
case '"':
*modifier = _BV(1); // hold shift
// fall through
case '\'':
*keycode = 0x34;
break;
case '<':
*modifier = _BV(1); // hold shift
// fall through
case ',':
*keycode = 0x36;
break;
case '>':
*modifier = _BV(1); // hold shift
// fall through
case '.':
*keycode = 0x37;
break;
case '?':
*modifier = _BV(1); // hold shift
// fall through
case '/':
*keycode = 0x38;
break;
case ' ':
*keycode = 0x2C;
break;
case '\t':
*keycode = 0x2B;
break;
case '\n':
*keycode = 0x28;
break;
}
}
}
uint8_t Trinket_Hid_Combo::getLEDstate()
{
return led_state;
}
// checks if USB is connected, 0 if not connected
char Trinket_Hid_Combo::isConnected()
{
return usb_hasCommed;
}