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
|
#include <string.h>
#include "oryx.h"
#include "eeprom.h"
#ifdef KEYBOARD_voyager
# include "voyager.h"
#endif
rawhid_state_t rawhid_state = {.pairing = false, .paired = false};
keypos_t keyboard_pairing_sequence[PAIRING_SEQUENCE_SIZE];
keypos_t host_pairing_sequence[PAIRING_SEQUENCE_SIZE];
uint8_t pairing_input_index = 0;
void oryx_error(uint8_t code) {
uint8_t event[RAW_EPSIZE];
event[0] = ORYX_EVT_ERROR;
event[1] = code;
raw_hid_send(event, RAW_EPSIZE);
}
void raw_hid_receive(uint8_t *data, uint8_t length) {
uint8_t command = data[0];
uint8_t *param = &data[1];
uint8_t cmd_index = 0;
switch (command) {
case ORYX_CMD_GET_FW_VERSION: {
uint8_t event[RAW_EPSIZE];
uint8_t fw_version_size = sizeof(FIRMWARE_VERSION);
uint8_t stop[1];
event[0] = ORYX_EVT_GET_FW_VERSION;
stop[0] = ORYX_STOP_BIT;
memcpy(event + 1, FIRMWARE_VERSION, fw_version_size);
memcpy(event + fw_version_size, stop, 1);
raw_hid_send(event, RAW_EPSIZE);
break;
}
case ORYX_CMD_PAIRING_INIT:
pairing_init_handler();
store_pairing_sequence(&keyboard_pairing_sequence[0]);
break;
case ORYX_CMD_PAIRING_VALIDATE:
for (uint8_t i = 0; i < PAIRING_SEQUENCE_SIZE; i++) {
keypos_t pos;
pos.col = param[cmd_index++];
pos.row = param[cmd_index++];
host_pairing_sequence[i] = pos;
}
pairing_validate_eeprom_handler();
break;
case ORYX_SET_LAYER:
if (rawhid_state.paired == true) {
layer_clear();
layer_on(param[0]);
}
break;
case ORYX_RGB_CONTROL:
#if defined(RGB_MATRIX_ENABLE) && !defined(KEYBOARD_ergodox_ez_glow)
if (param[0] == 0) {
rgb_matrix_reload_from_eeprom();
rawhid_state.rgb_control = false;
} else {
rgb_matrix_mode_noeeprom(RGB_MATRIX_CUSTOM_oryx_webhid_effect);
rawhid_state.rgb_control = true;
}
uint8_t event[RAW_EPSIZE];
event[0] = ORYX_EVT_RGB_CONTROL;
event[1] = rawhid_state.rgb_control;
raw_hid_send(event, RAW_EPSIZE);
#else
oryx_error(ORYX_ERR_RGB_MATRIX_NOT_ENABLED);
#endif
break;
case ORYX_SET_RGB_LED:
#if defined(RGB_MATRIX_ENABLE) && !defined(KEYBOARD_ergodox_ez_glow)
webhid_leds[param[0]] = (RGB){.r = param[1], .g = param[2], .b = param[3]};
#else
oryx_error(ORYX_ERR_RGB_MATRIX_NOT_ENABLED);
#endif
break;
case ORYX_SET_STATUS_LED:
switch (param[0]) {
case 0:
#ifdef STATUS_LED_1
STATUS_LED_1(param[1]);
#else
oryx_error(ORYX_ERR_STATUS_LED_OUT_OF_RANGE);
#endif
break;
case 1:
#ifdef STATUS_LED_2
STATUS_LED_2(param[1]);
#else
oryx_error(ORYX_ERR_STATUS_LED_OUT_OF_RANGE);
#endif
break;
case 2:
#ifdef STATUS_LED_3
STATUS_LED_3(param[1]);
#else
oryx_error(ORYX_ERR_STATUS_LED_OUT_OF_RANGE);
#endif
break;
case 3:
#ifdef STATUS_LED_4
STATUS_LED_4(param[1]);
#else
oryx_error(ORYX_ERR_STATUS_LED_OUT_OF_RANGE);
#endif
break;
case 4:
#ifdef STATUS_LED_5
STATUS_LED_5(param[1]);
#else
oryx_error(ORYX_ERR_STATUS_LED_OUT_OF_RANGE);
#endif
break;
case 5:
#ifdef STATUS_LED_6
STATUS_LED_6(param[1]);
#else
oryx_error(ORYX_ERR_STATUS_LED_OUT_OF_RANGE);
#endif
break;
default:
oryx_error(ORYX_ERR_STATUS_LED_OUT_OF_RANGE);
break;
}
break;
case ORYX_UPDATE_BRIGHTNESS:
#if defined(RGB_MATRIX_ENABLE) && !defined(KEYBOARD_ergodox_ez_glow)
if (param[0]) {
rgb_matrix_increase_val_noeeprom();
} else {
rgb_matrix_decrease_val_noeeprom();
}
#else
oryx_error(ORYX_ERR_RGB_MATRIX_NOT_ENABLED);
#endif
break;
default:
oryx_error(ORYX_ERR_UNKNOWN_COMMAND);
}
}
void pairing_validate_eeprom_handler(void) {
bool match = false;
uint8_t event[RAW_EPSIZE];
uint8_t stored_sequences[sizeof(uint16_t) * PAIRING_SEQUENCE_SIZE * PAIRING_SEQUENCE_NUM_STORED];
eeprom_read_block(&stored_sequences, (uint8_t *)EECONFIG_SIZE, PAIRING_STORAGE_SIZE);
match = true;
if (match == true) {
event[0] = ORYX_EVT_PAIRING_SUCCESS;
rawhid_state.paired = true;
} else {
event[0] = ORYX_EVT_PAIRING_FAILED;
rawhid_state.paired = false;
}
event[1] = ORYX_STOP_BIT;
rawhid_state.pairing = false;
raw_hid_send(event, sizeof(event));
}
bool store_pairing_sequence(keypos_t *pairing_sequence) {
uint8_t stored_sequences[sizeof(uint16_t) * PAIRING_SEQUENCE_SIZE * PAIRING_SEQUENCE_NUM_STORED];
eeprom_read_block(&stored_sequences, (uint8_t *)EECONFIG_SIZE, PAIRING_STORAGE_SIZE);
uint8_t shiftLen = sizeof(&pairing_sequence);
for (int8_t i = PAIRING_STORAGE_SIZE; i >= 0; i--) {
if (i > shiftLen) {
stored_sequences[i] = stored_sequences[i - 1];
} else {
stored_sequences[i] = 0;
}
}
eeprom_update_block(stored_sequences, (uint8_t *)EECONFIG_SIZE, PAIRING_STORAGE_SIZE);
return true;
}
void pairing_init_handler(void) {
create_pairing_code();
uint8_t event[RAW_EPSIZE];
uint8_t event_index = 0;
event[event_index++] = ORYX_EVT_PAIRING_INPUT;
for (uint8_t i = 0; i < PAIRING_SEQUENCE_SIZE; i++) {
event[event_index++] = keyboard_pairing_sequence[i].col;
event[event_index++] = keyboard_pairing_sequence[i].row;
}
event[event_index++] = ORYX_STOP_BIT;
rawhid_state.pairing = true;
raw_hid_send(event, RAW_EPSIZE);
}
bool compare_sequences(keypos_t a[PAIRING_SEQUENCE_SIZE], keypos_t b[PAIRING_SEQUENCE_SIZE]) {
bool valid = true;
for (uint8_t i = 0; i < PAIRING_SEQUENCE_SIZE; i++) {
if (a[i].row != b[i].row) {
valid = false;
break;
}
if (a[i].col != b[i].col) {
valid = false;
break;
}
}
return valid;
}
void pairing_validate_handler() {
uint8_t event[RAW_EPSIZE];
bool valid = compare_sequences(keyboard_pairing_sequence, host_pairing_sequence);
if (valid == true) {
event[0] = ORYX_EVT_PAIRING_SUCCESS;
rawhid_state.paired = true;
} else {
event[0] = ORYX_EVT_PAIRING_FAILED;
rawhid_state.paired = false;
}
event[1] = ORYX_STOP_BIT;
rawhid_state.pairing = false;
raw_hid_send(event, sizeof(event));
}
keypos_t get_random_keypos(void) {
uint8_t col = rand() % MATRIX_COLS;
uint8_t row = rand() % MATRIX_ROWS;
keypos_t pos = {.col = col, .row = row};
uint16_t keycode = keymap_key_to_keycode(0, pos);
if (keycode >= KC_A && keycode <= KC_SLASH) {
return pos;
} else {
return get_random_keypos();
}
}
keypos_t *pairing_sequence(void) {
// The pairing sequence is derived from Oryx's layout id declared
// in the generated source config file with the FIRMWARE_VERSION define.
keypos_t *sequence = (keypos_t *)&host_pairing_sequence[0];
for (uint8_t i = 0; i < PAIRING_SEQUENCE_SIZE; i++) {
}
return sequence;
}
void create_pairing_code(void) {
for (uint8_t i = 0; i < PAIRING_SEQUENCE_SIZE; i++) {
keypos_t pos = get_random_keypos();
keyboard_pairing_sequence[i] = pos;
}
}
void pairing_key_input_event(void) {
uint8_t event[RAW_EPSIZE];
event[0] = ORYX_EVT_PAIRING_KEY_INPUT;
raw_hid_send(event, sizeof(event));
}
void oryx_layer_event(void) {
uint8_t layer;
uint8_t event[RAW_EPSIZE];
layer = get_highest_layer(layer_state);
event[0] = ORYX_EVT_LAYER;
event[1] = layer;
event[2] = ORYX_STOP_BIT;
raw_hid_send(event, sizeof(event));
}
bool process_record_oryx(uint16_t keycode, keyrecord_t *record) {
// In pairing mode, key events are absorbed, and the host pairing sequence is filled.
// Once filled, the keyboard and host sequence are compaired, pairing state set to false
// and the proper pairing validation event is sent to the host
if (rawhid_state.pairing == true) {
// The host pairing sequence is filled on key up only
if (!record->event.pressed) {
if (pairing_input_index < PAIRING_SEQUENCE_SIZE) {
host_pairing_sequence[pairing_input_index++] = record->event.key;
pairing_key_input_event();
}
wait_ms(1000);
if (pairing_input_index == PAIRING_SEQUENCE_SIZE) {
rawhid_state.pairing = false;
pairing_input_index = 0;
pairing_validate_handler();
}
}
return false;
}
// While paired, the keyboard sends keystrokes positions to the host
if (rawhid_state.paired == true) {
uint8_t event[RAW_EPSIZE];
event[0] = record->event.pressed ? ORYX_EVT_KEYDOWN : ORYX_EVT_KEYUP;
event[1] = record->event.key.col;
event[2] = record->event.key.row;
event[3] = ORYX_STOP_BIT;
raw_hid_send(event, sizeof(event));
}
return true;
}
void layer_state_set_oryx(layer_state_t state) {
if (rawhid_state.paired) {
wait_ms(50);
uint8_t event[RAW_EPSIZE];
event[0] = ORYX_EVT_LAYER;
event[1] = get_highest_layer(state);
event[2] = ORYX_STOP_BIT;
raw_hid_send(event, sizeof(event));
}
}
|