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