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"
62 #ifdef MATRIX_HAS_GHOST
63 static bool has_ghost_in_row(uint8_t row)
65 matrix_row_t matrix_row = matrix_get_row(row);
66 // No ghost exists when less than 2 keys are down on the row
67 if (((matrix_row - 1) & matrix_row) == 0)
70 // Ghost occurs when the row shares column line with other row
71 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
72 if (i != row && (matrix_get_row(i) & matrix_row))
79 __attribute__ ((weak))
80 void matrix_setup(void) {
83 void keyboard_setup(void) {
87 void keyboard_init(void) {
90 #ifdef PS2_MOUSE_ENABLE
93 #ifdef SERIAL_MOUSE_ENABLE
96 #ifdef ADB_MOUSE_ENABLE
99 #ifdef BOOTMAGIC_ENABLE
104 #ifdef BACKLIGHT_ENABLE
107 #ifdef RGBLIGHT_ENABLE
110 #if defined(NKRO_ENABLE) && defined(FORCE_NKRO)
111 keymap_config.nkro = 1;
116 * Do keyboard routine jobs: scan mantrix, light LEDs, ...
117 * This is repeatedly called as fast as possible.
119 void keyboard_task(void)
121 static matrix_row_t matrix_prev[MATRIX_ROWS];
122 #ifdef MATRIX_HAS_GHOST
123 static matrix_row_t matrix_ghost[MATRIX_ROWS];
125 static uint8_t led_status = 0;
126 matrix_row_t matrix_row = 0;
127 matrix_row_t matrix_change = 0;
130 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
131 matrix_row = matrix_get_row(r);
132 matrix_change = matrix_row ^ matrix_prev[r];
134 #ifdef MATRIX_HAS_GHOST
135 if (has_ghost_in_row(r)) {
136 /* Keep track of whether ghosted status has changed for
137 * debugging. But don't update matrix_prev until un-ghosted, or
138 * the last key would be lost.
140 if (debug_matrix && matrix_ghost[r] != matrix_row) {
143 matrix_ghost[r] = matrix_row;
146 matrix_ghost[r] = matrix_row;
148 if (debug_matrix) matrix_print();
149 for (uint8_t c = 0; c < MATRIX_COLS; c++) {
150 if (matrix_change & ((matrix_row_t)1<<c)) {
151 action_exec((keyevent_t){
152 .key = (keypos_t){ .row = r, .col = c },
153 .pressed = (matrix_row & ((matrix_row_t)1<<c)),
154 .time = (timer_read() | 1) /* time should not be 0 */
156 // record a processed key
157 matrix_prev[r] ^= ((matrix_row_t)1<<c);
158 // process a key per task call
159 goto MATRIX_LOOP_END;
164 // call with pseudo tick event when no real key event.
169 #ifdef MOUSEKEY_ENABLE
170 // mousekey repeat & acceleration
174 #ifdef PS2_MOUSE_ENABLE
178 #ifdef SERIAL_MOUSE_ENABLE
182 #ifdef ADB_MOUSE_ENABLE
186 #ifdef SERIAL_LINK_ENABLE
187 serial_link_update();
190 #ifdef VISUALIZER_ENABLE
191 visualizer_update(default_layer_state, layer_state, visualizer_get_mods(), host_keyboard_leds());
195 if (led_status != host_keyboard_leds()) {
196 led_status = host_keyboard_leds();
197 keyboard_set_leds(led_status);
201 void keyboard_set_leds(uint8_t leds)
203 if (debug_keyboard) { debug("keyboard_set_led: "); debug_hex8(leds); debug("\n"); }