forked from djnugent/BLE_WASD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyReport.ino
148 lines (131 loc) · 3.23 KB
/
KeyReport.ino
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
/*
* Functions for manipulating and testing the key report which will be send for either wired
* or wireless use
*/
// Stores the parts of the USB HID key command
KeyReport keyReport;
/**
* Updates the key report given a key press
*
* Up to six keys can be simultaneously pressed plus the modifier keys
*
*/
void report_add(uint8_t hidKey) {
uint8_t i;
if (hidKey >= HID_LEFT_CTRL) {
keyReport.modifiers |= 1 << (hidKey - HID_LEFT_CTRL);
} else if (keyReport.keys[0] != hidKey && keyReport.keys[1] != hidKey
&& keyReport.keys[2] != hidKey && keyReport.keys[3] != hidKey
&& keyReport.keys[4] != hidKey && keyReport.keys[5] != hidKey) {
for (i = 0; i < 6; ++i) {
if (keyReport.keys[i] == 0) {
keyReport.keys[i] = hidKey;
break;
}
}
}
}
boolean is_clear() {
return keyReport.modifiers == 0 && keyReport.keys[0] == 0 && keyReport.keys[1] == 0
&& keyReport.keys[2] == 0 && keyReport.keys[3] == 0
&& keyReport.keys[4] == 0 && keyReport.keys[5] == 0;
}
/**
* Updates the key report given a key release
*/
void report_remove(uint8_t hidKey) {
uint8_t i;
if (hidKey >= HID_LEFT_CTRL) {
keyReport.modifiers &= ~(1 << (hidKey - HID_LEFT_CTRL));
} else {
for (i = 0; i < 6; ++i) {
if (keyReport.keys[i] == hidKey) {
keyReport.keys[i] = 0;
break;
}
}
}
}
/**
* Test modifiers to see if shift is currently pressed
*/
bool is_shift() {
return keyReport.modifiers == 0x02;
}
/**
* Test modifiers to see if control is currently pressed
*/
bool is_control() {
return keyReport.modifiers == 0x01;
}
/**
* Test modifiers to see if control and shift are currently pressed
*/
bool is_control_shift() {
return keyReport.modifiers == 0x03;
}
/**
* Clears all currently set modifiers - not sure if this is really needed
*/
void clear_modifiers() {
keyReport.modifiers = 0;
}
/**
* Clears all currently set modifiers - not sure if this is really needed
*/
void clear_key_report() {
keyReport.modifiers = 0x00;
keyReport.keys[0] = 0x00;
keyReport.keys[1] = 0x00;
keyReport.keys[2] = 0x00;
keyReport.keys[3] = 0x00;
keyReport.keys[4] = 0x00;
keyReport.keys[5] = 0x00;
}
/**
* Gets the key report
*/
KeyReport getKeyReport() {
return keyReport;
}
/**
* Outputs a debug string for the current key report
*/
void log_key_report(KeyReport report) {
#if defined (DEBUG)
String cmd = "0xFD-" + report_to_string(report);
Serial.println(cmd);
#endif
}
/**
* Returns a string representation of a key report
*/
String report_to_string(KeyReport report) {
String cmd = hex_to_str(report.modifiers);
cmd += "-";
cmd += hex_to_str(report.reserved);
cmd += "-";
cmd += hex_to_str(report.keys[0]);
cmd += "-";
cmd += hex_to_str(report.keys[1]);
cmd += "-";
cmd += hex_to_str(report.keys[2]);
cmd += "-";
cmd += hex_to_str(report.keys[3]);
cmd += "-";
cmd += hex_to_str(report.keys[4]);
cmd += "-";
cmd += hex_to_str(report.keys[5]);
return cmd;
}
/**
* Create a two character hex string for debugging
*/
String hex_to_str(uint8_t hidKey) {
String str = String(hidKey, HEX);
if (hidKey < 16) {
str = "0" + str;
}
str.toUpperCase();
return str;
}