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
49 #ifdef RGBLIGHT_ENABLE
50 # include "rgblight.h"
53 #ifdef MATRIX_HAS_GHOST
54 static bool is_row_ghosting(uint8_t row){
55 matrix_row_t state = matrix_get_row(row);
56 /* no ghosting happens when only one key in the row is pressed */
57 if (!(state - 1 & state)) return false;
58 /* ghosting occurs when two keys in the same column are pressed */
59 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
60 if (r != row && matrix_get_row(r) & state) return true;
67 __attribute__ ((weak))
68 void matrix_setup(void) {
71 void keyboard_setup(void) {
75 void keyboard_init(void) {
78 #ifdef PS2_MOUSE_ENABLE
81 #ifdef SERIAL_MOUSE_ENABLE
84 #ifdef ADB_MOUSE_ENABLE
87 #ifdef BOOTMAGIC_ENABLE
92 #ifdef BACKLIGHT_ENABLE
95 #ifdef RGBLIGHT_ENABLE
98 #if defined(NKRO_ENABLE) && defined(FORCE_NKRO)
103 /* does routine keyboard jobs */
104 void keyboard_task(void) {
105 static uint8_t led_status;
107 for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
108 static matrix_row_t previous_matrix[MATRIX_ROWS];
109 matrix_row_t state = matrix_get_row(r);
110 matrix_row_t changes = state ^ previous_matrix[r];
112 #ifdef MATRIX_HAS_GHOST
113 static matrix_row_t deghosting_matrix[MATRIX_ROWS];
114 if (is_row_ghosting(r)) {
115 /* debugs the deghosting mechanism */
116 /* doesn't update previous_matrix until the ghosting has stopped
117 * in order to prevent the last key from being lost
119 if (debug_matrix && deghosting_matrix[r] != state) {
122 deghosting_matrix[r] = state;
125 deghosting_matrix[r] = state;
127 if (debug_matrix) matrix_print();
128 for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
129 matrix_row_t mask = (matrix_row_t)1 << c;
130 if (changes & mask) {
132 event.key = (keypos_t){ .row = r, .col = c };
133 event.pressed = state & mask;
134 /* the time should not be 0 */
135 event.time = timer_read() | 1;
137 /* records the processed key event */
138 previous_matrix[r] ^= mask;
139 /* processes one key event per call */
140 goto event_processed;
145 /* sends tick events when the keyboard is idle */
148 #ifdef MOUSEKEY_ENABLE
149 /* repeats and accelerates the mouse keys */
152 #ifdef PS2_MOUSE_ENABLE
155 #ifdef SERIAL_MOUSE_ENABLE
158 #ifdef ADB_MOUSE_ENABLE
161 /* updates the LEDs */
162 if (led_status != host_keyboard_leds()) {
163 led_status = host_keyboard_leds();
164 keyboard_set_leds(led_status);
168 void keyboard_set_leds(uint8_t leds) {
169 if (debug_keyboard) dprintf("Keyboard LEDs state: %x\n", leds);