2 Copyright 2011, 2012, 2013 Jun Wako <wakojun@gmail.com>
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 #include "backlight.h"
32 #ifdef BOOTMAGIC_ENABLE
33 # include "bootmagic.h"
37 #ifdef MOUSEKEY_ENABLE
38 # include "mousekey.h"
40 #ifdef PS2_MOUSE_ENABLE
41 # include "ps2_mouse.h"
43 #ifdef SERIAL_MOUSE_ENABLE
44 # include "serial_mouse.h"
46 #ifdef ADB_MOUSE_ENABLE
50 #ifdef MATRIX_HAS_GHOST
51 static bool is_row_ghosting(uint8_t row){
52 matrix_row_t state = matrix_get_row(row);
53 /* no ghosting happens when only one key in the row is pressed */
54 if (!(state - 1 & state)) return false;
55 /* ghosting occurs when two keys in the same column are pressed */
56 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
57 if (r != row && matrix_get_row(r) & state) return true;
64 __attribute__ ((weak))
65 void matrix_setup(void) {
68 void keyboard_setup(void) {
72 void keyboard_init(void) {
75 #ifdef PS2_MOUSE_ENABLE
78 #ifdef SERIAL_MOUSE_ENABLE
81 #ifdef ADB_MOUSE_ENABLE
84 #ifdef BOOTMAGIC_ENABLE
89 #ifdef BACKLIGHT_ENABLE
92 #if defined(NKRO_ENABLE) && defined(FORCE_NKRO)
97 /* does routine keyboard jobs */
98 void keyboard_task(void) {
99 static uint8_t led_status;
101 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
102 static matrix_row_t previous_matrix[MATRIX_ROWS];
103 matrix_row_t state = matrix_get_row(r);
104 matrix_row_t changes = state ^ previous_matrix[r];
106 #ifdef MATRIX_HAS_GHOST
107 static matrix_row_t deghosting_matrix[MATRIX_ROWS];
108 if (is_row_ghosting(r)) {
109 /* debugs the deghosting mechanism */
110 /* doesn't update previous_matrix until the ghosting has stopped
111 * in order to prevent the last key from being lost
113 if (debug_matrix && deghosting_matrix[r] != state) {
116 deghosting_matrix[r] = state;
119 deghosting_matrix[r] = state;
121 if (debug_matrix) matrix_print();
122 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
123 matrix_row_t mask = (matrix_row_t)1 << c;
124 if (changes & mask) {
126 event.key = (keypos_t){ .row = r, .col = c };
127 event.pressed = state & mask;
128 /* the time should not be 0 */
129 event.time = timer_read() | 1;
131 /* records the processed key event */
132 previous_matrix[r] ^= mask;
133 /* processes one key event per call */
134 goto event_processed;
139 /* sends tick events when the keyboard is idle */
142 #ifdef MOUSEKEY_ENABLE
143 /* repeats and accelerates the mouse keys */
146 #ifdef PS2_MOUSE_ENABLE
149 #ifdef SERIAL_MOUSE_ENABLE
152 #ifdef ADB_MOUSE_ENABLE
155 /* updates the LEDs */
156 if (led_status != host_keyboard_leds()) {
157 led_status = host_keyboard_leds();
158 keyboard_set_leds(led_status);
162 void keyboard_set_leds(uint8_t leds) {
163 if (debug_keyboard) dprintf("Keyboard LEDs state: %x\n", leds);