]> git.gir.st - tmk_keyboard.git/blob - tmk_core/common/command.c
Reduce code size of magic commands
[tmk_keyboard.git] / tmk_core / common / command.c
1 /*
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include <stdint.h>
18 #include <stdbool.h>
19 #include <util/delay.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 "backlight.h"
36
37 #ifdef MOUSEKEY_ENABLE
38 #include "mousekey.h"
39 #endif
40
41 #ifdef PROTOCOL_PJRC
42 # include "usb_keyboard.h"
43 # ifdef EXTRAKEY_ENABLE
44 # include "usb_extra.h"
45 # endif
46 #endif
47
48 #ifdef PROTOCOL_VUSB
49 # include "usbdrv.h"
50 #endif
51
52
53 static bool command_common(uint8_t code);
54 static void command_common_help(void);
55 static bool command_console(uint8_t code);
56 static void command_console_help(void);
57 #ifdef MOUSEKEY_ENABLE
58 static bool mousekey_console(uint8_t code);
59 static void mousekey_console_help(void);
60 #endif
61
62 static uint8_t numkey2num(uint8_t code);
63 static void switch_default_layer(uint8_t layer);
64
65
66 command_state_t command_state = ONESHOT;
67
68
69 bool command_proc(uint8_t code)
70 {
71 switch (command_state) {
72 case ONESHOT:
73 if (!IS_COMMAND())
74 return false;
75 return (command_extra(code) || command_common(code));
76 break;
77 case CONSOLE:
78 if (IS_COMMAND())
79 return (command_extra(code) || command_common(code));
80 else
81 return (command_console_extra(code) || command_console(code));
82 break;
83 #ifdef MOUSEKEY_ENABLE
84 case MOUSEKEY:
85 mousekey_console(code);
86 break;
87 #endif
88 default:
89 command_state = ONESHOT;
90 return false;
91 }
92 return true;
93 }
94
95 /* TODO: Refactoring is needed. */
96 /* This allows to define extra commands. return false when not processed. */
97 bool command_extra(uint8_t code) __attribute__ ((weak));
98 bool command_extra(uint8_t code)
99 {
100 return false;
101 }
102
103 bool command_console_extra(uint8_t code) __attribute__ ((weak));
104 bool command_console_extra(uint8_t code)
105 {
106 return false;
107 }
108
109
110 /***********************************************************
111 * Command common
112 ***********************************************************/
113 static void command_common_help(void)
114 {
115 print("\n\t- Magic -\n"
116 "d: debug\n"
117 "x: debug matrix\n"
118 "k: debug keyboard\n"
119 "m: debug mouse\n"
120 "v: version\n"
121 "s: status\n"
122 "c: console mode\n"
123 "0-4: layer0-4(F10-F4)\n"
124 "Paus: bootloader\n"
125
126 #ifdef KEYBOARD_LOCK_ENABLE
127 "Caps: Lock\n"
128 #endif
129
130 #ifdef BOOTMAGIC_ENABLE
131 "e: eeprom\n"
132 #endif
133
134 #ifdef NKRO_ENABLE
135 "n: NKRO\n"
136 #endif
137
138 #ifdef SLEEP_LED_ENABLE
139 "z: sleep LED test\n"
140 #endif
141 );
142 }
143
144 #ifdef BOOTMAGIC_ENABLE
145 static void print_eeconfig(void)
146 {
147 print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
148
149 debug_config_t dc;
150 dc.raw = eeconfig_read_debug();
151 print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
152 print(".enable: "); print_dec(dc.enable); print("\n");
153 print(".matrix: "); print_dec(dc.matrix); print("\n");
154 print(".keyboard: "); print_dec(dc.keyboard); print("\n");
155 print(".mouse: "); print_dec(dc.mouse); print("\n");
156
157 keymap_config_t kc;
158 kc.raw = eeconfig_read_keymap();
159 print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
160 print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
161 print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
162 print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
163 print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
164 print(".no_gui: "); print_dec(kc.no_gui); print("\n");
165 print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
166 print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
167 print(".nkro: "); print_dec(kc.nkro); print("\n");
168
169 #ifdef BACKLIGHT_ENABLE
170 backlight_config_t bc;
171 bc.raw = eeconfig_read_backlight();
172 print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
173 print(".enable: "); print_dec(bc.enable); print("\n");
174 print(".level: "); print_dec(bc.level); print("\n");
175 #endif
176 }
177 #endif
178
179 static bool command_common(uint8_t code)
180 {
181 static host_driver_t *host_driver = 0;
182 switch (code) {
183 #ifdef SLEEP_LED_ENABLE
184 case KC_Z:
185 // test breathing sleep LED
186 print("Sleep LED test\n");
187 sleep_led_toggle();
188 led_set(host_keyboard_leds());
189 break;
190 #endif
191 #ifdef BOOTMAGIC_ENABLE
192 case KC_E:
193 print("eeconfig:\n");
194 print_eeconfig();
195 break;
196 #endif
197 #ifdef KEYBOARD_LOCK_ENABLE
198 case KC_CAPSLOCK:
199 if (host_get_driver()) {
200 host_driver = host_get_driver();
201 clear_keyboard();
202 host_set_driver(0);
203 print("Locked.\n");
204 } else {
205 host_set_driver(host_driver);
206 print("Unlocked.\n");
207 }
208 break;
209 #endif
210 case KC_H:
211 case KC_SLASH: /* ? */
212 command_common_help();
213 break;
214 case KC_C:
215 debug_matrix = false;
216 debug_keyboard = false;
217 debug_mouse = false;
218 debug_enable = false;
219 command_console_help();
220 print("C> ");
221 command_state = CONSOLE;
222 break;
223 case KC_PAUSE:
224 clear_keyboard();
225 print("\n\nbootloader... ");
226 _delay_ms(1000);
227 bootloader_jump(); // not return
228 break;
229 case KC_D:
230 if (debug_enable) {
231 print("\ndebug: on\n");
232 debug_matrix = false;
233 debug_keyboard = false;
234 debug_mouse = false;
235 debug_enable = false;
236 } else {
237 print("\ndebug: off\n");
238 debug_enable = true;
239 }
240 break;
241 case KC_X: // debug matrix toggle
242 debug_matrix = !debug_matrix;
243 if (debug_matrix) {
244 print("\nmatrix: on\n");
245 debug_enable = true;
246 } else {
247 print("\nmatrix: off\n");
248 }
249 break;
250 case KC_K: // debug keyboard toggle
251 debug_keyboard = !debug_keyboard;
252 if (debug_keyboard) {
253 print("\nkeyboard: on\n");
254 debug_enable = true;
255 } else {
256 print("\nkeyboard: off\n");
257 }
258 break;
259 case KC_M: // debug mouse toggle
260 debug_mouse = !debug_mouse;
261 if (debug_mouse) {
262 print("\nmouse: on\n");
263 debug_enable = true;
264 } else {
265 print("\nmouse: off\n");
266 }
267 break;
268 case KC_V: // print version & information
269 print("\n\t- Version -\n");
270 print("DESC: " STR(DESCRIPTION) "\n");
271 print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
272 "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
273 "VER: " STR(DEVICE_VER) "\n");
274 print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
275 /* build options */
276 print("OPTIONS:"
277 #ifdef PROTOCOL_PJRC
278 " PJRC"
279 #endif
280 #ifdef PROTOCOL_LUFA
281 " LUFA"
282 #endif
283 #ifdef PROTOCOL_VUSB
284 " VUSB"
285 #endif
286 #ifdef BOOTMAGIC_ENABLE
287 " BOOTMAGIC"
288 #endif
289 #ifdef MOUSEKEY_ENABLE
290 " MOUSEKEY"
291 #endif
292 #ifdef EXTRAKEY_ENABLE
293 " EXTRAKEY"
294 #endif
295 #ifdef CONSOLE_ENABLE
296 " CONSOLE"
297 #endif
298 #ifdef COMMAND_ENABLE
299 " COMMAND"
300 #endif
301 #ifdef NKRO_ENABLE
302 " NKRO"
303 #endif
304 #ifdef KEYMAP_SECTION_ENABLE
305 " KEYMAP_SECTION"
306 #endif
307 " " STR(BOOTLOADER_SIZE) "\n");
308
309 print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
310 " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
311 " AVR_ARCH: avr" STR(__AVR_ARCH__) "\n");
312 break;
313 case KC_S:
314 print("\n\t- Status -\n");
315 print_val_hex8(host_keyboard_leds());
316 print_val_hex8(keyboard_protocol);
317 print_val_hex8(keyboard_idle);
318 print_val_hex32(timer_count);
319
320 #ifdef PROTOCOL_PJRC
321 print_val_hex8(UDCON);
322 print_val_hex8(UDIEN);
323 print_val_hex8(UDINT);
324 print_val_hex8(usb_keyboard_leds);
325 print_val_hex8(usb_keyboard_idle_count);
326 #endif
327
328 #ifdef PROTOCOL_PJRC
329 # if USB_COUNT_SOF
330 print_val_hex8(usbSofCount);
331 # endif
332 #endif
333 break;
334 #ifdef NKRO_ENABLE
335 case KC_N:
336 clear_keyboard(); //Prevents stuck keys.
337 keyboard_nkro = !keyboard_nkro;
338 if (keyboard_nkro)
339 print("NKRO: on\n");
340 else
341 print("NKRO: off\n");
342 break;
343 #endif
344 case KC_ESC:
345 case KC_GRV:
346 case KC_0:
347 case KC_F10:
348 switch_default_layer(0);
349 break;
350 case KC_1 ... KC_9:
351 switch_default_layer((code - KC_1) + 1);
352 break;
353 case KC_F1 ... KC_F9:
354 switch_default_layer((code - KC_F1) + 1);
355 break;
356 default:
357 print("?");
358 return false;
359 }
360 return true;
361 }
362
363
364 /***********************************************************
365 * Command console
366 ***********************************************************/
367 static void command_console_help(void)
368 {
369 print("\n\t- Console -\n"
370 "ESC/q: quit\n"
371 #ifdef MOUSEKEY_ENABLE
372 "m: mousekey\n"
373 #endif
374 );
375 }
376
377 static bool command_console(uint8_t code)
378 {
379 switch (code) {
380 case KC_H:
381 case KC_SLASH: /* ? */
382 command_console_help();
383 break;
384 case KC_Q:
385 case KC_ESC:
386 command_state = ONESHOT;
387 return false;
388 #ifdef MOUSEKEY_ENABLE
389 case KC_M:
390 mousekey_console_help();
391 print("M> ");
392 command_state = MOUSEKEY;
393 return true;
394 #endif
395 default:
396 print("?");
397 return false;
398 }
399 print("C> ");
400 return true;
401 }
402
403
404 #ifdef MOUSEKEY_ENABLE
405 /***********************************************************
406 * Mousekey console
407 ***********************************************************/
408 static uint8_t mousekey_param = 0;
409
410 static void mousekey_param_print(void)
411 {
412 print("\n\t- Values -\n");
413 print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
414 print("2: interval(ms): "); pdec(mk_interval); print("\n");
415 print("3: max_speed: "); pdec(mk_max_speed); print("\n");
416 print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
417 print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
418 print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
419 }
420
421 //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
422 #define PRINT_SET_VAL(v) xprintf(#v " = %d\n", (v))
423 static void mousekey_param_inc(uint8_t param, uint8_t inc)
424 {
425 switch (param) {
426 case 1:
427 if (mk_delay + inc < UINT8_MAX)
428 mk_delay += inc;
429 else
430 mk_delay = UINT8_MAX;
431 PRINT_SET_VAL(mk_delay);
432 break;
433 case 2:
434 if (mk_interval + inc < UINT8_MAX)
435 mk_interval += inc;
436 else
437 mk_interval = UINT8_MAX;
438 PRINT_SET_VAL(mk_interval);
439 break;
440 case 3:
441 if (mk_max_speed + inc < UINT8_MAX)
442 mk_max_speed += inc;
443 else
444 mk_max_speed = UINT8_MAX;
445 PRINT_SET_VAL(mk_max_speed);
446 break;
447 case 4:
448 if (mk_time_to_max + inc < UINT8_MAX)
449 mk_time_to_max += inc;
450 else
451 mk_time_to_max = UINT8_MAX;
452 PRINT_SET_VAL(mk_time_to_max);
453 break;
454 case 5:
455 if (mk_wheel_max_speed + inc < UINT8_MAX)
456 mk_wheel_max_speed += inc;
457 else
458 mk_wheel_max_speed = UINT8_MAX;
459 PRINT_SET_VAL(mk_wheel_max_speed);
460 break;
461 case 6:
462 if (mk_wheel_time_to_max + inc < UINT8_MAX)
463 mk_wheel_time_to_max += inc;
464 else
465 mk_wheel_time_to_max = UINT8_MAX;
466 PRINT_SET_VAL(mk_wheel_time_to_max);
467 break;
468 }
469 }
470
471 static void mousekey_param_dec(uint8_t param, uint8_t dec)
472 {
473 switch (param) {
474 case 1:
475 if (mk_delay > dec)
476 mk_delay -= dec;
477 else
478 mk_delay = 0;
479 PRINT_SET_VAL(mk_delay);
480 break;
481 case 2:
482 if (mk_interval > dec)
483 mk_interval -= dec;
484 else
485 mk_interval = 0;
486 PRINT_SET_VAL(mk_interval);
487 break;
488 case 3:
489 if (mk_max_speed > dec)
490 mk_max_speed -= dec;
491 else
492 mk_max_speed = 0;
493 PRINT_SET_VAL(mk_max_speed);
494 break;
495 case 4:
496 if (mk_time_to_max > dec)
497 mk_time_to_max -= dec;
498 else
499 mk_time_to_max = 0;
500 PRINT_SET_VAL(mk_time_to_max);
501 break;
502 case 5:
503 if (mk_wheel_max_speed > dec)
504 mk_wheel_max_speed -= dec;
505 else
506 mk_wheel_max_speed = 0;
507 PRINT_SET_VAL(mk_wheel_max_speed);
508 break;
509 case 6:
510 if (mk_wheel_time_to_max > dec)
511 mk_wheel_time_to_max -= dec;
512 else
513 mk_wheel_time_to_max = 0;
514 PRINT_SET_VAL(mk_wheel_time_to_max);
515 break;
516 }
517 }
518
519 static void mousekey_console_help(void)
520 {
521 print("\n\t- Mousekey -\n"
522 "ESC/q: quit\n"
523 "1: delay(*10ms)\n"
524 "2: interval(ms)\n"
525 "3: max_speed\n"
526 "4: time_to_max\n"
527 "5: wheel_max_speed\n"
528 "6: wheel_time_to_max\n"
529 "\n"
530 "p: print values\n"
531 "d: set defaults\n"
532 "up: +1\n"
533 "down: -1\n"
534 "pgup: +10\n"
535 "pgdown: -10\n"
536 "\n"
537 "speed = delta * max_speed * (repeat / time_to_max)\n");
538 xprintf("where delta: cursor=%d, wheel=%d\n"
539 "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
540 }
541
542 static bool mousekey_console(uint8_t code)
543 {
544 switch (code) {
545 case KC_H:
546 case KC_SLASH: /* ? */
547 mousekey_console_help();
548 break;
549 case KC_Q:
550 case KC_ESC:
551 if (mousekey_param) {
552 mousekey_param = 0;
553 } else {
554 print("C> ");
555 command_state = CONSOLE;
556 return false;
557 }
558 break;
559 case KC_P:
560 mousekey_param_print();
561 break;
562 case KC_1:
563 case KC_2:
564 case KC_3:
565 case KC_4:
566 case KC_5:
567 case KC_6:
568 mousekey_param = numkey2num(code);
569 break;
570 case KC_UP:
571 mousekey_param_inc(mousekey_param, 1);
572 break;
573 case KC_DOWN:
574 mousekey_param_dec(mousekey_param, 1);
575 break;
576 case KC_PGUP:
577 mousekey_param_inc(mousekey_param, 10);
578 break;
579 case KC_PGDN:
580 mousekey_param_dec(mousekey_param, 10);
581 break;
582 case KC_D:
583 mk_delay = MOUSEKEY_DELAY/10;
584 mk_interval = MOUSEKEY_INTERVAL;
585 mk_max_speed = MOUSEKEY_MAX_SPEED;
586 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
587 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
588 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
589 print("set default\n");
590 break;
591 default:
592 print("?");
593 return false;
594 }
595 if (mousekey_param)
596 xprintf("M%d> ", mousekey_param);
597 else
598 print("M>" );
599 return true;
600 }
601 #endif
602
603
604 /***********************************************************
605 * Utilities
606 ***********************************************************/
607 static uint8_t numkey2num(uint8_t code)
608 {
609 switch (code) {
610 case KC_1: return 1;
611 case KC_2: return 2;
612 case KC_3: return 3;
613 case KC_4: return 4;
614 case KC_5: return 5;
615 case KC_6: return 6;
616 case KC_7: return 7;
617 case KC_8: return 8;
618 case KC_9: return 9;
619 case KC_0: return 0;
620 }
621 return 0;
622 }
623
624 static void switch_default_layer(uint8_t layer)
625 {
626 xprintf("L%d\n", layer);
627 default_layer_set(1UL<<layer);
628 clear_keyboard();
629 }
Imprint / Impressum