-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyboard_functional.h
366 lines (308 loc) · 9.34 KB
/
keyboard_functional.h
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#ifndef H_KEYBOARD_FUNCTIONAL
#define H_KEYBOARD_FUNCTIONAL
#include "debug.h"
#include "bounded_array.h"
#include <functional>
#include "keycodes.h"
//See keycodes.h for details
namespace Z
{
//The amount of possible modifier keys
const uint8_t MODIFIER_AMOUNT=8;
const uint8_t KEYS_PER_PACKET=6;
const uint16_t KEYTYPE_MASK = 0xFF00;
enum class KeySignature : uint16_t
{
FUNCTION = 0x0000,
MODIFIER = MODIFIERKEY_ALT & KEYTYPE_MASK,
MEDIA = KEY_MEDIA_MUTE & KEYTYPE_MASK,
SYSTEM = KEY_SYSTEM_POWER_DOWN & KEYTYPE_MASK, //TODO: Find the correct mask
STANDARD = KEY_A & KEYTYPE_MASK,
};
enum class KeyType
{
FN,
MODIFIER,
MEDIA,
SYSTEM,
STANDARD,
UNRECOGNISED
};
using Keycode = uint16_t;
/*
* List of modifier keys on the keyboard
*/
enum FunctionKey
{
FN_RAISE,
FN_LOWER,
FN_WM,
FN_VIM,
FN_SPECIAL,
//Helper for getting the amount of modifiers
FN_AMOUNT
};
using FunctionKeyList = BoundedArray<bool, FN_AMOUNT>;
FunctionKeyList init_function_key_list();
/*
* Maps are stored in a column-row structure to allow
* [x][y] to be [horizontal][vertical]
* KEY_PAGE_DOWN* [
* x1:[y1,y2,y3...]
* x2:[y1,y2,y3...]
* ...
* ]
*/
template<size_t WIDTH, size_t HEIGHT>
using Keymap = BoundedArray< BoundedArray<Keycode, HEIGHT>, WIDTH>;
template<size_t WIDTH, size_t HEIGHT>
Keymap<WIDTH, HEIGHT> init_keymap(const Keycode map[HEIGHT][WIDTH])
{
auto result = Keymap<WIDTH, HEIGHT>();
for(size_t col_i = 0; col_i < WIDTH; ++col_i)
{
auto column = BoundedArray<Keycode, HEIGHT>();
for(size_t row_i = 0; row_i < HEIGHT; ++row_i)
{
column.push(map[row_i][col_i]);
}
result.push(column);
}
return result;
}
Keycode function_keycode(const FunctionKey modifier);
KeyType get_key_type(const Keycode key);
FunctionKey function_from_keycode(const Keycode key);
/**
Struct for representing a raw pressed key without any association
with a specified function
*/
struct KeyCoordinate
{
uint8_t x;
uint8_t y;
KeyCoordinate(){};
KeyCoordinate(uint8_t x, uint8_t y)
{
this->x = x;
this->y = y;
}
bool operator==(const KeyCoordinate& other) const
{
return other.x == this->x && other.y == this->y;
}
bool operator!=(const KeyCoordinate& other) const
{
return !(other == *this);
}
};
/*
Merges key coordinates of array1 with array2 with the coordinates of array2.
The coordinaes of array2 are modified using `function` before being put
in the result.
*/
template<size_t SIZE>
BoundedArray<KeyCoordinate, SIZE*2> merge_coordinates(
BoundedArray<KeyCoordinate, SIZE> array1,
BoundedArray<KeyCoordinate, SIZE> array2,
std::function<KeyCoordinate(KeyCoordinate)> function
)
{
BoundedArray<KeyCoordinate, SIZE*2> result;
for(size_t i = 0; i < array1.size(); ++i)
{
result.push(array1[i]);
}
for(size_t i = 0; i < array2.size(); ++i)
{
result.push(function(array2[i]));
}
return result;
}
/*
Translates a list of coordinates into key codes
*/
template<size_t WIDTH, size_t HEIGHT>
BoundedArray<Keycode, WIDTH*HEIGHT> translate_coordinates(
const BoundedArray<KeyCoordinate, WIDTH*HEIGHT> coordinates,
const Keymap<WIDTH, HEIGHT> map
)
{
auto result = BoundedArray<Keycode, WIDTH*HEIGHT>();
for(size_t i = 0; i < coordinates.size(); ++i)
{
result.push(map[coordinates[i].x][coordinates[i].y]);
}
return result;
}
/*
A list of different types of keys that are pressed
*/
template<size_t KEY_AMOUNT>
struct KeyTypes
{
BoundedArray<Keycode, KEY_AMOUNT> standard_keys;
BoundedArray<Keycode, MODIFIER_AMOUNT> modifiers;
KeyTypes()
{
this->function_keys = init_function_key_list();
}
bool contains_functionkey(Z::FunctionKey key) const
{
return function_keys[key];
}
void set_functionkey(Z::FunctionKey key, bool value)
{
this->function_keys[key] = value;
}
private:
FunctionKeyList function_keys;
};
/*
Creates a list of modifiers and keys from a list of keycodes
TODO: Optimze this to not copy the keys
*/
template<size_t KEY_AMOUNT>
KeyTypes<KEY_AMOUNT> keycodes_to_keytypes(const BoundedArray<Keycode, KEY_AMOUNT> keys)
{
KeyTypes<KEY_AMOUNT> result;
for(size_t i = 0; i < keys.size(); ++i)
{
auto key_type = get_key_type(keys[i]);
if(key_type == KeyType::FN)
{
result.set_functionkey(function_from_keycode(keys[i]), true);
}
else if(key_type == KeyType::MODIFIER)
{
result.modifiers.push(keys[i]);
}
else if(key_type == KeyType::STANDARD)
{
result.standard_keys.push(keys[i]);
}
}
return result;
}
/*
A keyboard packet that can be sent to the computer
*/
struct KeyPacket
{
uint16_t modifiers;
BoundedArray<Keycode, KEYS_PER_PACKET> keys;
KeyPacket()
{
modifiers = 0;
}
};
/*
Generate a KeyPacket from a KeyType list aswell as the last packet that
was sent
*/
template<size_t KEY_AMOUNT>
KeyPacket keytypes_to_packet(const KeyTypes<KEY_AMOUNT> keytypes, const KeyPacket old_packet)
{
KeyPacket result;
//Building the modifiers
for(size_t i = 0; i < keytypes.modifiers.size(); ++i)
{
result.modifiers |= keytypes.modifiers[i];
}
//Check which keys should be sent again
BoundedArray<Keycode, KEY_AMOUNT> keys_not_sent;
for(size_t i = 0; i < keytypes.standard_keys.size(); ++i)
{
if(old_packet.keys.contains(keytypes.standard_keys[i]))
{
result.keys.push(keytypes.standard_keys[i]);
}
else
{
keys_not_sent.push(keytypes.standard_keys[i]);
}
}
//If there are still spots left in the keys_to_send list
int16_t next_key = keys_not_sent.size() - 1;
while(result.keys.size() < KEYS_PER_PACKET && next_key >= 0)
{
result.keys.push(keys_not_sent[next_key]);
next_key--;
}
return result;
}
/*
Translates a list of keyboard coordinates into a list of bytes that can
be sent over UART and decoded by the decode_coordinates_from_bytes function
*/
template<size_t KEY_AMOUNT>
BoundedArray<uint8_t, KEY_AMOUNT * 2 + 1> coords_to_bytes(
BoundedArray<KeyCoordinate, KEY_AMOUNT> coords
)
{
auto result = BoundedArray<uint8_t, KEY_AMOUNT * 2 + 1>();
uint8_t checksum = 0;
for(size_t i = 0; i < coords.size(); ++i)
{
result.push(coords[i].x);
result.push(coords[i].y);
checksum += coords[i].x + coords[i].y;
}
result.push(checksum);
return result;
}
/*
Different things that can go wrong when translating bytes to coordinates
*/
enum class CoordsFromBytesError
{
SUCCESS,
INVALID_AMOUNT_OF_BYTES,
INVALID_CHECKSUM
};
/*
Result type that is returned when translating bytes to coordinates
*/
template<size_t KEY_AMOUNT>
struct CoordsFromBytesResult
{
BoundedArray<KeyCoordinate, KEY_AMOUNT> keys;
CoordsFromBytesError error;
CoordsFromBytesResult(CoordsFromBytesError error)
{
this->error = error;
}
CoordsFromBytesResult(BoundedArray<KeyCoordinate, KEY_AMOUNT> keys)
{
this->keys = keys;
this->error = CoordsFromBytesError::SUCCESS;
}
};
/*
Converts a stream of keyboard bytes into coordinates
*/
template<size_t KEY_AMOUNT>
CoordsFromBytesResult<KEY_AMOUNT> decode_coordinates_from_bytes(
BoundedArray<uint8_t, KEY_AMOUNT * 2 + 1> bytes
)
{
auto result = BoundedArray<KeyCoordinate, KEY_AMOUNT>();
if(bytes.size() % 2 != 1)
{
return CoordsFromBytesResult<KEY_AMOUNT>(CoordsFromBytesError::INVALID_AMOUNT_OF_BYTES);
}
uint8_t checksum = 0;
for(uint8_t i = 0; i < (bytes.size() - 1) / 2; ++i)
{
uint8_t index = i * 2;
result.push(KeyCoordinate(bytes[index], bytes[index+1]));
checksum += bytes[index] + bytes[index+1];
}
if(checksum != bytes[bytes.size() - 1])
{
return CoordsFromBytesResult<KEY_AMOUNT>(CoordsFromBytesError::INVALID_CHECKSUM);
}
return result;
}
}
#endif