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 #include "action_layer.h"
33 #ifdef BOOTMAGIC_ENABLE
34 # include "bootmagic.h"
38 #ifdef MOUSEKEY_ENABLE
39 # include "mousekey.h"
41 #ifdef PS2_MOUSE_ENABLE
42 # include "ps2_mouse.h"
44 #ifdef SERIAL_MOUSE_ENABLE
45 # include "serial_mouse.h"
47 #ifdef ADB_MOUSE_ENABLE
50 #ifdef RGBLIGHT_ENABLE
51 # include "rgblight.h"
53 #ifdef SERIAL_LINK_ENABLE
54 # include "serial_link/system/serial_link.h"
56 #ifdef VISUALIZER_ENABLE
57 # include "visualizer/visualizer.h"
60 #ifdef MATRIX_HAS_GHOST
61 static bool has_ghost_in_row(uint8_t row)
63 matrix_row_t matrix_row = matrix_get_row(row);
64 // No ghost exists when less than 2 keys are down on the row
65 if (((matrix_row - 1) & matrix_row) == 0)
68 // Ghost occurs when the row shares column line with other row
69 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
70 if (i != row && (matrix_get_row(i) & matrix_row))
77 __attribute__ ((weak))
78 void matrix_setup(void) {
81 void keyboard_setup(void) {
85 void keyboard_init(void) {
88 #ifdef PS2_MOUSE_ENABLE
91 #ifdef SERIAL_MOUSE_ENABLE
94 #ifdef ADB_MOUSE_ENABLE
97 #ifdef BOOTMAGIC_ENABLE
102 #ifdef BACKLIGHT_ENABLE
105 #ifdef RGBLIGHT_ENABLE
108 #if defined(NKRO_ENABLE) && defined(FORCE_NKRO)
109 keyboard_nkro = true;
114 * Do keyboard routine jobs: scan mantrix, light LEDs, ...
115 * This is repeatedly called as fast as possible.
117 void keyboard_task(void)
119 static matrix_row_t matrix_prev[MATRIX_ROWS];
120 #ifdef MATRIX_HAS_GHOST
121 static matrix_row_t matrix_ghost[MATRIX_ROWS];
123 static uint8_t led_status = 0;
124 matrix_row_t matrix_row = 0;
125 matrix_row_t matrix_change = 0;
128 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
129 matrix_row = matrix_get_row(r);
130 matrix_change = matrix_row ^ matrix_prev[r];
132 #ifdef MATRIX_HAS_GHOST
133 if (has_ghost_in_row(r)) {
134 /* Keep track of whether ghosted status has changed for
135 * debugging. But don't update matrix_prev until un-ghosted, or
136 * the last key would be lost.
138 if (debug_matrix && matrix_ghost[r] != matrix_row) {
141 matrix_ghost[r] = matrix_row;
144 matrix_ghost[r] = matrix_row;
146 if (debug_matrix) matrix_print();
147 for (uint8_t c = 0; c < MATRIX_COLS; c++) {
148 if (matrix_change & ((matrix_row_t)1<<c)) {
149 action_exec((keyevent_t){
150 .key = (keypos_t){ .row = r, .col = c },
151 .pressed = (matrix_row & ((matrix_row_t)1<<c)),
152 .time = (timer_read() | 1) /* time should not be 0 */
154 // record a processed key
155 matrix_prev[r] ^= ((matrix_row_t)1<<c);
156 // process a key per task call
157 goto MATRIX_LOOP_END;
162 // call with pseudo tick event when no real key event.
167 #ifdef MOUSEKEY_ENABLE
168 // mousekey repeat & acceleration
172 #ifdef PS2_MOUSE_ENABLE
176 #ifdef SERIAL_MOUSE_ENABLE
180 #ifdef ADB_MOUSE_ENABLE
184 #ifdef SERIAL_LINK_ENABLE
185 serial_link_update();
188 #ifdef VISUALIZER_ENABLE
189 visualizer_update(default_layer_state, layer_state, host_keyboard_leds());
193 if (led_status != host_keyboard_leds()) {
194 led_status = host_keyboard_leds();
195 keyboard_set_leds(led_status);
199 void keyboard_set_leds(uint8_t leds)
201 if (debug_keyboard) { debug("keyboard_set_led: "); debug_hex8(leds); debug("\n"); }