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