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