]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - tmk_core/common/command.c
Merge pull request #292 from obones/command_warning
[max/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 #ifdef KEYBOARD_LOCK_ENABLE
182     static host_driver_t *host_driver = 0;
183 #endif
184     switch (code) {
185 #ifdef SLEEP_LED_ENABLE
186         case KC_Z:
187             // test breathing sleep LED
188             print("Sleep LED test\n");
189             sleep_led_toggle();
190             led_set(host_keyboard_leds());
191             break;
192 #endif
193 #ifdef BOOTMAGIC_ENABLE
194         case KC_E:
195             print("eeconfig:\n");
196             print_eeconfig();
197             break;
198 #endif
199 #ifdef KEYBOARD_LOCK_ENABLE
200         case KC_CAPSLOCK:
201             if (host_get_driver()) {
202                 host_driver = host_get_driver();
203                 clear_keyboard();
204                 host_set_driver(0);
205                 print("Locked.\n");
206             } else {
207                 host_set_driver(host_driver);
208                 print("Unlocked.\n");
209             }
210             break;
211 #endif
212         case KC_H:
213         case KC_SLASH: /* ? */
214             command_common_help();
215             break;
216         case KC_C:
217             debug_matrix   = false;
218             debug_keyboard = false;
219             debug_mouse    = false;
220             debug_enable   = false;
221             command_console_help();
222             print("C> ");
223             command_state = CONSOLE;
224             break;
225         case KC_PAUSE:
226             clear_keyboard();
227             print("\n\nbootloader... ");
228             _delay_ms(1000);
229             bootloader_jump(); // not return
230             break;
231         case KC_D:
232             if (debug_enable) {
233                 print("\ndebug: off\n");
234                 debug_matrix   = false;
235                 debug_keyboard = false;
236                 debug_mouse    = false;
237                 debug_enable   = false;
238             } else {
239                 print("\ndebug: on\n");
240                 debug_enable   = true;
241             }
242             break;
243         case KC_X: // debug matrix toggle
244             debug_matrix = !debug_matrix;
245             if (debug_matrix) {
246                 print("\nmatrix: on\n");
247                 debug_enable = true;
248             } else {
249                 print("\nmatrix: off\n");
250             }
251             break;
252         case KC_K: // debug keyboard toggle
253             debug_keyboard = !debug_keyboard;
254             if (debug_keyboard) {
255                 print("\nkeyboard: on\n");
256                 debug_enable = true;
257             } else {
258                 print("\nkeyboard: off\n");
259             }
260             break;
261         case KC_M: // debug mouse toggle
262             debug_mouse = !debug_mouse;
263             if (debug_mouse) {
264                 print("\nmouse: on\n");
265                 debug_enable = true;
266             } else {
267                 print("\nmouse: off\n");
268             }
269             break;
270         case KC_V: // print version & information
271             print("\n\t- Version -\n");
272             print("DESC: " STR(DESCRIPTION) "\n");
273             print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
274                   "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
275                   "VER: " STR(DEVICE_VER) "\n");
276             print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
277             /* build options */
278             print("OPTIONS:"
279 #ifdef PROTOCOL_PJRC
280             " PJRC"
281 #endif
282 #ifdef PROTOCOL_LUFA
283             " LUFA"
284 #endif
285 #ifdef PROTOCOL_VUSB
286             " VUSB"
287 #endif
288 #ifdef BOOTMAGIC_ENABLE
289             " BOOTMAGIC"
290 #endif
291 #ifdef MOUSEKEY_ENABLE
292             " MOUSEKEY"
293 #endif
294 #ifdef EXTRAKEY_ENABLE
295             " EXTRAKEY"
296 #endif
297 #ifdef CONSOLE_ENABLE
298             " CONSOLE"
299 #endif
300 #ifdef COMMAND_ENABLE
301             " COMMAND"
302 #endif
303 #ifdef NKRO_ENABLE
304             " NKRO"
305 #endif
306 #ifdef KEYMAP_SECTION_ENABLE
307             " KEYMAP_SECTION"
308 #endif
309             " " STR(BOOTLOADER_SIZE) "\n");
310
311             print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
312                   " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
313                   " AVR_ARCH: avr" STR(__AVR_ARCH__) "\n");
314             break;
315         case KC_S:
316             print("\n\t- Status -\n");
317             print_val_hex8(host_keyboard_leds());
318             print_val_hex8(keyboard_protocol);
319             print_val_hex8(keyboard_idle);
320 #ifdef NKRO_ENABLE
321             print_val_hex8(keyboard_nkro);
322 #endif
323             print_val_hex32(timer_count);
324
325 #ifdef PROTOCOL_PJRC
326             print_val_hex8(UDCON);
327             print_val_hex8(UDIEN);
328             print_val_hex8(UDINT);
329             print_val_hex8(usb_keyboard_leds);
330             print_val_hex8(usb_keyboard_idle_count);
331 #endif
332
333 #ifdef PROTOCOL_PJRC
334 #   if USB_COUNT_SOF
335             print_val_hex8(usbSofCount);
336 #   endif
337 #endif
338             break;
339 #ifdef NKRO_ENABLE
340         case KC_N:
341             clear_keyboard(); //Prevents stuck keys.
342             keyboard_nkro = !keyboard_nkro;
343             if (keyboard_nkro)
344                 print("NKRO: on\n");
345             else
346                 print("NKRO: off\n");
347             break;
348 #endif
349         case KC_ESC:
350         case KC_GRV:
351         case KC_0:
352         case KC_F10:
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_F9:
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\t- Console -\n"
375           "ESC/q:       quit\n"
376 #ifdef MOUSEKEY_ENABLE
377           "m:   mousekey\n"
378 #endif
379     );
380 }
381
382 static bool command_console(uint8_t code)
383 {
384     switch (code) {
385         case KC_H:
386         case KC_SLASH: /* ? */
387             command_console_help();
388             break;
389         case KC_Q:
390         case KC_ESC:
391             command_state = ONESHOT;
392             return false;
393 #ifdef MOUSEKEY_ENABLE
394         case KC_M:
395             mousekey_console_help();
396             print("M> ");
397             command_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\t- Values -\n");
418     print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
419     print("2: interval(ms): "); pdec(mk_interval); print("\n");
420     print("3: max_speed: "); pdec(mk_max_speed); print("\n");
421     print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
422     print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
423     print("6: 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 #define PRINT_SET_VAL(v)  xprintf(#v " = %d\n", (v))
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\t- Mousekey -\n"
527           "ESC/q:       quit\n"
528           "1:   delay(*10ms)\n"
529           "2:   interval(ms)\n"
530           "3:   max_speed\n"
531           "4:   time_to_max\n"
532           "5:   wheel_max_speed\n"
533           "6:   wheel_time_to_max\n"
534           "\n"
535           "p:   print values\n"
536           "d:   set defaults\n"
537           "up:  +1\n"
538           "down:        -1\n"
539           "pgup:        +10\n"
540           "pgdown:      -10\n"
541           "\n"
542           "speed = delta * max_speed * (repeat / time_to_max)\n");
543     xprintf("where delta: cursor=%d, wheel=%d\n" 
544             "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA,  MOUSEKEY_WHEEL_DELTA);
545 }
546
547 static bool mousekey_console(uint8_t code)
548 {
549     switch (code) {
550         case KC_H:
551         case KC_SLASH: /* ? */
552             mousekey_console_help();
553             break;
554         case KC_Q:
555         case KC_ESC:
556             if (mousekey_param) {
557                 mousekey_param = 0;
558             } else {
559                 print("C> ");
560                 command_state = CONSOLE;
561                 return false;
562             }
563             break;
564         case KC_P:
565             mousekey_param_print();
566             break;
567         case KC_1:
568         case KC_2:
569         case KC_3:
570         case KC_4:
571         case KC_5:
572         case KC_6:
573             mousekey_param = numkey2num(code);
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\n");
595             break;
596         default:
597             print("?");
598             return false;
599     }
600     if (mousekey_param)
601         xprintf("M%d> ", mousekey_param);
602     else
603         print("M>" );
604     return true;
605 }
606 #endif
607
608
609 /***********************************************************
610  * Utilities
611  ***********************************************************/
612 static uint8_t numkey2num(uint8_t code)
613 {
614     switch (code) {
615         case KC_1: return 1;
616         case KC_2: return 2;
617         case KC_3: return 3;
618         case KC_4: return 4;
619         case KC_5: return 5;
620         case KC_6: return 6;
621         case KC_7: return 7;
622         case KC_8: return 8;
623         case KC_9: return 9;
624         case KC_0: return 0;
625     }
626     return 0;
627 }
628
629 static void switch_default_layer(uint8_t layer)
630 {
631     xprintf("L%d\n", layer);
632     default_layer_set(1UL<<layer);
633     clear_keyboard();
634 }