qmk_firmware/common/command.c

255 lines
7.0 KiB
C
Raw Normal View History

2011-07-20 15:32:52 +00:00
/*
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/>.
*/
2011-02-12 15:15:51 +00:00
#include <stdint.h>
#include <stdbool.h>
#include <util/delay.h>
#include "keycode.h"
2011-02-12 15:15:51 +00:00
#include "host.h"
#include "print.h"
#include "debug.h"
#include "util.h"
#include "timer.h"
#include "keyboard.h"
#include "bootloader.h"
2011-02-12 15:15:51 +00:00
#include "command.h"
2012-10-16 02:20:49 +00:00
#ifdef MOUSEKEY_ENABLE
#include "mousekey.h"
#endif
2011-02-12 15:15:51 +00:00
#ifdef HOST_PJRC
# include "usb_keyboard.h"
# ifdef EXTRAKEY_ENABLE
2011-02-12 15:15:51 +00:00
# include "usb_extra.h"
# endif
#endif
#ifdef HOST_VUSB
# include "usbdrv.h"
#endif
2011-02-12 15:15:51 +00:00
2012-10-16 02:20:49 +00:00
static bool command_common(uint8_t code);
2011-02-12 15:15:51 +00:00
static void help(void);
static void switch_layer(uint8_t layer);
2012-10-16 02:20:49 +00:00
static void clear_keyboard(void);
2011-02-12 15:15:51 +00:00
static bool last_print_enable;
2011-02-12 15:15:51 +00:00
2012-10-16 02:20:49 +00:00
bool command_proc(uint8_t code)
{
2011-02-12 15:15:51 +00:00
if (!IS_COMMAND())
2012-10-16 02:20:49 +00:00
return false;
2011-02-12 15:15:51 +00:00
2012-10-16 02:20:49 +00:00
last_print_enable = print_enable;
2011-02-12 15:15:51 +00:00
print_enable = true;
2012-10-16 02:20:49 +00:00
if (command_extra(code) || command_common(code)) {
_delay_ms(500);
2012-10-16 02:20:49 +00:00
return true;
}
print_enable = last_print_enable;
2012-10-16 02:20:49 +00:00
return false;
}
/* This allows to define extra commands. return 0 when not processed. */
2012-10-16 02:20:49 +00:00
bool command_extra(uint8_t code) __attribute__ ((weak));
bool command_extra(uint8_t code)
{
2012-10-16 02:20:49 +00:00
return false;
}
2012-10-16 02:20:49 +00:00
static bool command_common(uint8_t code)
{
2012-10-16 02:20:49 +00:00
switch (code) {
case KC_H:
2011-02-12 15:15:51 +00:00
help();
break;
2012-10-16 02:20:49 +00:00
case KC_DEL:
clear_keyboard();
print("jump to bootloader... ");
2011-02-12 15:15:51 +00:00
_delay_ms(1000);
bootloader_jump(); // not return
print("not supported.\n");
2011-02-12 15:15:51 +00:00
break;
case KC_D:
2011-02-12 15:15:51 +00:00
debug_enable = !debug_enable;
if (debug_enable) {
last_print_enable = true;
print("debug enabled.\n");
debug_matrix = true;
debug_keyboard = true;
debug_mouse = true;
} else {
print("debug disabled.\n");
last_print_enable = false;
debug_matrix = false;
debug_keyboard = false;
debug_mouse = false;
}
break;
case KC_X: // debug matrix toggle
2011-02-12 15:15:51 +00:00
debug_matrix = !debug_matrix;
if (debug_matrix)
print("debug matrix enabled.\n");
else
print("debug matrix disabled.\n");
break;
case KC_K: // debug keyboard toggle
2011-02-12 15:15:51 +00:00
debug_keyboard = !debug_keyboard;
if (debug_keyboard)
print("debug keyboard enabled.\n");
else
print("debug keyboard disabled.\n");
break;
case KC_M: // debug mouse toggle
2011-02-12 15:15:51 +00:00
debug_mouse = !debug_mouse;
if (debug_mouse)
print("debug mouse enabled.\n");
else
print("debug mouse disabled.\n");
break;
case KC_V: // print version & information
2011-02-12 15:15:51 +00:00
print(STR(DESCRIPTION) "\n");
break;
case KC_T: // print timer
2011-02-12 15:15:51 +00:00
print("timer: "); phex16(timer_count); print("\n");
break;
case KC_P: // print toggle
2011-02-21 16:21:53 +00:00
if (last_print_enable) {
2011-02-12 15:15:51 +00:00
print("print disabled.\n");
last_print_enable = false;
} else {
last_print_enable = true;
print("print enabled.\n");
}
break;
case KC_S:
2012-06-29 07:48:36 +00:00
print("host_keyboard_leds:"); phex(host_keyboard_leds()); print("\n");
2011-02-12 15:15:51 +00:00
#ifdef HOST_PJRC
print("UDCON: "); phex(UDCON); print("\n");
print("UDIEN: "); phex(UDIEN); print("\n");
print("UDINT: "); phex(UDINT); print("\n");
print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
print("usb_keyboard_protocol: "); phex(usb_keyboard_protocol); print("\n");
print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
#endif
#ifdef HOST_VUSB
# if USB_COUNT_SOF
print("usbSofCount: "); phex(usbSofCount); print("\n");
# endif
2011-02-12 15:15:51 +00:00
#endif
break;
#ifdef NKRO_ENABLE
case KC_N:
2011-02-12 15:15:51 +00:00
keyboard_nkro = !keyboard_nkro;
if (keyboard_nkro)
print("NKRO: enabled\n");
2011-02-12 15:15:51 +00:00
else
print("NKRO: disabled\n");
2011-02-12 15:15:51 +00:00
break;
#endif
#ifdef EXTRAKEY_ENABLE
case KC_ESC:
2011-05-21 01:28:57 +00:00
#ifdef HOST_PJRC
2011-02-12 15:15:51 +00:00
if (suspend && remote_wakeup) {
usb_remote_wakeup();
} else {
2011-05-21 01:28:57 +00:00
host_system_send(SYSTEM_POWER_DOWN);
host_system_send(0);
_delay_ms(500);
2011-02-12 15:15:51 +00:00
}
2011-05-21 01:28:57 +00:00
#else
host_system_send(SYSTEM_POWER_DOWN);
host_system_send(0);
_delay_ms(500);
2011-05-21 01:28:57 +00:00
#endif
2011-02-12 15:15:51 +00:00
break;
#endif
case KC_0:
2012-10-16 02:20:49 +00:00
case KC_F10:
2011-02-12 15:15:51 +00:00
switch_layer(0);
break;
case KC_1:
2012-10-16 02:20:49 +00:00
case KC_F1:
2011-02-12 15:15:51 +00:00
switch_layer(1);
break;
case KC_2:
2012-10-16 02:20:49 +00:00
case KC_F2:
2011-02-12 15:15:51 +00:00
switch_layer(2);
break;
case KC_3:
2012-10-16 02:20:49 +00:00
case KC_F3:
2011-02-12 15:15:51 +00:00
switch_layer(3);
break;
case KC_4:
2012-10-16 02:20:49 +00:00
case KC_F4:
2011-02-12 15:15:51 +00:00
switch_layer(4);
break;
default:
2012-10-16 02:20:49 +00:00
return false;
2011-02-12 15:15:51 +00:00
}
2012-10-16 02:20:49 +00:00
return true;
2011-02-12 15:15:51 +00:00
}
static void help(void)
{
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 version\n");
print("t: print timer count\n");
print("s: print status\n");
2012-10-16 02:20:49 +00:00
print("ESC: power down/wake up\n");
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");
#ifdef NKRO_ENABLE
print("n: toggle NKRO\n");
2011-02-12 15:15:51 +00:00
#endif
2012-10-16 02:20:49 +00:00
print("DEL: jump to bootloader\n");
2011-02-12 15:15:51 +00:00
}
static void switch_layer(uint8_t layer)
{
print("current_layer: "); phex(current_layer); print("\n");
print("default_layer: "); phex(default_layer); print("\n");
current_layer = layer;
default_layer = layer;
print("switch to Layer: "); phex(layer); print("\n");
}
2012-10-16 02:20:49 +00:00
static void clear_keyboard(void)
{
host_clear_keys();
host_send_keyboard_report();
host_system_send(0);
host_consumer_send(0);
#ifdef MOUSEKEY_ENABLE
mousekey_clear();
mousekey_send();
#endif
}