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