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