refactor keyboard.h, host.h
parent
acc974c64b
commit
2b8cd88ab1
@ -1,93 +1,132 @@
|
||||
#include "usb_keycodes.h"
|
||||
#include "keyboard.h"
|
||||
#include "host.h"
|
||||
#include "layer.h"
|
||||
#include "matrix_skel.h"
|
||||
#include "led.h"
|
||||
#include "keyboard.h"
|
||||
#include "usb_keycodes.h"
|
||||
#include "timer.h"
|
||||
#include "print.h"
|
||||
#include "debug.h"
|
||||
#include "command.h"
|
||||
#ifdef MOUSEKEY_ENABLE
|
||||
#include "mousekey.h"
|
||||
#endif
|
||||
|
||||
static report_keyboard_t report0;
|
||||
static report_keyboard_t report1;
|
||||
report_keyboard_t *keyboard_report = &report0;
|
||||
report_keyboard_t *keyboard_report_prev = &report1;
|
||||
|
||||
static uint8_t last_led = 0;
|
||||
|
||||
void keyboard_set_led(uint8_t usb_led)
|
||||
{
|
||||
led_set(usb_led);
|
||||
}
|
||||
|
||||
void keyboard_send(void)
|
||||
void keyboard_init(void)
|
||||
{
|
||||
host_keyboard_send(keyboard_report);
|
||||
timer_init();
|
||||
matrix_init();
|
||||
#ifdef PS2_MOUSE_ENABLE
|
||||
ps2_mouse_init();
|
||||
#endif
|
||||
}
|
||||
|
||||
void keyboard_add_key(uint8_t code)
|
||||
void keyboard_proc(void)
|
||||
{
|
||||
int8_t i = 0;
|
||||
int8_t empty = -1;
|
||||
for (; i < REPORT_KEYS; i++) {
|
||||
if (keyboard_report_prev->keys[i] == code) {
|
||||
keyboard_report->keys[i] = code;
|
||||
break;
|
||||
}
|
||||
if (empty == -1 && keyboard_report_prev->keys[i] == KB_NO && keyboard_report->keys[i] == KB_NO) {
|
||||
empty = i;
|
||||
}
|
||||
}
|
||||
if (i == REPORT_KEYS && empty != -1) {
|
||||
keyboard_report->keys[empty] = code;
|
||||
}
|
||||
}
|
||||
uint8_t fn_bits = 0;
|
||||
|
||||
void keyboard_add_mod_bit(uint8_t mod)
|
||||
{
|
||||
keyboard_report->mods |= mod;
|
||||
matrix_scan();
|
||||
|
||||
if (matrix_is_modified()) {
|
||||
if (debug_matrix) matrix_print();
|
||||
#ifdef DEBUG_LED
|
||||
// LED flash for debug
|
||||
DEBUG_LED_CONFIG;
|
||||
DEBUG_LED_ON;
|
||||
#endif
|
||||
}
|
||||
|
||||
void keyboard_set_mods(uint8_t mods)
|
||||
{
|
||||
keyboard_report->mods = mods;
|
||||
if (matrix_has_ghost()) {
|
||||
// should send error?
|
||||
debug("matrix has ghost!!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
void keyboard_add_code(uint8_t code)
|
||||
{
|
||||
if (IS_MOD(code)) {
|
||||
keyboard_add_mod_bit(MOD_BIT(code));
|
||||
host_swap_keyboard_report();
|
||||
host_clear_keyboard_report();
|
||||
for (int row = 0; row < matrix_rows(); row++) {
|
||||
for (int col = 0; col < matrix_cols(); col++) {
|
||||
if (!matrix_is_on(row, col)) continue;
|
||||
|
||||
uint8_t code = layer_get_keycode(row, col);
|
||||
if (code == KB_NO) {
|
||||
// do nothing
|
||||
} else if (IS_MOD(code)) {
|
||||
host_add_mod_bit(MOD_BIT(code));
|
||||
} else if (IS_FN(code)) {
|
||||
fn_bits |= FN_BIT(code);
|
||||
}
|
||||
#ifdef USB_EXTRA_ENABLE
|
||||
/* TODO: use new API
|
||||
// audio control & system control
|
||||
else if (code == KB_MUTE) {
|
||||
usb_extra_audio_send(AUDIO_MUTE);
|
||||
usb_extra_audio_send(0);
|
||||
_delay_ms(500);
|
||||
} else if (code == KB_VOLU) {
|
||||
usb_extra_audio_send(AUDIO_VOL_UP);
|
||||
usb_extra_audio_send(0);
|
||||
_delay_ms(200);
|
||||
} else if (code == KB_VOLD) {
|
||||
usb_extra_audio_send(AUDIO_VOL_DOWN);
|
||||
usb_extra_audio_send(0);
|
||||
_delay_ms(200);
|
||||
} else if (code == KB_PWR) {
|
||||
if (suspend && remote_wakeup) {
|
||||
usb_remote_wakeup();
|
||||
} else {
|
||||
keyboard_add_key(code);
|
||||
usb_extra_system_send(SYSTEM_POWER_DOWN);
|
||||
}
|
||||
_delay_ms(1000);
|
||||
}
|
||||
|
||||
void keyboard_swap_report(void)
|
||||
{
|
||||
report_keyboard_t *tmp = keyboard_report_prev;
|
||||
keyboard_report_prev = keyboard_report;
|
||||
keyboard_report = tmp;
|
||||
*/
|
||||
#endif
|
||||
else if (IS_KEY(code)) {
|
||||
host_add_key(code);
|
||||
}
|
||||
|
||||
void keyboard_clear_report(void)
|
||||
{
|
||||
keyboard_report->mods = 0;
|
||||
for (int8_t i = 0; i < REPORT_KEYS; i++) {
|
||||
keyboard_report->keys[i] = 0;
|
||||
#ifdef MOUSEKEY_ENABLE
|
||||
else if (IS_MOUSEKEY(code)) {
|
||||
mousekey_decode(code);
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
debug("ignore keycode: "); debug_hex(code); debug("\n");
|
||||
}
|
||||
|
||||
uint8_t keyboard_has_anykey(void)
|
||||
{
|
||||
uint8_t cnt = 0;
|
||||
for (int i = 0; i < REPORT_KEYS; i++) {
|
||||
if (keyboard_report->keys[i])
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
uint8_t *keyboard_get_keys(void)
|
||||
{
|
||||
return keyboard_report->keys;
|
||||
layer_switching(fn_bits);
|
||||
|
||||
if (command_proc()) {
|
||||
// not send report
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t keyboard_get_mods(void)
|
||||
{
|
||||
return keyboard_report->mods;
|
||||
if (matrix_is_modified()) {
|
||||
host_send_keyboard_report();
|
||||
#ifdef DEBUG_LED
|
||||
// LED flash for debug
|
||||
DEBUG_LED_CONFIG;
|
||||
DEBUG_LED_OFF;
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef MOUSEKEY_ENABLE
|
||||
mousekey_send();
|
||||
#endif
|
||||
|
||||
#ifdef PS2_MOUSE_ENABLE
|
||||
// TODO: should comform new API
|
||||
if (ps2_mouse_read() == 0)
|
||||
ps2_mouse_usb_send();
|
||||
#endif
|
||||
|
||||
if (last_led != host_keyboard_led()) {
|
||||
led_set(host_keyboard_led());
|
||||
last_led = host_keyboard_led();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,75 @@
|
||||
#include <stdint.h>
|
||||
#include <util/delay.h>
|
||||
#include "usb_keycodes.h"
|
||||
#include "usb_mouse.h"
|
||||
#include "mousekey.h"
|
||||
|
||||
|
||||
static int8_t mousekey_x = 0;
|
||||
static int8_t mousekey_y = 0;
|
||||
static int8_t mousekey_v = 0;
|
||||
static int8_t mousekey_h = 0;
|
||||
static uint8_t mousekey_btn = 0;
|
||||
static uint8_t mousekey_btn_prev = 0;
|
||||
static uint8_t mousekey_repeat = 0;
|
||||
|
||||
|
||||
/*
|
||||
* TODO: fix acceleration algorithm
|
||||
* see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
|
||||
*/
|
||||
#ifndef MOUSEKEY_DELAY_TIME
|
||||
# define MOUSEKEY_DELAY_TIME 255
|
||||
#endif
|
||||
|
||||
|
||||
static inline uint8_t move_unit(void)
|
||||
{
|
||||
return 10 + (mousekey_repeat < 50 ? mousekey_repeat/5 : 10);
|
||||
}
|
||||
|
||||
void mousekey_decode(uint8_t code)
|
||||
{
|
||||
if (code == KB_MS_UP) mousekey_y -= move_unit();
|
||||
else if (code == KB_MS_DOWN) mousekey_y += move_unit();
|
||||
else if (code == KB_MS_LEFT) mousekey_x -= move_unit();
|
||||
else if (code == KB_MS_RIGHT) mousekey_x += move_unit();
|
||||
else if (code == KB_MS_BTN1) mousekey_btn |= MOUSE_BTN1;
|
||||
else if (code == KB_MS_BTN2) mousekey_btn |= MOUSE_BTN2;
|
||||
else if (code == KB_MS_BTN3) mousekey_btn |= MOUSE_BTN3;
|
||||
else if (code == KB_MS_BTN4) mousekey_btn |= MOUSE_BTN4;
|
||||
else if (code == KB_MS_BTN5) mousekey_btn |= MOUSE_BTN5;
|
||||
else if (code == KB_MS_WH_UP) mousekey_v += 1;
|
||||
else if (code == KB_MS_WH_DOWN) mousekey_v -= 1;
|
||||
else if (code == KB_MS_WH_LEFT) mousekey_h -= 1;
|
||||
else if (code == KB_MS_WH_RIGHT) mousekey_h += 1;
|
||||
}
|
||||
|
||||
bool mousekey_changed(void)
|
||||
{
|
||||
return (mousekey_x || mousekey_y || mousekey_v || mousekey_h || mousekey_btn != mousekey_btn_prev);
|
||||
}
|
||||
|
||||
void mousekey_usb_send(void)
|
||||
{
|
||||
if (mousekey_changed()) {
|
||||
mousekey_btn_prev = mousekey_btn;
|
||||
if (mousekey_x && mousekey_y)
|
||||
usb_mouse_send(mousekey_x*0.7, mousekey_y*0.7, mousekey_v, mousekey_h, mousekey_btn);
|
||||
else
|
||||
usb_mouse_send(mousekey_x, mousekey_y, mousekey_v, mousekey_h, mousekey_btn);
|
||||
|
||||
usb_mouse_print(mousekey_x, mousekey_y, mousekey_v, mousekey_h, mousekey_btn);
|
||||
|
||||
if (mousekey_x || mousekey_y || mousekey_v || mousekey_h)
|
||||
_delay_ms(MOUSEKEY_DELAY_TIME >> (mousekey_repeat < 5 ? mousekey_repeat : 4));
|
||||
mousekey_repeat++;
|
||||
} else {
|
||||
mousekey_repeat = 0;
|
||||
}
|
||||
mousekey_x = 0;
|
||||
mousekey_y = 0;
|
||||
mousekey_v = 0;
|
||||
mousekey_h = 0;
|
||||
mousekey_btn = 0;
|
||||
}
|
@ -1,102 +0,0 @@
|
||||
#include <stdint.h>
|
||||
#include <util/delay.h>
|
||||
#include "usb_keycodes.h"
|
||||
#include "host.h"
|
||||
#include "timer.h"
|
||||
#include "print.h"
|
||||
#include "mousekey.h"
|
||||
|
||||
|
||||
static report_mouse_t report;
|
||||
static report_mouse_t report_prev;
|
||||
|
||||
static uint8_t mousekey_repeat = 0;
|
||||
|
||||
|
||||
/*
|
||||
* TODO: fix acceleration algorithm
|
||||
* see wikipedia http://en.wikipedia.org/wiki/Mouse_keys
|
||||
*/
|
||||
#ifndef MOUSEKEY_DELAY_TIME
|
||||
# define MOUSEKEY_DELAY_TIME 255
|
||||
#endif
|
||||
|
||||
|
||||
static inline uint8_t move_unit(void)
|
||||
{
|
||||
uint8_t unit = (10 + (mousekey_repeat));
|
||||
return unit > 127 ? 127 : unit;
|
||||
}
|
||||
|
||||
void mousekey_decode(uint8_t code)
|
||||
{
|
||||
if (code == KB_MS_UP) report.y -= move_unit();
|
||||
else if (code == KB_MS_DOWN) report.y += move_unit();
|
||||
else if (code == KB_MS_LEFT) report.x -= move_unit();
|
||||
else if (code == KB_MS_RIGHT) report.x += move_unit();
|
||||
else if (code == KB_MS_BTN1) report.buttons |= MOUSE_BTN1;
|
||||
else if (code == KB_MS_BTN2) report.buttons |= MOUSE_BTN2;
|
||||
else if (code == KB_MS_BTN3) report.buttons |= MOUSE_BTN3;
|
||||
/*
|
||||
else if (code == KB_MS_BTN4) report.buttons |= MOUSE_BTN4;
|
||||
else if (code == KB_MS_BTN5) report.buttons |= MOUSE_BTN5;
|
||||
else if (code == KB_MS_WH_UP) report.v += 1;
|
||||
else if (code == KB_MS_WH_DOWN) report.v -= 1;
|
||||
else if (code == KB_MS_WH_LEFT) report.h -= 1;
|
||||
else if (code == KB_MS_WH_RIGHT)report.h += 1;
|
||||
*/
|
||||
}
|
||||
|
||||
bool mousekey_changed(void)
|
||||
{
|
||||
return (report.buttons != report_prev.buttons ||
|
||||
report.x != report_prev.x ||
|
||||
report.y != report_prev.y ||
|
||||
report.x || report.y);
|
||||
//return (report.buttons != report_prev.buttons || report.x || report.y);
|
||||
}
|
||||
|
||||
void mousekey_send(void)
|
||||
{
|
||||
static uint16_t last_timer = 0;
|
||||
|
||||
if (!mousekey_changed()) {
|
||||
mousekey_repeat = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
// send immediately when buttun state is changed
|
||||
if (report.buttons == report_prev.buttons) {
|
||||
// TODO: delay parameter setting
|
||||
if ((timer_elapsed(last_timer) < (mousekey_repeat == 1 ? 20 : 5))) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (report.x && report.y) {
|
||||
report.x *= 0.7;
|
||||
report.y *= 0.7;
|
||||
}
|
||||
|
||||
/*
|
||||
print("mousekey_repeat: "); phex(mousekey_repeat); print("\n");
|
||||
print("timer: "); phex16(timer_read()); print("\n");
|
||||
print("last_timer: "); phex16(last_timer); print("\n");
|
||||
print("mousekey: "); phex(report.buttons); print(" "); phex(report.x); print(" "); phex(report.y); print("\n");
|
||||
*/
|
||||
|
||||
host_mouse_send(&report);
|
||||
report_prev.buttons = report.buttons;
|
||||
report_prev.x = report.x;
|
||||
report_prev.y = report.y;
|
||||
if (mousekey_repeat != 0xFF) mousekey_repeat++;
|
||||
last_timer = timer_read();
|
||||
mousekey_clear_report();
|
||||
}
|
||||
|
||||
void mousekey_clear_report(void)
|
||||
{
|
||||
report.buttons = 0;
|
||||
report.x = 0;
|
||||
report.y = 0;
|
||||
}
|
Loading…
Reference in New Issue