aboutsummaryrefslogtreecommitdiff
path: root/quantum
diff options
context:
space:
mode:
authorQMK Bot <hello@qmk.fm>2021-02-07 23:16:47 +0000
committerQMK Bot <hello@qmk.fm>2021-02-07 23:16:47 +0000
commit3a98bd75c88c5cc64aed72b9d2e3a8857d949fed (patch)
tree7edcbbfa0c0efd16e456d6012f90833922d3bb3c /quantum
parentb8031a1613290ada50f4cb746216fcae90d8935d (diff)
parent99bffc2a21ebed07fd767ad2a9a7e1aadd491ef3 (diff)
downloadqmk_firmware-3a98bd75c88c5cc64aed72b9d2e3a8857d949fed.tar.gz
qmk_firmware-3a98bd75c88c5cc64aed72b9d2e3a8857d949fed.zip
Merge remote-tracking branch 'origin/master' into develop
Diffstat (limited to 'quantum')
-rw-r--r--quantum/bitwise.c123
-rw-r--r--quantum/bitwise.h40
-rw-r--r--quantum/command.c786
-rw-r--r--quantum/command.h163
-rw-r--r--quantum/led.h54
-rw-r--r--quantum/matrix.h79
-rw-r--r--quantum/ring_buffer.h44
-rw-r--r--quantum/split_common/transport.h2
-rw-r--r--quantum/util.h26
9 files changed, 1316 insertions, 1 deletions
diff --git a/quantum/bitwise.c b/quantum/bitwise.c
new file mode 100644
index 000000000..861cca005
--- /dev/null
+++ b/quantum/bitwise.c
@@ -0,0 +1,123 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include "util.h"
19
20// bit population - return number of on-bit
21__attribute__((noinline)) uint8_t bitpop(uint8_t bits) {
22 uint8_t c;
23 for (c = 0; bits; c++) bits &= bits - 1;
24 return c;
25 /*
26 const uint8_t bit_count[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
27 return bit_count[bits>>4] + bit_count[bits&0x0F]
28 */
29}
30
31uint8_t bitpop16(uint16_t bits) {
32 uint8_t c;
33 for (c = 0; bits; c++) bits &= bits - 1;
34 return c;
35}
36
37uint8_t bitpop32(uint32_t bits) {
38 uint8_t c;
39 for (c = 0; bits; c++) bits &= bits - 1;
40 return c;
41}
42
43// most significant on-bit - return highest location of on-bit
44// NOTE: return 0 when bit0 is on or all bits are off
45__attribute__((noinline)) uint8_t biton(uint8_t bits) {
46 uint8_t n = 0;
47 if (bits >> 4) {
48 bits >>= 4;
49 n += 4;
50 }
51 if (bits >> 2) {
52 bits >>= 2;
53 n += 2;
54 }
55 if (bits >> 1) {
56 bits >>= 1;
57 n += 1;
58 }
59 return n;
60}
61
62uint8_t biton16(uint16_t bits) {
63 uint8_t n = 0;
64 if (bits >> 8) {
65 bits >>= 8;
66 n += 8;
67 }
68 if (bits >> 4) {
69 bits >>= 4;
70 n += 4;
71 }
72 if (bits >> 2) {
73 bits >>= 2;
74 n += 2;
75 }
76 if (bits >> 1) {
77 bits >>= 1;
78 n += 1;
79 }
80 return n;
81}
82
83uint8_t biton32(uint32_t bits) {
84 uint8_t n = 0;
85 if (bits >> 16) {
86 bits >>= 16;
87 n += 16;
88 }
89 if (bits >> 8) {
90 bits >>= 8;
91 n += 8;
92 }
93 if (bits >> 4) {
94 bits >>= 4;
95 n += 4;
96 }
97 if (bits >> 2) {
98 bits >>= 2;
99 n += 2;
100 }
101 if (bits >> 1) {
102 bits >>= 1;
103 n += 1;
104 }
105 return n;
106}
107
108__attribute__((noinline)) uint8_t bitrev(uint8_t bits) {
109 bits = (bits & 0x0f) << 4 | (bits & 0xf0) >> 4;
110 bits = (bits & 0b00110011) << 2 | (bits & 0b11001100) >> 2;
111 bits = (bits & 0b01010101) << 1 | (bits & 0b10101010) >> 1;
112 return bits;
113}
114
115uint16_t bitrev16(uint16_t bits) {
116 bits = bitrev(bits & 0x00ff) << 8 | bitrev((bits & 0xff00) >> 8);
117 return bits;
118}
119
120uint32_t bitrev32(uint32_t bits) {
121 bits = (uint32_t)bitrev16(bits & 0x0000ffff) << 16 | bitrev16((bits & 0xffff0000) >> 16);
122 return bits;
123}
diff --git a/quantum/bitwise.h b/quantum/bitwise.h
new file mode 100644
index 000000000..276bc7437
--- /dev/null
+++ b/quantum/bitwise.h
@@ -0,0 +1,40 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#pragma once
19
20#include <stdint.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26uint8_t bitpop(uint8_t bits);
27uint8_t bitpop16(uint16_t bits);
28uint8_t bitpop32(uint32_t bits);
29
30uint8_t biton(uint8_t bits);
31uint8_t biton16(uint16_t bits);
32uint8_t biton32(uint32_t bits);
33
34uint8_t bitrev(uint8_t bits);
35uint16_t bitrev16(uint16_t bits);
36uint32_t bitrev32(uint32_t bits);
37
38#ifdef __cplusplus
39}
40#endif
diff --git a/quantum/command.c b/quantum/command.c
new file mode 100644
index 000000000..34c4b36b1
--- /dev/null
+++ b/quantum/command.c
@@ -0,0 +1,786 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#include <stdint.h>
18#include <stdbool.h>
19#include "wait.h"
20#include "keycode.h"
21#include "host.h"
22#include "keymap.h"
23#include "print.h"
24#include "debug.h"
25#include "util.h"
26#include "timer.h"
27#include "keyboard.h"
28#include "bootloader.h"
29#include "action_layer.h"
30#include "action_util.h"
31#include "eeconfig.h"
32#include "sleep_led.h"
33#include "led.h"
34#include "command.h"
35#include "quantum.h"
36#include "version.h"
37
38#ifdef BACKLIGHT_ENABLE
39# include "backlight.h"
40#endif
41
42#if defined(MOUSEKEY_ENABLE) && !defined(MK_3_SPEED)
43# include "mousekey.h"
44#endif
45
46#ifdef AUDIO_ENABLE
47# include "audio.h"
48#endif /* AUDIO_ENABLE */
49
50static bool command_common(uint8_t code);
51static void command_common_help(void);
52static void print_version(void);
53static void print_status(void);
54static bool command_console(uint8_t code);
55static void command_console_help(void);
56#if defined(MOUSEKEY_ENABLE) && !defined(MK_3_SPEED)
57static bool mousekey_console(uint8_t code);
58static void mousekey_console_help(void);
59#endif
60
61static void switch_default_layer(uint8_t layer);
62
63command_state_t command_state = ONESHOT;
64
65bool command_proc(uint8_t code) {
66 switch (command_state) {
67 case ONESHOT:
68 if (!IS_COMMAND()) return false;
69 return (command_extra(code) || command_common(code));
70 break;
71 case CONSOLE:
72 if (IS_COMMAND())
73 return (command_extra(code) || command_common(code));
74 else
75 return (command_console_extra(code) || command_console(code));
76 break;
77#if defined(MOUSEKEY_ENABLE) && !defined(MK_3_SPEED)
78 case MOUSEKEY:
79 mousekey_console(code);
80 break;
81#endif
82 default:
83 command_state = ONESHOT;
84 return false;
85 }
86 return true;
87}
88
89/* TODO: Refactoring is needed. */
90/* This allows to define extra commands. return false when not processed. */
91bool command_extra(uint8_t code) __attribute__((weak));
92bool command_extra(uint8_t code) {
93 (void)code;
94 return false;
95}
96
97bool command_console_extra(uint8_t code) __attribute__((weak));
98bool command_console_extra(uint8_t code) {
99 (void)code;
100 return false;
101}
102
103/***********************************************************
104 * Command common
105 ***********************************************************/
106static void command_common_help(void) {
107 print("\n\t- Magic -\n" STR(MAGIC_KEY_DEBUG) ": Debug Message Toggle\n" STR(MAGIC_KEY_DEBUG_MATRIX) ": Matrix Debug Mode Toggle - Show keypresses in matrix grid\n" STR(MAGIC_KEY_DEBUG_KBD) ": Keyboard Debug Toggle - Show keypress report\n" STR(MAGIC_KEY_DEBUG_MOUSE) ": Debug Mouse Toggle\n" STR(MAGIC_KEY_VERSION) ": Version\n" STR(MAGIC_KEY_STATUS) ": Status\n" STR(MAGIC_KEY_CONSOLE) ": Activate Console Mode\n"
108
109#if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
110 STR(MAGIC_KEY_LAYER0) ": Switch to Layer 0\n" STR(MAGIC_KEY_LAYER1) ": Switch to Layer 1\n" STR(MAGIC_KEY_LAYER2) ": Switch to Layer 2\n" STR(MAGIC_KEY_LAYER3) ": Switch to Layer 3\n" STR(MAGIC_KEY_LAYER4) ": Switch to Layer 4\n" STR(MAGIC_KEY_LAYER5) ": Switch to Layer 5\n" STR(MAGIC_KEY_LAYER6) ": Switch to Layer 6\n" STR(MAGIC_KEY_LAYER7) ": Switch to Layer 7\n" STR(MAGIC_KEY_LAYER8) ": Switch to Layer 8\n" STR(MAGIC_KEY_LAYER9) ": Switch to Layer 9\n"
111#endif
112
113#if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
114 "F1-F10: Switch to Layer 0-9 (F10 = L0)\n"
115#endif
116
117#if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
118 "0-9: Switch to Layer 0-9\n"
119#endif
120
121 STR(MAGIC_KEY_LAYER0_ALT) ": Switch to Layer 0 (alternate)\n"
122
123 STR(MAGIC_KEY_BOOTLOADER) ": Jump to Bootloader\n" STR(MAGIC_KEY_BOOTLOADER_ALT) ": Jump to Bootloader (alternate)\n"
124
125#ifdef KEYBOARD_LOCK_ENABLE
126 STR(MAGIC_KEY_LOCK) ": Lock Keyboard\n"
127#endif
128
129 STR(MAGIC_KEY_EEPROM) ": Print EEPROM Settings\n" STR(MAGIC_KEY_EEPROM_CLEAR) ": Clear EEPROM\n"
130
131#ifdef NKRO_ENABLE
132 STR(MAGIC_KEY_NKRO) ": NKRO Toggle\n"
133#endif
134
135#ifdef SLEEP_LED_ENABLE
136 STR(MAGIC_KEY_SLEEP_LED) ": Sleep LED Test\n"
137#endif
138 );
139}
140
141static void print_version(void) {
142 // print version & information
143 print("\n\t- Version -\n");
144 print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
145 "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
146 "VER: " STR(DEVICE_VER) "\n");
147 print("BUILD: (" __DATE__ ")\n");
148#ifndef SKIP_VERSION
149# ifdef PROTOCOL_CHIBIOS
150 print("CHIBIOS: " STR(CHIBIOS_VERSION) ", CONTRIB: " STR(CHIBIOS_CONTRIB_VERSION) "\n");
151# endif
152#endif
153
154 /* build options */
155 print("OPTIONS:"
156
157#ifdef PROTOCOL_LUFA
158 " LUFA"
159#endif
160#ifdef PROTOCOL_VUSB
161 " VUSB"
162#endif
163#ifdef BOOTMAGIC_ENABLE
164 " BOOTMAGIC"
165#endif
166#ifdef MOUSEKEY_ENABLE
167 " MOUSEKEY"
168#endif
169#ifdef EXTRAKEY_ENABLE
170 " EXTRAKEY"
171#endif
172#ifdef CONSOLE_ENABLE
173 " CONSOLE"
174#endif
175#ifdef COMMAND_ENABLE
176 " COMMAND"
177#endif
178#ifdef NKRO_ENABLE
179 " NKRO"
180#endif
181#ifdef LTO_ENABLE
182 " LTO"
183#endif
184
185 " " STR(BOOTLOADER_SIZE) "\n");
186
187 print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
188#if defined(__AVR__)
189 " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__ " AVR_ARCH: avr" STR(__AVR_ARCH__)
190#endif
191 "\n");
192
193 return;
194}
195
196static void print_status(void) {
197 print("\n\t- Status -\n");
198
199 print_val_hex8(host_keyboard_leds());
200#ifndef PROTOCOL_VUSB
201 // these aren't set on the V-USB protocol, so we just ignore them for now
202 print_val_hex8(keyboard_protocol);
203 print_val_hex8(keyboard_idle);
204#endif
205#ifdef NKRO_ENABLE
206 print_val_hex8(keymap_config.nkro);
207#endif
208 print_val_hex32(timer_read32());
209 return;
210}
211
212static void print_eeconfig(void) {
213// Print these variables if NO_PRINT or USER_PRINT are not defined.
214#if !defined(NO_PRINT) && !defined(USER_PRINT)
215
216 print("default_layer: ");
217 print_dec(eeconfig_read_default_layer());
218 print("\n");
219
220 debug_config_t dc;
221 dc.raw = eeconfig_read_debug();
222 print("debug_config.raw: ");
223 print_hex8(dc.raw);
224 print("\n");
225 print(".enable: ");
226 print_dec(dc.enable);
227 print("\n");
228 print(".matrix: ");
229 print_dec(dc.matrix);
230 print("\n");
231 print(".keyboard: ");
232 print_dec(dc.keyboard);
233 print("\n");
234 print(".mouse: ");
235 print_dec(dc.mouse);
236 print("\n");
237
238 keymap_config_t kc;
239 kc.raw = eeconfig_read_keymap();
240 print("keymap_config.raw: ");
241 print_hex8(kc.raw);
242 print("\n");
243 print(".swap_control_capslock: ");
244 print_dec(kc.swap_control_capslock);
245 print("\n");
246 print(".capslock_to_control: ");
247 print_dec(kc.capslock_to_control);
248 print("\n");
249 print(".swap_lctl_lgui: ");
250 print_dec(kc.swap_lctl_lgui);
251 print("\n");
252 print(".swap_rctl_rgui: ");
253 print_dec(kc.swap_rctl_rgui);
254 print("\n");
255 print(".swap_lalt_lgui: ");
256 print_dec(kc.swap_lalt_lgui);
257 print("\n");
258 print(".swap_ralt_rgui: ");
259 print_dec(kc.swap_ralt_rgui);
260 print("\n");
261 print(".no_gui: ");
262 print_dec(kc.no_gui);
263 print("\n");
264 print(".swap_grave_esc: ");
265 print_dec(kc.swap_grave_esc);
266 print("\n");
267 print(".swap_backslash_backspace: ");
268 print_dec(kc.swap_backslash_backspace);
269 print("\n");
270 print(".nkro: ");
271 print_dec(kc.nkro);
272 print("\n");
273
274# ifdef BACKLIGHT_ENABLE
275 backlight_config_t bc;
276 bc.raw = eeconfig_read_backlight();
277 print("backlight_config.raw: ");
278 print_hex8(bc.raw);
279 print("\n");
280 print(".enable: ");
281 print_dec(bc.enable);
282 print("\n");
283 print(".level: ");
284 print_dec(bc.level);
285 print("\n");
286# endif /* BACKLIGHT_ENABLE */
287
288#endif /* !NO_PRINT */
289}
290
291static bool command_common(uint8_t code) {
292#ifdef KEYBOARD_LOCK_ENABLE
293 static host_driver_t *host_driver = 0;
294#endif
295
296 switch (code) {
297#ifdef SLEEP_LED_ENABLE
298
299 // test breathing sleep LED
300 case MAGIC_KC(MAGIC_KEY_SLEEP_LED):
301 print("Sleep LED Test\n");
302 sleep_led_toggle();
303 led_set(host_keyboard_leds());
304 break;
305#endif
306
307 // print stored eeprom config
308 case MAGIC_KC(MAGIC_KEY_EEPROM):
309 print("eeconfig:\n");
310 print_eeconfig();
311 break;
312
313 // clear eeprom
314 case MAGIC_KC(MAGIC_KEY_EEPROM_CLEAR):
315 print("Clearing EEPROM\n");
316 eeconfig_init();
317 break;
318
319#ifdef KEYBOARD_LOCK_ENABLE
320
321 // lock/unlock keyboard
322 case MAGIC_KC(MAGIC_KEY_LOCK):
323 if (host_get_driver()) {
324 host_driver = host_get_driver();
325 clear_keyboard();
326 host_set_driver(0);
327 print("Locked.\n");
328 } else {
329 host_set_driver(host_driver);
330 print("Unlocked.\n");
331 }
332 break;
333#endif
334
335 // print help
336 case MAGIC_KC(MAGIC_KEY_HELP):
337 case MAGIC_KC(MAGIC_KEY_HELP_ALT):
338 command_common_help();
339 break;
340
341 // activate console
342 case MAGIC_KC(MAGIC_KEY_CONSOLE):
343 debug_matrix = false;
344 debug_keyboard = false;
345 debug_mouse = false;
346 debug_enable = false;
347 command_console_help();
348 print("C> ");
349 command_state = CONSOLE;
350 break;
351
352 // jump to bootloader
353 case MAGIC_KC(MAGIC_KEY_BOOTLOADER):
354 case MAGIC_KC(MAGIC_KEY_BOOTLOADER_ALT):
355 print("\n\nJumping to bootloader... ");
356 reset_keyboard();
357 break;
358
359 // debug toggle
360 case MAGIC_KC(MAGIC_KEY_DEBUG):
361 debug_enable = !debug_enable;
362 if (debug_enable) {
363 print("\ndebug: on\n");
364 } else {
365 print("\ndebug: off\n");
366 debug_matrix = false;
367 debug_keyboard = false;
368 debug_mouse = false;
369 }
370 break;
371
372 // debug matrix toggle
373 case MAGIC_KC(MAGIC_KEY_DEBUG_MATRIX):
374 debug_matrix = !debug_matrix;
375 if (debug_matrix) {
376 print("\nmatrix: on\n");
377 debug_enable = true;
378 } else {
379 print("\nmatrix: off\n");
380 }
381 break;
382
383 // debug keyboard toggle
384 case MAGIC_KC(MAGIC_KEY_DEBUG_KBD):
385 debug_keyboard = !debug_keyboard;
386 if (debug_keyboard) {
387 print("\nkeyboard: on\n");
388 debug_enable = true;
389 } else {
390 print("\nkeyboard: off\n");
391 }
392 break;
393
394 // debug mouse toggle
395 case MAGIC_KC(MAGIC_KEY_DEBUG_MOUSE):
396 debug_mouse = !debug_mouse;
397 if (debug_mouse) {
398 print("\nmouse: on\n");
399 debug_enable = true;
400 } else {
401 print("\nmouse: off\n");
402 }
403 break;
404
405 // print version
406 case MAGIC_KC(MAGIC_KEY_VERSION):
407 print_version();
408 break;
409
410 // print status
411 case MAGIC_KC(MAGIC_KEY_STATUS):
412 print_status();
413 break;
414
415#ifdef NKRO_ENABLE
416
417 // NKRO toggle
418 case MAGIC_KC(MAGIC_KEY_NKRO):
419 clear_keyboard(); // clear to prevent stuck keys
420 keymap_config.nkro = !keymap_config.nkro;
421 if (keymap_config.nkro) {
422 print("NKRO: on\n");
423 } else {
424 print("NKRO: off\n");
425 }
426 break;
427#endif
428
429 // switch layers
430
431 case MAGIC_KC(MAGIC_KEY_LAYER0_ALT):
432 switch_default_layer(0);
433 break;
434
435#if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
436
437 case MAGIC_KC(MAGIC_KEY_LAYER0):
438 switch_default_layer(0);
439 break;
440
441 case MAGIC_KC(MAGIC_KEY_LAYER1):
442 switch_default_layer(1);
443 break;
444
445 case MAGIC_KC(MAGIC_KEY_LAYER2):
446 switch_default_layer(2);
447 break;
448
449 case MAGIC_KC(MAGIC_KEY_LAYER3):
450 switch_default_layer(3);
451 break;
452
453 case MAGIC_KC(MAGIC_KEY_LAYER4):
454 switch_default_layer(4);
455 break;
456
457 case MAGIC_KC(MAGIC_KEY_LAYER5):
458 switch_default_layer(5);
459 break;
460
461 case MAGIC_KC(MAGIC_KEY_LAYER6):
462 switch_default_layer(6);
463 break;
464
465 case MAGIC_KC(MAGIC_KEY_LAYER7):
466 switch_default_layer(7);
467 break;
468
469 case MAGIC_KC(MAGIC_KEY_LAYER8):
470 switch_default_layer(8);
471 break;
472
473 case MAGIC_KC(MAGIC_KEY_LAYER9):
474 switch_default_layer(9);
475 break;
476#endif
477
478#if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
479
480 case KC_F1 ... KC_F9:
481 switch_default_layer((code - KC_F1) + 1);
482 break;
483 case KC_F10:
484 switch_default_layer(0);
485 break;
486#endif
487
488#if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
489
490 case KC_1 ... KC_9:
491 switch_default_layer((code - KC_1) + 1);
492 break;
493 case KC_0:
494 switch_default_layer(0);
495 break;
496#endif
497
498 default:
499 print("?");
500 return false;
501 }
502 return true;
503}
504
505/***********************************************************
506 * Command console
507 ***********************************************************/
508static void command_console_help(void) {
509 print("\n\t- Console -\n"
510 "ESC/q: quit\n"
511#ifdef MOUSEKEY_ENABLE
512 "m: mousekey\n"
513#endif
514 );
515}
516
517static bool command_console(uint8_t code) {
518 switch (code) {
519 case KC_H:
520 case KC_SLASH: /* ? */
521 command_console_help();
522 break;
523 case KC_Q:
524 case KC_ESC:
525 command_state = ONESHOT;
526 return false;
527#if defined(MOUSEKEY_ENABLE) && !defined(MK_3_SPEED)
528 case KC_M:
529 mousekey_console_help();
530 print("M> ");
531 command_state = MOUSEKEY;
532 return true;
533#endif
534 default:
535 print("?");
536 return false;
537 }
538 print("C> ");
539 return true;
540}
541
542#if defined(MOUSEKEY_ENABLE) && !defined(MK_3_SPEED)
543/***********************************************************
544 * Mousekey console
545 ***********************************************************/
546static uint8_t mousekey_param = 0;
547
548static void mousekey_param_print(void) {
549// Print these variables if NO_PRINT or USER_PRINT are not defined.
550# if !defined(NO_PRINT) && !defined(USER_PRINT)
551 print("\n\t- Values -\n");
552 print("1: delay(*10ms): ");
553 print_dec(mk_delay);
554 print("\n");
555 print("2: interval(ms): ");
556 print_dec(mk_interval);
557 print("\n");
558 print("3: max_speed: ");
559 print_dec(mk_max_speed);
560 print("\n");
561 print("4: time_to_max: ");
562 print_dec(mk_time_to_max);
563 print("\n");
564 print("5: wheel_max_speed: ");
565 print_dec(mk_wheel_max_speed);
566 print("\n");
567 print("6: wheel_time_to_max: ");
568 print_dec(mk_wheel_time_to_max);
569 print("\n");
570# endif /* !NO_PRINT */
571}
572
573//#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
574# define PRINT_SET_VAL(v) xprintf(# v " = %d\n", (v))
575static void mousekey_param_inc(uint8_t param, uint8_t inc) {
576 switch (param) {
577 case 1:
578 if (mk_delay + inc < UINT8_MAX)
579 mk_delay += inc;
580 else
581 mk_delay = UINT8_MAX;
582 PRINT_SET_VAL(mk_delay);
583 break;
584 case 2:
585 if (mk_interval + inc < UINT8_MAX)
586 mk_interval += inc;
587 else
588 mk_interval = UINT8_MAX;
589 PRINT_SET_VAL(mk_interval);
590 break;
591 case 3:
592 if (mk_max_speed + inc < UINT8_MAX)
593 mk_max_speed += inc;
594 else
595 mk_max_speed = UINT8_MAX;
596 PRINT_SET_VAL(mk_max_speed);
597 break;
598 case 4:
599 if (mk_time_to_max + inc < UINT8_MAX)
600 mk_time_to_max += inc;
601 else
602 mk_time_to_max = UINT8_MAX;
603 PRINT_SET_VAL(mk_time_to_max);
604 break;
605 case 5:
606 if (mk_wheel_max_speed + inc < UINT8_MAX)
607 mk_wheel_max_speed += inc;
608 else
609 mk_wheel_max_speed = UINT8_MAX;
610 PRINT_SET_VAL(mk_wheel_max_speed);
611 break;
612 case 6:
613 if (mk_wheel_time_to_max + inc < UINT8_MAX)
614 mk_wheel_time_to_max += inc;
615 else
616 mk_wheel_time_to_max = UINT8_MAX;
617 PRINT_SET_VAL(mk_wheel_time_to_max);
618 break;
619 }
620}
621
622static void mousekey_param_dec(uint8_t param, uint8_t dec) {
623 switch (param) {
624 case 1:
625 if (mk_delay > dec)
626 mk_delay -= dec;
627 else
628 mk_delay = 0;
629 PRINT_SET_VAL(mk_delay);
630 break;
631 case 2:
632 if (mk_interval > dec)
633 mk_interval -= dec;
634 else
635 mk_interval = 0;
636 PRINT_SET_VAL(mk_interval);
637 break;
638 case 3:
639 if (mk_max_speed > dec)
640 mk_max_speed -= dec;
641 else
642 mk_max_speed = 0;
643 PRINT_SET_VAL(mk_max_speed);
644 break;
645 case 4:
646 if (mk_time_to_max > dec)
647 mk_time_to_max -= dec;
648 else
649 mk_time_to_max = 0;
650 PRINT_SET_VAL(mk_time_to_max);
651 break;
652 case 5:
653 if (mk_wheel_max_speed > dec)
654 mk_wheel_max_speed -= dec;
655 else
656 mk_wheel_max_speed = 0;
657 PRINT_SET_VAL(mk_wheel_max_speed);
658 break;
659 case 6:
660 if (mk_wheel_time_to_max > dec)
661 mk_wheel_time_to_max -= dec;
662 else
663 mk_wheel_time_to_max = 0;
664 PRINT_SET_VAL(mk_wheel_time_to_max);
665 break;
666 }
667}
668
669static void mousekey_console_help(void) {
670 print("\n\t- Mousekey -\n"
671 "ESC/q: quit\n"
672 "1: delay(*10ms)\n"
673 "2: interval(ms)\n"
674 "3: max_speed\n"
675 "4: time_to_max\n"
676 "5: wheel_max_speed\n"
677 "6: wheel_time_to_max\n"
678 "\n"
679 "p: print values\n"
680 "d: set defaults\n"
681 "up: +1\n"
682 "down: -1\n"
683 "pgup: +10\n"
684 "pgdown: -10\n"
685 "\n"
686 "speed = delta * max_speed * (repeat / time_to_max)\n");
687 xprintf("where delta: cursor=%d, wheel=%d\n"
688 "See http://en.wikipedia.org/wiki/Mouse_keys\n",
689 MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
690}
691
692static bool mousekey_console(uint8_t code) {
693 switch (code) {
694 case KC_H:
695 case KC_SLASH: /* ? */
696 mousekey_console_help();
697 break;
698 case KC_Q:
699 case KC_ESC:
700 if (mousekey_param) {
701 mousekey_param = 0;
702 } else {
703 print("C> ");
704 command_state = CONSOLE;
705 return false;
706 }
707 break;
708 case KC_P:
709 mousekey_param_print();
710 break;
711 case KC_1:
712 case KC_2:
713 case KC_3:
714 case KC_4:
715 case KC_5:
716 case KC_6:
717 mousekey_param = numkey2num(code);
718 break;
719 case KC_UP:
720 mousekey_param_inc(mousekey_param, 1);
721 break;
722 case KC_DOWN:
723 mousekey_param_dec(mousekey_param, 1);
724 break;
725 case KC_PGUP:
726 mousekey_param_inc(mousekey_param, 10);
727 break;
728 case KC_PGDN:
729 mousekey_param_dec(mousekey_param, 10);
730 break;
731 case KC_D:
732 mk_delay = MOUSEKEY_DELAY / 10;
733 mk_interval = MOUSEKEY_INTERVAL;
734 mk_max_speed = MOUSEKEY_MAX_SPEED;
735 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
736 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
737 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
738 print("set default\n");
739 break;
740 default:
741 print("?");
742 return false;
743 }
744 if (mousekey_param) {
745 xprintf("M%d> ", mousekey_param);
746 } else {
747 print("M>");
748 }
749 return true;
750}
751#endif
752
753/***********************************************************
754 * Utilities
755 ***********************************************************/
756uint8_t numkey2num(uint8_t code) {
757 switch (code) {
758 case KC_1:
759 return 1;
760 case KC_2:
761 return 2;
762 case KC_3:
763 return 3;
764 case KC_4:
765 return 4;
766 case KC_5:
767 return 5;
768 case KC_6:
769 return 6;
770 case KC_7:
771 return 7;
772 case KC_8:
773 return 8;
774 case KC_9:
775 return 9;
776 case KC_0:
777 return 0;
778 }
779 return 0;
780}
781
782static void switch_default_layer(uint8_t layer) {
783 xprintf("L%d\n", layer);
784 default_layer_set(1UL << layer);
785 clear_keyboard();
786}
diff --git a/quantum/command.h b/quantum/command.h
new file mode 100644
index 000000000..4f77be085
--- /dev/null
+++ b/quantum/command.h
@@ -0,0 +1,163 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#pragma once
19
20/* FIXME: Add doxygen comments for the behavioral defines in here. */
21
22/* TODO: Refactoring */
23typedef enum { ONESHOT, CONSOLE, MOUSEKEY } command_state_t;
24extern command_state_t command_state;
25
26/* This allows to extend commands. Return false when command is not processed. */
27bool command_extra(uint8_t code);
28bool command_console_extra(uint8_t code);
29
30#ifdef COMMAND_ENABLE
31uint8_t numkey2num(uint8_t code);
32bool command_proc(uint8_t code);
33#else
34# define command_proc(code) false
35#endif
36
37#ifndef IS_COMMAND
38# define IS_COMMAND() (get_mods() == MOD_MASK_SHIFT)
39#endif
40
41#ifndef MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
42# define MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS true
43#endif
44
45#ifndef MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
46# define MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS true
47#endif
48
49#ifndef MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
50# define MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM false
51#endif
52
53#ifndef MAGIC_KEY_HELP
54# define MAGIC_KEY_HELP H
55#endif
56
57#ifndef MAGIC_KEY_HELP_ALT
58# define MAGIC_KEY_HELP_ALT SLASH
59#endif
60
61#ifndef MAGIC_KEY_DEBUG
62# define MAGIC_KEY_DEBUG D
63#endif
64
65#ifndef MAGIC_KEY_DEBUG_MATRIX
66# define MAGIC_KEY_DEBUG_MATRIX X
67#endif
68
69#ifndef MAGIC_KEY_DEBUG_KBD
70# define MAGIC_KEY_DEBUG_KBD K
71#endif
72
73#ifndef MAGIC_KEY_DEBUG_MOUSE
74# define MAGIC_KEY_DEBUG_MOUSE M
75#endif
76
77#ifndef MAGIC_KEY_VERSION
78# define MAGIC_KEY_VERSION V
79#endif
80
81#ifndef MAGIC_KEY_STATUS
82# define MAGIC_KEY_STATUS S
83#endif
84
85#ifndef MAGIC_KEY_CONSOLE
86# define MAGIC_KEY_CONSOLE C
87#endif
88
89#ifndef MAGIC_KEY_LAYER0
90# define MAGIC_KEY_LAYER0 0
91#endif
92
93#ifndef MAGIC_KEY_LAYER0_ALT
94# define MAGIC_KEY_LAYER0_ALT GRAVE
95#endif
96
97#ifndef MAGIC_KEY_LAYER1
98# define MAGIC_KEY_LAYER1 1
99#endif
100
101#ifndef MAGIC_KEY_LAYER2
102# define MAGIC_KEY_LAYER2 2
103#endif
104
105#ifndef MAGIC_KEY_LAYER3
106# define MAGIC_KEY_LAYER3 3
107#endif
108
109#ifndef MAGIC_KEY_LAYER4
110# define MAGIC_KEY_LAYER4 4
111#endif
112
113#ifndef MAGIC_KEY_LAYER5
114# define MAGIC_KEY_LAYER5 5
115#endif
116
117#ifndef MAGIC_KEY_LAYER6
118# define MAGIC_KEY_LAYER6 6
119#endif
120
121#ifndef MAGIC_KEY_LAYER7
122# define MAGIC_KEY_LAYER7 7
123#endif
124
125#ifndef MAGIC_KEY_LAYER8
126# define MAGIC_KEY_LAYER8 8
127#endif
128
129#ifndef MAGIC_KEY_LAYER9
130# define MAGIC_KEY_LAYER9 9
131#endif
132
133#ifndef MAGIC_KEY_BOOTLOADER
134# define MAGIC_KEY_BOOTLOADER B
135#endif
136
137#ifndef MAGIC_KEY_BOOTLOADER_ALT
138# define MAGIC_KEY_BOOTLOADER_ALT ESC
139#endif
140
141#ifndef MAGIC_KEY_LOCK
142# define MAGIC_KEY_LOCK CAPS
143#endif
144
145#ifndef MAGIC_KEY_EEPROM
146# define MAGIC_KEY_EEPROM E
147#endif
148
149#ifndef MAGIC_KEY_EEPROM_CLEAR
150# define MAGIC_KEY_EEPROM_CLEAR BSPACE
151#endif
152
153#ifndef MAGIC_KEY_NKRO
154# define MAGIC_KEY_NKRO N
155#endif
156
157#ifndef MAGIC_KEY_SLEEP_LED
158# define MAGIC_KEY_SLEEP_LED Z
159
160#endif
161
162#define XMAGIC_KC(key) KC_##key
163#define MAGIC_KC(key) XMAGIC_KC(key)
diff --git a/quantum/led.h b/quantum/led.h
new file mode 100644
index 000000000..0fe38ea03
--- /dev/null
+++ b/quantum/led.h
@@ -0,0 +1,54 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#pragma once
19
20#include <stdint.h>
21#include <stdbool.h>
22
23/* FIXME: Add doxygen comments here. */
24
25/* keyboard LEDs */
26#define USB_LED_NUM_LOCK 0
27#define USB_LED_CAPS_LOCK 1
28#define USB_LED_SCROLL_LOCK 2
29#define USB_LED_COMPOSE 3
30#define USB_LED_KANA 4
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36typedef union {
37 uint8_t raw;
38 struct {
39 bool num_lock : 1;
40 bool caps_lock : 1;
41 bool scroll_lock : 1;
42 bool compose : 1;
43 bool kana : 1;
44 uint8_t reserved : 3;
45 };
46} led_t;
47
48void led_set(uint8_t usb_led);
49
50void led_init_ports(void);
51
52#ifdef __cplusplus
53}
54#endif
diff --git a/quantum/matrix.h b/quantum/matrix.h
new file mode 100644
index 000000000..ce57010a4
--- /dev/null
+++ b/quantum/matrix.h
@@ -0,0 +1,79 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#pragma once
19
20#include <stdint.h>
21#include <stdbool.h>
22
23#if (MATRIX_COLS <= 8)
24typedef uint8_t matrix_row_t;
25#elif (MATRIX_COLS <= 16)
26typedef uint16_t matrix_row_t;
27#elif (MATRIX_COLS <= 32)
28typedef uint32_t matrix_row_t;
29#else
30# error "MATRIX_COLS: invalid value"
31#endif
32
33#define MATRIX_ROW_SHIFTER ((matrix_row_t)1)
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39/* number of matrix rows */
40uint8_t matrix_rows(void);
41/* number of matrix columns */
42uint8_t matrix_cols(void);
43/* should be called at early stage of startup before matrix_init.(optional) */
44void matrix_setup(void);
45/* intialize matrix for scaning. */
46void matrix_init(void);
47/* scan all key states on matrix */
48uint8_t matrix_scan(void);
49/* whether modified from previous scan. used after matrix_scan. */
50bool matrix_is_modified(void) __attribute__((deprecated));
51/* whether a switch is on */
52bool matrix_is_on(uint8_t row, uint8_t col);
53/* matrix state on row */
54matrix_row_t matrix_get_row(uint8_t row);
55/* print matrix for debug */
56void matrix_print(void);
57/* delay between changing matrix pin state and reading values */
58void matrix_output_select_delay(void);
59void matrix_output_unselect_delay(void);
60/* only for backwards compatibility. delay between changing matrix pin state and reading values */
61void matrix_io_delay(void);
62
63/* power control */
64void matrix_power_up(void);
65void matrix_power_down(void);
66
67/* executes code for Quantum */
68void matrix_init_quantum(void);
69void matrix_scan_quantum(void);
70
71void matrix_init_kb(void);
72void matrix_scan_kb(void);
73
74void matrix_init_user(void);
75void matrix_scan_user(void);
76
77#ifdef __cplusplus
78}
79#endif
diff --git a/quantum/ring_buffer.h b/quantum/ring_buffer.h
new file mode 100644
index 000000000..284745ca8
--- /dev/null
+++ b/quantum/ring_buffer.h
@@ -0,0 +1,44 @@
1#pragma once
2
3#include <util/atomic.h>
4#include <stdint.h>
5#include <stdbool.h>
6
7#ifndef RBUF_SIZE
8# define RBUF_SIZE 32
9#endif
10
11static uint8_t rbuf[RBUF_SIZE];
12static uint8_t rbuf_head = 0;
13static uint8_t rbuf_tail = 0;
14static inline bool rbuf_enqueue(uint8_t data) {
15 bool ret = false;
16 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
17 uint8_t next = (rbuf_head + 1) % RBUF_SIZE;
18 if (next != rbuf_tail) {
19 rbuf[rbuf_head] = data;
20 rbuf_head = next;
21 ret = true;
22 }
23 }
24 return ret;
25}
26static inline uint8_t rbuf_dequeue(void) {
27 uint8_t val = 0;
28 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
29 if (rbuf_head != rbuf_tail) {
30 val = rbuf[rbuf_tail];
31 rbuf_tail = (rbuf_tail + 1) % RBUF_SIZE;
32 }
33 }
34
35 return val;
36}
37static inline bool rbuf_has_data(void) {
38 bool has_data;
39 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { has_data = (rbuf_head != rbuf_tail); }
40 return has_data;
41}
42static inline void rbuf_clear(void) {
43 ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { rbuf_head = rbuf_tail = 0; }
44}
diff --git a/quantum/split_common/transport.h b/quantum/split_common/transport.h
index f3e752bf9..c667bfab8 100644
--- a/quantum/split_common/transport.h
+++ b/quantum/split_common/transport.h
@@ -1,6 +1,6 @@
1#pragma once 1#pragma once
2 2
3#include "common/matrix.h" 3#include "matrix.h"
4 4
5void transport_master_init(void); 5void transport_master_init(void);
6void transport_slave_init(void); 6void transport_slave_init(void);
diff --git a/quantum/util.h b/quantum/util.h
new file mode 100644
index 000000000..bef3b9abe
--- /dev/null
+++ b/quantum/util.h
@@ -0,0 +1,26 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17#pragma once
18
19#include "bitwise.h"
20
21// convert to L string
22#define LSTR(s) XLSTR(s)
23#define XLSTR(s) L## #s
24// convert to string
25#define STR(s) XSTR(s)
26#define XSTR(s) #s