qmk_firmware/keyboard.c

143 lines
3.2 KiB
C
Raw Normal View History

2011-02-10 06:51:30 +00:00
#include "keyboard.h"
2011-02-08 15:03:58 +00:00
#include "host.h"
2011-02-10 06:51:30 +00:00
#include "layer.h"
2011-02-21 06:43:17 +00:00
#include "matrix.h"
2011-02-08 15:03:58 +00:00
#include "led.h"
2011-02-10 06:51:30 +00:00
#include "usb_keycodes.h"
#include "timer.h"
2011-02-08 15:03:58 +00:00
#include "print.h"
2011-02-10 06:51:30 +00:00
#include "debug.h"
#include "command.h"
#ifdef MOUSEKEY_ENABLE
#include "mousekey.h"
#endif
2011-02-21 06:43:17 +00:00
#ifdef USB_EXTRA_ENABLE
#include <util/delay.h>
#endif
2011-02-08 15:03:58 +00:00
2011-02-12 15:15:51 +00:00
static uint8_t last_leds = 0;
2011-02-08 15:03:58 +00:00
2011-02-10 06:51:30 +00:00
void keyboard_init(void)
2011-02-08 15:03:58 +00:00
{
2011-02-10 06:51:30 +00:00
timer_init();
matrix_init();
#ifdef PS2_MOUSE_ENABLE
ps2_mouse_init();
#endif
2011-02-08 15:03:58 +00:00
}
2011-02-10 06:51:30 +00:00
void keyboard_proc(void)
2011-02-08 15:03:58 +00:00
{
2011-02-10 06:51:30 +00:00
uint8_t fn_bits = 0;
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
2011-02-08 15:03:58 +00:00
}
2011-02-10 06:51:30 +00:00
if (matrix_has_ghost()) {
// should send error?
debug("matrix has ghost!!\n");
return;
2011-02-08 15:03:58 +00:00
}
2011-02-10 06:51:30 +00:00
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;
2011-02-08 15:03:58 +00:00
2011-02-10 06:51:30 +00:00
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
// audio control & system control
else if (code == KB_MUTE) {
2011-05-21 01:28:57 +00:00
host_audio_send(AUDIO_MUTE);
2011-02-10 06:51:30 +00:00
_delay_ms(500);
2011-05-21 01:28:57 +00:00
host_audio_send(0);
2011-02-10 06:51:30 +00:00
} else if (code == KB_VOLU) {
2011-05-21 01:28:57 +00:00
host_audio_send(AUDIO_VOL_UP);
2011-02-10 06:51:30 +00:00
_delay_ms(200);
2011-05-21 01:28:57 +00:00
host_audio_send(0);
2011-02-10 06:51:30 +00:00
} else if (code == KB_VOLD) {
2011-05-21 01:28:57 +00:00
host_audio_send(AUDIO_VOL_DOWN);
2011-02-10 06:51:30 +00:00
_delay_ms(200);
2011-05-21 01:28:57 +00:00
host_audio_send(0);
2011-02-10 06:51:30 +00:00
} else if (code == KB_PWR) {
2011-05-21 01:28:57 +00:00
#ifdef HOST_PJRC
2011-02-10 06:51:30 +00:00
if (suspend && remote_wakeup) {
usb_remote_wakeup();
} else {
2011-05-21 01:28:57 +00:00
host_system_send(SYSTEM_POWER_DOWN);
2011-02-10 06:51:30 +00:00
}
2011-05-21 01:28:57 +00:00
#else
host_system_send(SYSTEM_POWER_DOWN);
#endif
2011-02-10 06:51:30 +00:00
_delay_ms(1000);
}
#endif
else if (IS_KEY(code)) {
host_add_key(code);
}
#ifdef MOUSEKEY_ENABLE
else if (IS_MOUSEKEY(code)) {
mousekey_decode(code);
}
#endif
else {
debug("ignore keycode: "); debug_hex(code); debug("\n");
}
}
2011-02-08 15:03:58 +00:00
}
2011-02-10 06:51:30 +00:00
layer_switching(fn_bits);
2011-02-08 15:03:58 +00:00
2011-02-10 06:51:30 +00:00
if (command_proc()) {
return;
2011-02-08 15:03:58 +00:00
}
// TODO: should send only when changed from last report
2011-02-10 06:51:30 +00:00
if (matrix_is_modified()) {
host_send_keyboard_report();
#ifdef DEBUG_LED
// LED flash for debug
DEBUG_LED_CONFIG;
DEBUG_LED_OFF;
#endif
2011-02-08 15:03:58 +00:00
}
2011-02-10 06:51:30 +00:00
#ifdef MOUSEKEY_ENABLE
mousekey_send();
#endif
2011-02-08 15:03:58 +00:00
2011-02-10 06:51:30 +00:00
#ifdef PS2_MOUSE_ENABLE
// TODO: should comform new API
if (ps2_mouse_read() == 0)
ps2_mouse_usb_send();
#endif
2011-02-12 15:15:51 +00:00
if (last_leds != host_keyboard_leds()) {
keyboard_set_leds(host_keyboard_leds());
last_leds = host_keyboard_leds();
2011-02-10 06:51:30 +00:00
}
2011-02-08 15:03:58 +00:00
}
2011-02-12 15:15:51 +00:00
void keyboard_set_leds(uint8_t leds)
{
led_set(leds);
}