]> git.donarmstrong.com Git - tmk_firmware.git/blob - common/command.c
Fix modifier stuck of Lock command(#127)
[tmk_firmware.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 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\n----- Command Help -----\n");
116     print("c:   enter console mode\n");
117     print("d:   toggle debug enable\n");
118     print("x:   toggle matrix debug\n");
119     print("k:   toggle keyboard debug\n");
120     print("m:   toggle mouse debug\n");
121 #ifdef SLEEP_LED_ENABLE
122     print("z:   toggle sleep LED test\n");
123 #endif
124     print("v:   print device version & info\n");
125     print("t:   print timer count\n");
126     print("s:   print status\n");
127     print("e:   print eeprom config\n");
128 #ifdef NKRO_ENABLE
129     print("n:   toggle NKRO\n");
130 #endif
131     print("0/F10:       switch to Layer0 \n");
132     print("1/F1:        switch to Layer1 \n");
133     print("2/F2:        switch to Layer2 \n");
134     print("3/F3:        switch to Layer3 \n");
135     print("4/F4:        switch to Layer4 \n");
136     print("PScr:        power down/remote wake-up\n");
137     print("Caps:        Lock Keyboard(Child Proof)\n");
138     print("Paus:        jump to bootloader\n");
139 }
140
141 #ifdef BOOTMAGIC_ENABLE
142 static void print_eeconfig(void)
143 {
144     print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
145
146     debug_config_t dc;
147     dc.raw = eeconfig_read_debug();
148     print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
149     print(".enable: "); print_dec(dc.enable); print("\n");
150     print(".matrix: "); print_dec(dc.matrix); print("\n");
151     print(".keyboard: "); print_dec(dc.keyboard); print("\n");
152     print(".mouse: "); print_dec(dc.mouse); print("\n");
153
154     keymap_config_t kc;
155     kc.raw = eeconfig_read_keymap();
156     print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
157     print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
158     print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
159     print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
160     print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
161     print(".no_gui: "); print_dec(kc.no_gui); print("\n");
162     print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
163     print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
164     print(".nkro: "); print_dec(kc.nkro); print("\n");
165
166 #ifdef BACKLIGHT_ENABLE
167     backlight_config_t bc;
168     bc.raw = eeconfig_read_backlight();
169     print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
170     print(".enable: "); print_dec(bc.enable); print("\n");
171     print(".level: "); print_dec(bc.level); print("\n");
172 #endif
173 }
174 #endif
175
176 static bool command_common(uint8_t code)
177 {
178     static host_driver_t *host_driver = 0;
179     switch (code) {
180 #ifdef SLEEP_LED_ENABLE
181         case KC_Z:
182             // test breathing sleep LED
183             print("Sleep LED test\n");
184             sleep_led_toggle();
185             led_set(host_keyboard_leds());
186             break;
187 #endif
188 #ifdef BOOTMAGIC_ENABLE
189         case KC_E:
190             print("eeconfig:\n");
191             print_eeconfig();
192             break;
193 #endif
194         case KC_CAPSLOCK:
195             if (host_get_driver()) {
196                 host_driver = host_get_driver();
197                 clear_keyboard();
198                 host_set_driver(0);
199                 print("Locked.\n");
200             } else {
201                 host_set_driver(host_driver);
202                 print("Unlocked.\n");
203             }
204             break;
205         case KC_H:
206         case KC_SLASH: /* ? */
207             command_common_help();
208             break;
209         case KC_C:
210             debug_matrix   = false;
211             debug_keyboard = false;
212             debug_mouse    = false;
213             debug_enable   = false;
214             command_console_help();
215             print("\nEnter Console Mode\n");
216             print("C> ");
217             command_state = CONSOLE;
218             break;
219         case KC_PAUSE:
220             clear_keyboard();
221             print("\n\nJump to bootloader... ");
222             _delay_ms(1000);
223             bootloader_jump(); // not return
224             print("not supported.\n");
225             break;
226         case KC_D:
227             if (debug_enable) {
228                 print("\nDEBUG: disabled.\n");
229                 debug_matrix   = false;
230                 debug_keyboard = false;
231                 debug_mouse    = false;
232                 debug_enable   = false;
233             } else {
234                 print("\nDEBUG: enabled.\n");
235                 debug_enable   = true;
236             }
237             break;
238         case KC_X: // debug matrix toggle
239             debug_matrix = !debug_matrix;
240             if (debug_matrix) {
241                 print("\nDEBUG: matrix enabled.\n");
242                 debug_enable = true;
243             } else {
244                 print("\nDEBUG: matrix disabled.\n");
245             }
246             break;
247         case KC_K: // debug keyboard toggle
248             debug_keyboard = !debug_keyboard;
249             if (debug_keyboard) {
250                 print("\nDEBUG: keyboard enabled.\n");
251                 debug_enable = true;
252             } else {
253                 print("\nDEBUG: keyboard disabled.\n");
254             }
255             break;
256         case KC_M: // debug mouse toggle
257             debug_mouse = !debug_mouse;
258             if (debug_mouse) {
259                 print("\nDEBUG: mouse enabled.\n");
260                 debug_enable = true;
261             } else {
262                 print("\nDEBUG: mouse disabled.\n");
263             }
264             break;
265         case KC_V: // print version & information
266             print("\n\n----- Version -----\n");
267             print("DESC: " STR(DESCRIPTION) "\n");
268             print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
269                   "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
270                   "VER: " STR(DEVICE_VER) "\n");
271             print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
272             /* build options */
273             print("OPTIONS:"
274 #ifdef PROTOCOL_PJRC
275             " PJRC"
276 #endif
277 #ifdef PROTOCOL_LUFA
278             " LUFA"
279 #endif
280 #ifdef PROTOCOL_VUSB
281             " VUSB"
282 #endif
283 #ifdef BOOTMAGIC_ENABLE
284             " BOOTMAGIC"
285 #endif
286 #ifdef MOUSEKEY_ENABLE
287             " MOUSEKEY"
288 #endif
289 #ifdef EXTRAKEY_ENABLE
290             " EXTRAKEY"
291 #endif
292 #ifdef CONSOLE_ENABLE
293             " CONSOLE"
294 #endif
295 #ifdef COMMAND_ENABLE
296             " COMMAND"
297 #endif
298 #ifdef NKRO_ENABLE
299             " NKRO"
300 #endif
301 #ifdef KEYMAP_SECTION_ENABLE
302             " KEYMAP_SECTION"
303 #endif
304             " " STR(BOOTLOADER_SIZE) "\n");
305
306             print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__) 
307                   " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
308                   " AVR_ARCH: avr" STR(__AVR_ARCH__) "\n");
309             break;
310         case KC_T: // print timer
311             print_val_hex32(timer_count);
312             break;
313         case KC_S:
314             print("\n\n----- Status -----\n");
315             print_val_hex8(host_keyboard_leds());
316             print_val_hex8(keyboard_protocol);
317             print_val_hex8(keyboard_idle);
318 #ifdef PROTOCOL_PJRC
319             print_val_hex8(UDCON);
320             print_val_hex8(UDIEN);
321             print_val_hex8(UDINT);
322             print_val_hex8(usb_keyboard_leds);
323             print_val_hex8(usb_keyboard_idle_count);
324 #endif
325
326 #ifdef PROTOCOL_PJRC
327 #   if USB_COUNT_SOF
328             print_val_hex8(usbSofCount);
329 #   endif
330 #endif
331             break;
332 #ifdef NKRO_ENABLE
333         case KC_N:
334             clear_keyboard(); //Prevents stuck keys.
335             keyboard_nkro = !keyboard_nkro;
336             if (keyboard_nkro)
337                 print("NKRO: enabled\n");
338             else
339                 print("NKRO: disabled\n");
340             break;
341 #endif
342 #ifdef EXTRAKEY_ENABLE
343         case KC_PSCREEN:
344             // TODO: Power key should take this feature? otherwise any key during suspend.
345 #ifdef PROTOCOL_PJRC
346             if (suspend && remote_wakeup) {
347                 usb_remote_wakeup();
348             } else {
349                 host_system_send(SYSTEM_POWER_DOWN);
350                 host_system_send(0);
351                 _delay_ms(500);
352             }
353 #else
354             host_system_send(SYSTEM_POWER_DOWN);
355             _delay_ms(100);
356             host_system_send(0);
357             _delay_ms(500);
358 #endif
359             break;
360 #endif
361         case KC_ESC:
362         case KC_GRV:
363         case KC_0:
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_F12:
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\n----- Console Help -----\n");
386     print("ESC/q:       quit\n");
387 #ifdef MOUSEKEY_ENABLE
388     print("m:   mousekey\n");
389 #endif
390 }
391
392 static bool command_console(uint8_t code)
393 {
394     switch (code) {
395         case KC_H:
396         case KC_SLASH: /* ? */
397             command_console_help();
398             break;
399         case KC_Q:
400         case KC_ESC:
401             print("\nQuit Console Mode\n");
402             command_state = ONESHOT;
403             return false;
404 #ifdef MOUSEKEY_ENABLE
405         case KC_M:
406             mousekey_console_help();
407             print("\nEnter Mousekey Console\n");
408             print("M0>");
409             command_state = MOUSEKEY;
410             return true;
411 #endif
412         default:
413             print("?");
414             return false;
415     }
416     print("C> ");
417     return true;
418 }
419
420
421 #ifdef MOUSEKEY_ENABLE
422 /***********************************************************
423  * Mousekey console
424  ***********************************************************/
425 static uint8_t mousekey_param = 0;
426
427 static void mousekey_param_print(void)
428 {
429     print("\n\n----- Mousekey Parameters -----\n");
430     print("1: mk_delay(*10ms): "); pdec(mk_delay); print("\n");
431     print("2: mk_interval(ms): "); pdec(mk_interval); print("\n");
432     print("3: mk_max_speed: "); pdec(mk_max_speed); print("\n");
433     print("4: mk_time_to_max: "); pdec(mk_time_to_max); print("\n");
434     print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
435     print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
436 }
437
438 #define PRINT_SET_VAL(v)  print(#v " = "); print_dec(v); print("\n");
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\n----- Mousekey Parameters Help -----\n");
538     print("ESC/q:       quit\n");
539     print("1:   select mk_delay(*10ms)\n");
540     print("2:   select mk_interval(ms)\n");
541     print("3:   select mk_max_speed\n");
542     print("4:   select mk_time_to_max\n");
543     print("5:   select mk_wheel_max_speed\n");
544     print("6:   select mk_wheel_time_to_max\n");
545     print("p:   print prameters\n");
546     print("d:   set default values\n");
547     print("up:  increase prameters(+1)\n");
548     print("down:        decrease prameters(-1)\n");
549     print("pgup:        increase prameters(+10)\n");
550     print("pgdown:      decrease prameters(-10)\n");
551     print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
552     print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA);
553     print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA); print("\n");
554     print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
555 }
556
557 static bool mousekey_console(uint8_t code)
558 {
559     switch (code) {
560         case KC_H:
561         case KC_SLASH: /* ? */
562             mousekey_console_help();
563             break;
564         case KC_Q:
565         case KC_ESC:
566             mousekey_param = 0;
567             print("\nQuit Mousekey Console\n");
568             print("C> ");
569             command_state = CONSOLE;
570             return false;
571         case KC_P:
572             mousekey_param_print();
573             break;
574         case KC_1:
575         case KC_2:
576         case KC_3:
577         case KC_4:
578         case KC_5:
579         case KC_6:
580         case KC_7:
581         case KC_8:
582         case KC_9:
583         case KC_0:
584             mousekey_param = numkey2num(code);
585             print("selected parameter: "); pdec(mousekey_param); print("\n");
586             break;
587         case KC_UP:
588             mousekey_param_inc(mousekey_param, 1);
589             break;
590         case KC_DOWN:
591             mousekey_param_dec(mousekey_param, 1);
592             break;
593         case KC_PGUP:
594             mousekey_param_inc(mousekey_param, 10);
595             break;
596         case KC_PGDN:
597             mousekey_param_dec(mousekey_param, 10);
598             break;
599         case KC_D:
600             mk_delay = MOUSEKEY_DELAY/10;
601             mk_interval = MOUSEKEY_INTERVAL;
602             mk_max_speed = MOUSEKEY_MAX_SPEED;
603             mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
604             mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
605             mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
606             print("set default values.\n");
607             break;
608         default:
609             print("?");
610             return false;
611     }
612     print("M"); pdec(mousekey_param); print("> ");
613     return true;
614 }
615 #endif
616
617
618 /***********************************************************
619  * Utilities
620  ***********************************************************/
621 static uint8_t numkey2num(uint8_t code)
622 {
623     switch (code) {
624         case KC_1: return 1;
625         case KC_2: return 2;
626         case KC_3: return 3;
627         case KC_4: return 4;
628         case KC_5: return 5;
629         case KC_6: return 6;
630         case KC_7: return 7;
631         case KC_8: return 8;
632         case KC_9: return 9;
633         case KC_0: return 0;
634     }
635     return 0;
636 }
637
638 static void switch_default_layer(uint8_t layer)
639 {
640     print("switch_default_layer: "); print_dec(biton32(default_layer_state));
641     print(" to "); print_dec(layer); print("\n");
642     default_layer_set(1UL<<layer);
643     clear_keyboard();
644 }