-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathHIDKeyboard.cpp
More file actions
218 lines (200 loc) · 5.71 KB
/
HIDKeyboard.cpp
File metadata and controls
218 lines (200 loc) · 5.71 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
/****************************************************************************
* HIDKeyboard.cpp
*
* Written by Chris Taylor for SparkFun Electronics, 10/12/12
*
* This code is released on the Beerware License. If you use it to make
* something cool, and you ever happen to meet me, buy me a beer.
*
* For use with the Arduino-keyboard-0.3 ATMega8U2 firmware from Darran.
* For full installation instructions, see README.txt.
*
* Implements functions needed to translate keyboard values into HID
* values and send to the ATMega8U2 on an UNO running the Arduino-keyboard
* firmware.
*
* These functions work by accepting ASCII values and translating them into
* HID values using the HIDTable in HIDKeyboard.h. They then load them into
* an 8-byte report buffer and send them over TX to the ATMega8U2. the format
* of the report buffer is
*
* buf[0] = modifier (value 0 for no modifier)
* buf[1] = reserved
* buf[2] = HID value of key press
* buf[3:7] = reserved
*
* The arduino-keyboard firmware for the ATMega8U2 can be found at
*
* http://hunt.net.nz/users/darran/weblog/b3029/Arduino_UNO_Keyboard_HID_version_03.html
*
* The HID tables and class description can be found at
*
* http://www.usb.org/developers/devclass_docs/Hut1_11.pdf
* http://www.usb.org/developers/devclass_docs/HID1_11.pdf
*
* The keyboard values used in this library can be found on page 53 of the
* HID table.
*
* For full installation instructions of the arduino-keyboard library, please
* see README.txt
*
****************************************************************************/
#include "Arduino.h"
#include "HIDKeyboard.h"
/****************************************************************************
* Generic constructor
*
* Clears buf[] report array
****************************************************************************/
HIDKeyboard::HIDKeyboard()
{
buf[0] = 0;
buf[1] = 0;
buf[2] = 0;
buf[3] = 0;
buf[4] = 0;
buf[5] = 0;
buf[6] = 0;
buf[7] = 0;
}
/****************************************************************************
* begin()
*
* parameters: none
*
* returns: void
*
* Ends any current Serial communication and hijacks to send report buffers
* to the ATMega8U2 at 9600 baud.
****************************************************************************/
void HIDKeyboard::begin()
{
Serial.end();
Serial.begin(9600);
}
/****************************************************************************
* pressKey(modifier, key)
*
* parameters:
* uint8_t modifier -- HID modifier value to send with key
* uint8_t key -- ASCII character value
*
* returns: void
*
* Converts <key> to an HID value, packages with the modifier, and sends
* report.
****************************************************************************/
void HIDKeyboard::pressKey(uint8_t modifier, uint8_t key)
{
this->buf[0] = modifier;
this->buf[2] = HIDTable[key];
Serial.write(this->buf, 8);
}
/****************************************************************************
* pressKey(key)
*
* parameters:
* uint8_t key -- ASCII character value
*
* returns: void
*
* Overloaded from pressKey(modifier, key)
*
* Looks up key's modifier, and calls pressKey(modifier, key);
****************************************************************************/
void HIDKeyboard::pressKey(uint8_t key)
{
pressKey(modifierTable[key], key);
}
/****************************************************************************
* pressSpecialKey(modifier, specialKey)
*
* parameters:
* uint8_t modifier -- HID modifier to send with specialKey
* uint8_t specialKey -- HID table value
*
* returns: void
*
* Packages <modifier> with <specialKey> and sends report
***************************************************************************/
void HIDKeyboard::pressSpecialKey(uint8_t modifier, uint8_t specialKey)
{
this->buf[0] = modifier;
this->buf[2] = specialKey;
Serial.write(this->buf, 8);
}
/****************************************************************************
* pressSpecialKey(specialKey)
*
* parameters:
* uint8_t specialKey -- HID table value
*
* returns: void
*
* Overloaded from pressSpecialKey(modifier, specialKey)
*
* Calls pressSpecialKey(0, specialKey)
****************************************************************************/
void HIDKeyboard::pressSpecialKey(uint8_t specialKey)
{
if(specialKey <= 0x08)
{
pressSpecialKey(specialKey, 0);
}
else
{
pressSpecialKey(0, specialKey);
}
}
/****************************************************************************
* releaseKey()
*
* parameters: none
*
* returns: void
*
* Sends empty report, indicating that no keys are pressed
****************************************************************************/
void HIDKeyboard::releaseKey()
{
this->buf[0] = 0;
this->buf[2] = 0;
Serial.write(this->buf, 8);
}
/****************************************************************************
* print(sequence)
*
* parameters:
* char* sequence -- character string
*
* returns: void
*
* Converts ASCII string <sequence> into HID values and presses the keys
* in order.
****************************************************************************/
void HIDKeyboard::print(char* sequence)
{
int i = 0;
while(sequence[i] != '\0')
{
pressKey(modifierTable[sequence[i]], sequence[i]);
releaseKey();
i++;
}
}
/****************************************************************************
* println(sequence)
*
* parameters:
* char* sequence -- character string
*
* returns: void
*
* Calls print(sequence) followed by an ENTER press and release
****************************************************************************/
void HIDKeyboard::println(char* sequence)
{
print(sequence);
pressSpecialKey(0, ENTER);
releaseKey();
}