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