summaryrefslogtreecommitdiffstats
path: root/common/command.c
blob: 216ad06128821074f0cdc2858abaf1b82c284da0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*
Copyright 2011 Jun Wako <wakojun@gmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdbool.h>
#include <util/delay.h>
#include "keycode.h"
#include "host.h"
#include "keymap.h"
#include "print.h"
#include "debug.h"
#include "util.h"
#include "timer.h"
#include "keyboard.h"
#include "bootloader.h"
#include "action_layer.h"
#include "eeconfig.h"
#include "sleep_led.h"
#include "led.h"
#include "command.h"

#ifdef MOUSEKEY_ENABLE
#include "mousekey.h"
#endif

#ifdef PROTOCOL_PJRC
#   include "usb_keyboard.h"
#   ifdef EXTRAKEY_ENABLE
#       include "usb_extra.h"
#   endif
#endif

#ifdef PROTOCOL_VUSB
#   include "usbdrv.h"
#endif


static bool command_common(uint8_t code);
static void command_common_help(void);
static bool command_console(uint8_t code);
static void command_console_help(void);
#ifdef MOUSEKEY_ENABLE
static bool mousekey_console(uint8_t code);
static void mousekey_console_help(void);
#endif

static uint8_t numkey2num(uint8_t code);
static void switch_default_layer(uint8_t layer);


typedef enum { ONESHOT, CONSOLE, MOUSEKEY } cmdstate_t;
static cmdstate_t state = ONESHOT;


bool command_proc(uint8_t code)
{
    switch (state) {
        case ONESHOT:
            if (!IS_COMMAND())
                return false;
            return (command_extra(code) || command_common(code));
        case CONSOLE:
            command_console(code);
            break;
#ifdef MOUSEKEY_ENABLE
        case MOUSEKEY:
            mousekey_console(code);
            break;
#endif
        default:
            state = ONESHOT;
            return false;
    }
    return true;
}

/* This allows to define extra commands. return false when not processed. */
bool command_extra(uint8_t code) __attribute__ ((weak));
bool command_extra(uint8_t code)
{
    return false;
}


/***********************************************************
 * Command common
 ***********************************************************/
static void command_common_help(void)
{
    print("\n\n----- Command Help -----\n");
    print("c:	enter console mode\n");
    print("d:	toggle debug enable\n");
    print("x:	toggle matrix debug\n");
    print("k:	toggle keyboard debug\n");
    print("m:	toggle mouse debug\n");
    print("p:	toggle print enable\n");
    print("v:	print device version & info\n");
    print("t:	print timer count\n");
    print("s:	print status\n");
    print("e:	print eeprom config\n");
#ifdef NKRO_ENABLE
    print("n:	toggle NKRO\n");
#endif
    print("0/F10:	switch to Layer0 \n");
    print("1/F1:	switch to Layer1 \n");
    print("2/F2:	switch to Layer2 \n");
    print("3/F3:	switch to Layer3 \n");
    print("4/F4:	switch to Layer4 \n");
    print("PScr:	power down/remote wake-up\n");
    print("Caps:	Lock Keyboard(Child Proof)\n");
    print("Paus:	jump to bootloader\n");
}

#ifdef BOOTMAGIC_ENABLE
static void print_eeconfig(void)
{
    print("default_layer: "); print_dec(eeconfig_read_defalt_layer()); print("\n");

    debug_config_t dc;
    dc.raw = eeconfig_read_debug();
    print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
    print(".enable: "); print_dec(dc.enable); print("\n");
    print(".matrix: "); print_dec(dc.matrix); print("\n");
    print(".keyboard: "); print_dec(dc.keyboard); print("\n");
    print(".mouse: "); print_dec(dc.mouse); print("\n");

    keymap_config_t kc;
    kc.raw = eeconfig_read_keymap();
    print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
    print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
    print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
    print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
    print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
    print(".no_gui: "); print_dec(kc.no_gui); print("\n");
    print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
    print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
}
#endif

static bool command_common(uint8_t code)
{
    static host_driver_t *host_driver = 0;
    switch (code) {
        case KC_Z:
            // test breathing sleep LED
            print("Sleep LED test\n");
            sleep_led_toggle();
            led_set(host_keyboard_leds());
            break;
#ifdef BOOTMAGIC_ENABLE
        case KC_E:
            print("eeconfig:\n");
            print_eeconfig();
            break;
#endif
        case KC_CAPSLOCK:
            if (host_get_driver()) {
                host_driver = host_get_driver();
                host_set_driver(0);
                print("Locked.\n");
            } else {
                host_set_driver(host_driver);
                print("Unlocked.\n");
            }
            break;
        case KC_H:
        case KC_SLASH: /* ? */
            command_common_help();
            break;
        case KC_C:
            debug_matrix   = false;
            debug_keyboard = false;
            debug_mouse    = false;
            debug_enable   = false;
            command_console_help();
            print("\nEnter Console Mode\n");
            print("C> ");
            state = CONSOLE;
            break;
        case KC_PAUSE:
            clear_keyboard();
            print("\n\nJump to bootloader... ");
            _delay_ms(1000);
            bootloader_jump(); // not return
            print("not supported.\n");
            break;
        case KC_D:
            if (debug_enable) {
                print("\nDEBUG: disabled.\n");
                debug_matrix   = false;
                debug_keyboard = false;
                debug_mouse    = false;
                debug_enable   = false;
            } else {
                print("\nDEBUG: enabled.\n");
                debug_enable   = true;
            }
            break;
        case KC_X: // debug matrix toggle
            debug_matrix = !debug_matrix;
            if (debug_matrix) {
                print("\nDEBUG: matrix enabled.\n");
                debug_enable = true;
            } else {
                print("\nDEBUG: matrix disabled.\n");
            }
            break;
        case KC_K: // debug keyboard toggle
            debug_keyboard = !debug_keyboard;
            if (debug_keyboard) {
                print("\nDEBUG: keyboard enabled.\n");
                debug_enable = true;
            } else {
                print("\nDEBUG: keyboard disabled.\n");
            }
            break;
        case KC_M: // debug mouse toggle
            debug_mouse = !debug_mouse;
            if (debug_mouse) {
                print("\nDEBUG: mouse enabled.\n");
                debug_enable = true;
            } else {
                print("\nDEBUG: mouse disabled.\n");
            }
            break;
        case KC_V: // print version & information
            print("\n\n----- Version -----\n");
            print(STR(DESCRIPTION) "\n");
            print(STR(MANUFACTURER) "(" STR(VENDOR_ID) ")/");
            print(STR(PRODUCT) "(" STR(PRODUCT_ID) ") ");
            print("VERSION: " STR(DEVICE_VER) "\n");
            break;
        case KC_T: // print timer
            print_val_hex32(timer_count);
            break;
        case KC_S:
            print("\n\n----- Status -----\n");
            print_val_hex8(host_keyboard_leds());
#ifdef PROTOCOL_PJRC
            print_val_hex8(UDCON);
            print_val_hex8(UDIEN);
            print_val_hex8(UDINT);
            print_val_hex8(usb_keyboard_leds);
            print_val_hex8(usb_keyboard_protocol);
            print_val_hex8(usb_keyboard_idle_config);
            print_val_hex8(usb_keyboard_idle_count);
#endif

#ifdef PROTOCOL_PJRC
#   if USB_COUNT_SOF
            print_val_hex8(usbSofCount);
#   endif
#endif
            break;
#ifdef NKRO_ENABLE
        case KC_N:
            clear_keyboard(); //Prevents stuck keys.
            keyboard_nkro = !keyboard_nkro;
            if (keyboard_nkro)
                print("NKRO: enabled\n");
            else
                print("NKRO: disabled\n");
            break;
#endif
#ifdef EXTRAKEY_ENABLE
        case KC_PSCREEN:
            // TODO: Power key should take this feature? otherwise any key during suspend.
#ifdef PROTOCOL_PJRC
            if (suspend && remote_wakeup) {
                usb_remote_wakeup();
            } else {
                host_system_send(SYSTEM_POWER_DOWN);
                host_system_send(0);
                _delay_ms(500);
            }
#else
            host_system_send(SYSTEM_POWER_DOWN);
            _delay_ms(100);
            host_system_send(0);
            _delay_ms(500);
#endif
            break;
#endif
        case KC_ESC:
        case KC_GRV:
        case KC_0:
            switch_default_layer(0);
            break;
        case KC_1 ... KC_9:
            switch_default_layer((code - KC_1) + 1);
            break;
        case KC_F1 ... KC_F12:
            switch_default_layer((code - KC_F1) + 1);
            break;
        default:
            print("?");
            return false;
    }
    return true;
}


/***********************************************************
 * Command console
 ***********************************************************/
static void command_console_help(void)
{
    print("\n\n----- Console Help -----\n");
    print("ESC/q:	quit\n");
#ifdef MOUSEKEY_ENABLE
    print("m:	mousekey\n");
#endif
}

static bool command_console(uint8_t code)
{
    switch (code) {
        case KC_H:
        case KC_SLASH: /* ? */
            command_console_help();
            break;
        case KC_Q:
        case KC_ESC:
            print("\nQuit Console Mode\n");
            state = ONESHOT;
            return false;
#ifdef MOUSEKEY_ENABLE
        case KC_M:
            mousekey_console_help();
            print("\nEnter Mousekey Console\n");
            print("M0>");
            state = MOUSEKEY;
            return true;
#endif
        default:
            print("?");
            return false;
    }
    print("C> ");
    return true;
}


#ifdef MOUSEKEY_ENABLE
/***********************************************************
 * Mousekey console
 ***********************************************************/
static uint8_t mousekey_param = 0;

static void mousekey_param_print(void)
{
    print("\n\n----- Mousekey Parameters -----\n");
    print("1: mk_delay(*10ms): "); pdec(mk_delay); print("\n");
    print("2: mk_interval(ms): "); pdec(mk_interval); print("\n");
    print("3: mk_max_speed: "); pdec(mk_max_speed); print("\n");
    print("4: mk_time_to_max: "); pdec(mk_time_to_max); print("\n");
    print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
    print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
}

#define PRINT_SET_VAL(v)  print(#v " = "); print_dec(v); print("\n");
static void mousekey_param_inc(uint8_t param, uint8_t inc)
{
    switch (param) {
        case 1:
            if (mk_delay + inc < UINT8_MAX)
                mk_delay += inc;
            else
                mk_delay = UINT8_MAX;
            PRINT_SET_VAL(mk_delay);
            break;
        case 2:
            if (mk_interval + inc < UINT8_MAX)
                mk_interval += inc;
            else
                mk_interval = UINT8_MAX;
            PRINT_SET_VAL(mk_interval);
            break;
        case 3:
            if (mk_max_speed + inc < UINT8_MAX)
                mk_max_speed += inc;
            else
                mk_max_speed = UINT8_MAX;
            PRINT_SET_VAL(mk_max_speed);
            break;
        case 4:
            if (mk_time_to_max + inc < UINT8_MAX)
                mk_time_to_max += inc;
            else
                mk_time_to_max = UINT8_MAX;
            PRINT_SET_VAL(mk_time_to_max);
            break;
        case 5:
            if (mk_wheel_max_speed + inc < UINT8_MAX)
                mk_wheel_max_speed += inc;
            else
                mk_wheel_max_speed = UINT8_MAX;
            PRINT_SET_VAL(mk_wheel_max_speed);
            break;
        case 6:
            if (mk_wheel_time_to_max + inc < UINT8_MAX)
                mk_wheel_time_to_max += inc;
            else
                mk_wheel_time_to_max = UINT8_MAX;
            PRINT_SET_VAL(mk_wheel_time_to_max);
            break;
    }
}

static void mousekey_param_dec(uint8_t param, uint8_t dec)
{
    switch (param) {
        case 1:
            if (mk_delay > dec)
                mk_delay -= dec;
            else
                mk_delay = 0;
            PRINT_SET_VAL(mk_delay);
            break;
        case 2:
            if (mk_interval > dec)
                mk_interval -= dec;
            else
                mk_interval = 0;
            PRINT_SET_VAL(mk_interval);
            break;
        case 3:
            if (mk_max_speed > dec)
                mk_max_speed -= dec;
            else
                mk_max_speed = 0;
            PRINT_SET_VAL(mk_max_speed);
            break;
        case 4:
            if (mk_time_to_max > dec)
                mk_time_to_max -= dec;
            else
                mk_time_to_max = 0;
            PRINT_SET_VAL(mk_time_to_max);
            break;
        case 5:
            if (mk_wheel_max_speed > dec)
                mk_wheel_max_speed -= dec;
            else
                mk_wheel_max_speed = 0;
            PRINT_SET_VAL(mk_wheel_max_speed);
            break;
        case 6:
            if (mk_wheel_time_to_max > dec)
                mk_wheel_time_to_max -= dec;
            else
                mk_wheel_time_to_max = 0;
            PRINT_SET_VAL(mk_wheel_time_to_max);
            break;
    }
}

static void mousekey_console_help(void)
{
    print("\n\n----- Mousekey Parameters Help -----\n");
    print("ESC/q:	quit\n");
    print("1:	select mk_delay(*10ms)\n");
    print("2:	select mk_interval(ms)\n");
    print("3:	select mk_max_speed\n");
    print("4:	select mk_time_to_max\n");
    print("5:	select mk_wheel_max_speed\n");
    print("6:	select mk_wheel_time_to_max\n");
    print("p:	print prameters\n");
    print("d:	set default values\n");
    print("up:	increase prameters(+1)\n");
    print("down:	decrease prameters(-1)\n");
    print("pgup:	increase prameters(+10)\n");
    print("pgdown:	decrease prameters(-10)\n");
    print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
    print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA);
    print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA); print("\n");
    print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
}

static bool mousekey_console(uint8_t code)
{
    switch (code) {
        case KC_H:
        case KC_SLASH: /* ? */
            mousekey_console_help();
            break;
        case KC_Q:
        case KC_ESC:
            mousekey_param = 0;
            print("\nQuit Mousekey Console\n");
            print("C> ");
            state = CONSOLE;
            return false;
        case KC_P:
            mousekey_param_print();
            break;
        case KC_1:
        case KC_2:
        case KC_3:
        case KC_4:
        case KC_5:
        case KC_6:
        case KC_7:
        case KC_8:
        case KC_9:
        case KC_0:
            mousekey_param = numkey2num(code);
            print("selected parameter: "); pdec(mousekey_param); print("\n");
            break;
        case KC_UP:
            mousekey_param_inc(mousekey_param, 1);
            break;
        case KC_DOWN:
            mousekey_param_dec(mousekey_param, 1);
            break;
        case KC_PGUP:
            mousekey_param_inc(mousekey_param, 10);
            break;
        case KC_PGDN:
            mousekey_param_dec(mousekey_param, 10);
            break;
        case KC_D:
            mk_delay = MOUSEKEY_DELAY/10;
            mk_interval = MOUSEKEY_INTERVAL;
            mk_max_speed = MOUSEKEY_MAX_SPEED;
            mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
            mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
            mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
            print("set default values.\n");
            break;
        default:
            print("?");
            return false;
    }
    print("M"); pdec(mousekey_param); print("> ");
    return true;
}
#endif


/***********************************************************
 * Utilities
 ***********************************************************/
static uint8_t numkey2num(uint8_t code)
{
    switch (code) {
        case KC_1: return 1;
        case KC_2: return 2;
        case KC_3: return 3;
        case KC_4: return 4;
        case KC_5: return 5;
        case KC_6: return 6;
        case KC_7: return 7;
        case KC_8: return 8;
        case KC_9: return 9;
        case KC_0: return 0;
    }
    return 0;
}

static void switch_default_layer(uint8_t layer)
{
    print("switch_default_layer: "); print_dec(biton32(default_layer_state));
    print(" to "); print_dec(layer); print("\n");
    default_layer_set(layer);
    clear_keyboard();
}