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