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