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