#include "util.h" | #include "util.h" | ||||
#include "timer.h" | #include "timer.h" | ||||
#include "keyboard.h" | #include "keyboard.h" | ||||
#include "matrix.h" | |||||
#include "bootloader.h" | #include "bootloader.h" | ||||
#include "command.h" | #include "command.h" | ||||
#ifdef MOUSEKEY_ENABLE | |||||
#include "mousekey.h" | |||||
#endif | |||||
#ifdef HOST_PJRC | #ifdef HOST_PJRC | ||||
# include "usb_keyboard.h" | # include "usb_keyboard.h" | ||||
#endif | #endif | ||||
static uint8_t command_common(void); | |||||
static bool command_common(uint8_t code); | |||||
static void help(void); | static void help(void); | ||||
static void switch_layer(uint8_t layer); | static void switch_layer(uint8_t layer); | ||||
static void clear_keyboard(void); | |||||
static bool last_print_enable; | static bool last_print_enable; | ||||
uint8_t command_proc(void) | |||||
{ | |||||
uint8_t processed = 0; | |||||
last_print_enable = print_enable; | |||||
bool command_proc(uint8_t code) | |||||
{ | |||||
if (!IS_COMMAND()) | if (!IS_COMMAND()) | ||||
return 0; | |||||
return false; | |||||
last_print_enable = print_enable; | |||||
print_enable = true; | print_enable = true; | ||||
if (command_extra() || command_common()) { | |||||
processed = 1; | |||||
if (command_extra(code) || command_common(code)) { | |||||
_delay_ms(500); | _delay_ms(500); | ||||
return true; | |||||
} | } | ||||
print_enable = last_print_enable; | print_enable = last_print_enable; | ||||
return processed; | |||||
return false; | |||||
} | } | ||||
/* This allows to define extra commands. return 0 when not processed. */ | /* This allows to define extra commands. return 0 when not processed. */ | ||||
uint8_t command_extra(void) __attribute__ ((weak)); | |||||
uint8_t command_extra(void) | |||||
bool command_extra(uint8_t code) __attribute__ ((weak)); | |||||
bool command_extra(uint8_t code) | |||||
{ | { | ||||
return 0; | |||||
return false; | |||||
} | } | ||||
static uint8_t command_common(void) | |||||
static bool command_common(uint8_t code) | |||||
{ | { | ||||
switch (host_get_first_key()) { | |||||
switch (code) { | |||||
case KC_H: | case KC_H: | ||||
help(); | help(); | ||||
break; | break; | ||||
case KC_B: | |||||
case KC_DEL: | |||||
clear_keyboard(); | |||||
print("jump to bootloader... "); | print("jump to bootloader... "); | ||||
_delay_ms(1000); | _delay_ms(1000); | ||||
bootloader_jump(); // not return | bootloader_jump(); // not return | ||||
#endif | #endif | ||||
break; | break; | ||||
#endif | #endif | ||||
case KC_BSPC: | |||||
matrix_init(); | |||||
print("clear matrix\n"); | |||||
break; | |||||
case KC_0: | case KC_0: | ||||
case KC_F10: | |||||
switch_layer(0); | switch_layer(0); | ||||
break; | break; | ||||
case KC_1: | case KC_1: | ||||
case KC_F1: | |||||
switch_layer(1); | switch_layer(1); | ||||
break; | break; | ||||
case KC_2: | case KC_2: | ||||
case KC_F2: | |||||
switch_layer(2); | switch_layer(2); | ||||
break; | break; | ||||
case KC_3: | case KC_3: | ||||
case KC_F3: | |||||
switch_layer(3); | switch_layer(3); | ||||
break; | break; | ||||
case KC_4: | case KC_4: | ||||
case KC_F4: | |||||
switch_layer(4); | switch_layer(4); | ||||
break; | break; | ||||
default: | default: | ||||
return 0; | |||||
return false; | |||||
} | } | ||||
return 1; | |||||
return true; | |||||
} | } | ||||
static void help(void) | static void help(void) | ||||
{ | { | ||||
print("b: jump to bootloader\n"); | |||||
print("d: toggle debug enable\n"); | print("d: toggle debug enable\n"); | ||||
print("x: toggle matrix debug\n"); | print("x: toggle matrix debug\n"); | ||||
print("k: toggle keyboard debug\n"); | print("k: toggle keyboard debug\n"); | ||||
print("v: print version\n"); | print("v: print version\n"); | ||||
print("t: print timer count\n"); | print("t: print timer count\n"); | ||||
print("s: print status\n"); | print("s: print status\n"); | ||||
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 | #ifdef NKRO_ENABLE | ||||
print("n: toggle NKRO\n"); | print("n: toggle NKRO\n"); | ||||
#endif | #endif | ||||
print("Backspace: clear matrix\n"); | |||||
print("ESC: power down/wake up\n"); | |||||
print("0: switch to Layer0 \n"); | |||||
print("1: switch to Layer1 \n"); | |||||
print("2: switch to Layer2 \n"); | |||||
print("3: switch to Layer3 \n"); | |||||
print("4: switch to Layer4 \n"); | |||||
print("DEL: jump to bootloader\n"); | |||||
} | } | ||||
static void switch_layer(uint8_t layer) | static void switch_layer(uint8_t layer) | ||||
default_layer = layer; | default_layer = layer; | ||||
print("switch to Layer: "); phex(layer); print("\n"); | print("switch to Layer: "); phex(layer); print("\n"); | ||||
} | } | ||||
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 | |||||
} |
#ifndef COMMAND_H | #ifndef COMMAND_H | ||||
#define COMMAND | #define COMMAND | ||||
uint8_t command_proc(void); | |||||
bool command_proc(uint8_t code); | |||||
/* This allows to extend commands. Return 0 when command is not processed. */ | /* This allows to extend commands. Return 0 when command is not processed. */ | ||||
uint8_t command_extra(void); | |||||
bool command_extra(uint8_t code); | |||||
#endif | #endif |
#define debug(s) if(debug_enable) print_P(PSTR(s)) | #define debug(s) if(debug_enable) print_P(PSTR(s)) | ||||
#define debug_P(s) if(debug_enable) print_P(s) | |||||
#define debug_S(s) if(debug_enable) print_S(s) | |||||
#define debug_hex(c) if(debug_enable) phex(c) | #define debug_hex(c) if(debug_enable) phex(c) | ||||
#define debug_hex16(i) if(debug_enable) phex16(i) | #define debug_hex16(i) if(debug_enable) phex16(i) | ||||
#define debug_bin(c) if(debug_enable) pbin(c) | #define debug_bin(c) if(debug_enable) pbin(c) |
#ifdef MOUSEKEY_ENABLE | #ifdef MOUSEKEY_ENABLE | ||||
#include "mousekey.h" | #include "mousekey.h" | ||||
#endif | #endif | ||||
#ifdef EXTRAKEY_ENABLE | |||||
#include <util/delay.h> | |||||
#endif | |||||
#define LAYER_DELAY 250 | #define LAYER_DELAY 250 | ||||
static void register_code(uint8_t code) | static void register_code(uint8_t code) | ||||
{ | { | ||||
if IS_KEY(code) { | if IS_KEY(code) { | ||||
host_add_key(code); | |||||
host_send_keyboard_report(); | |||||
if (command_proc(code)) { | |||||
//clear_keyboard(); | |||||
} else { | |||||
host_add_key(code); | |||||
host_send_keyboard_report(); | |||||
} | |||||
} | } | ||||
else if IS_MOD(code) { | else if IS_MOD(code) { | ||||
host_add_mod_bit(MOD_BIT(code)); | host_add_mod_bit(MOD_BIT(code)); | ||||
* Ld: Switch back to default layer(*unregister* all keys but modifiers) | * Ld: Switch back to default layer(*unregister* all keys but modifiers) | ||||
*/ | */ | ||||
#define NEXT(state) do { \ | #define NEXT(state) do { \ | ||||
debug("NEXT: "); print_P(state_str(kbdstate)); \ | |||||
debug("NEXT: "); debug_P(state_str(kbdstate)); \ | |||||
kbdstate = state; \ | kbdstate = state; \ | ||||
debug(" -> "); print_P(state_str(kbdstate)); debug("\n"); \ | |||||
debug(" -> "); debug_P(state_str(kbdstate)); debug("\n"); \ | |||||
} while (0) | } while (0) | ||||
static inline void process_key(keyevent_t event) | static inline void process_key(keyevent_t event) | ||||
uint8_t tmp_mods; | uint8_t tmp_mods; | ||||
debug("state: "); print_P(state_str(kbdstate)); | |||||
debug("state: "); debug_P(state_str(kbdstate)); | |||||
debug(" kind: "); debug_hex(kind); | debug(" kind: "); debug_hex(kind); | ||||
debug(" code: "); debug_hex(code); | debug(" code: "); debug_hex(code); | ||||
if (event.pressed) { debug("d"); } else { debug("u"); } | if (event.pressed) { debug("d"); } else { debug("u"); } | ||||
matrix_row_t matrix_change = 0; | matrix_row_t matrix_change = 0; | ||||
matrix_scan(); | matrix_scan(); | ||||
if (command_proc()) { | |||||
debug("COMMAND\n"); | |||||
// TODO: COMMAND state? | |||||
clear_keyboard(); | |||||
return; | |||||
} | |||||
for (int r = 0; r < MATRIX_ROWS; r++) { | for (int r = 0; r < MATRIX_ROWS; r++) { | ||||
matrix_row = matrix_get_row(r); | matrix_row = matrix_get_row(r); | ||||
matrix_change = matrix_row ^ matrix_prev[r]; | matrix_change = matrix_row ^ matrix_prev[r]; | ||||
if (matrix_change) { | if (matrix_change) { | ||||
if (debug_matrix) matrix_print(); | |||||
matrix_debug(); | |||||
for (int c = 0; c < MATRIX_COLS; c++) { | for (int c = 0; c < MATRIX_COLS; c++) { | ||||
if (matrix_change & (1<<c)) { | if (matrix_change & (1<<c)) { | ||||
current_layer = default_layer; | current_layer = default_layer; | ||||
} | } | ||||
} | } | ||||
return; | return; | ||||
} | } | ||||
/* count keys pressed */ | /* count keys pressed */ | ||||
uint8_t matrix_key_count(void); | uint8_t matrix_key_count(void); | ||||
/* print matrix for debug */ | /* print matrix for debug */ | ||||
void matrix_print(void); | |||||
void matrix_debug(void); | |||||
#endif | #endif |
if (mousekey_repeat > mk_time_to_max) { | if (mousekey_repeat > mk_time_to_max) { | ||||
unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed; | unit = MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed; | ||||
} else { | } else { | ||||
unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_time_to_max; | |||||
unit = (MOUSEKEY_WHEEL_DELTA * mk_wheel_max_speed * mousekey_repeat) / mk_wheel_time_to_max; | |||||
} | } | ||||
if (unit == 0) return 1; | if (unit == 0) return 1; | ||||
return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : unit); | return (unit > MOUSEKEY_WHEEL_MAX ? MOUSEKEY_WHEEL_MAX : unit); |