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