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