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/>.
32 #include "action_layer.h"
33 #ifdef BACKLIGHT_ENABLE
34 # include "backlight.h"
36 #ifdef BOOTMAGIC_ENABLE
37 # include "bootmagic.h"
41 #ifdef MOUSEKEY_ENABLE
42 # include "mousekey.h"
44 #ifdef PS2_MOUSE_ENABLE
45 # include "ps2_mouse.h"
47 #ifdef SERIAL_MOUSE_ENABLE
48 # include "serial_mouse.h"
50 #ifdef ADB_MOUSE_ENABLE
53 #ifdef RGBLIGHT_ENABLE
54 # include "rgblight.h"
57 # include "process_steno.h"
59 #ifdef FAUXCLICKY_ENABLE
60 # include "fauxclicky.h"
62 #ifdef SERIAL_LINK_ENABLE
63 # include "serial_link/system/serial_link.h"
65 #ifdef VISUALIZER_ENABLE
66 # include "visualizer/visualizer.h"
68 #ifdef POINTING_DEVICE_ENABLE
69 # include "pointing_device.h"
72 # include "process_midi.h"
80 #ifdef OLED_DRIVER_ENABLE
81 # include "oled_driver.h"
83 #ifdef VELOCIKEY_ENABLE
84 # include "velocikey.h"
87 // Only enable this if console is enabled to print to
88 #if defined(DEBUG_MATRIX_SCAN_RATE) && defined(CONSOLE_ENABLE)
89 static uint32_t matrix_timer = 0;
90 static uint32_t matrix_scan_count = 0;
92 void matrix_scan_perf_task(void) {
95 uint32_t timer_now = timer_read32();
96 if (TIMER_DIFF_32(timer_now, matrix_timer) > 1000) {
97 dprintf("matrix scan frequency: %d\n", matrix_scan_count);
99 matrix_timer = timer_now;
100 matrix_scan_count = 0;
104 # define matrix_scan_perf_task()
107 #ifdef MATRIX_HAS_GHOST
108 extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
109 static matrix_row_t get_real_keys(uint8_t row, matrix_row_t rowdata) {
110 matrix_row_t out = 0;
111 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
112 // read each key in the row data and check if the keymap defines it as a real key
113 if (pgm_read_byte(&keymaps[0][row][col]) && (rowdata & (1 << col))) {
114 // this creates new row data, if a key is defined in the keymap, it will be set here
121 static inline bool popcount_more_than_one(matrix_row_t rowdata) {
122 rowdata &= rowdata - 1; // if there are less than two bits (keys) set, rowdata will become zero
126 static inline bool has_ghost_in_row(uint8_t row, matrix_row_t rowdata) {
127 /* No ghost exists when less than 2 keys are down on the row.
128 If there are "active" blanks in the matrix, the key can't be pressed by the user,
129 there is no doubt as to which keys are really being pressed.
130 The ghosts will be ignored, they are KC_NO. */
131 rowdata = get_real_keys(row, rowdata);
132 if ((popcount_more_than_one(rowdata)) == 0) {
135 /* Ghost occurs when the row shares a column line with other row,
136 and two columns are read on each row. Blanks in the matrix don't matter,
137 so they are filtered out.
138 If there are two or more real keys pressed and they match columns with
139 at least two of another row's real keys, the row will be ignored. Keep in mind,
140 we are checking one row at a time, not all of them at once.
142 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
143 if (i != row && popcount_more_than_one(get_real_keys(i, matrix_get_row(i)) & rowdata)) {
152 void disable_jtag(void) {
153 // To use PF4-7 (PC2-5 on ATmega32A), disable JTAG by writing JTD bit twice within four cycles.
154 #if (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
157 #elif defined(__AVR_ATmega32A__)
163 /** \brief matrix_setup
167 __attribute__((weak)) void matrix_setup(void) {}
169 /** \brief keyboard_pre_init_user
173 __attribute__((weak)) void keyboard_pre_init_user(void) {}
175 /** \brief keyboard_pre_init_kb
179 __attribute__((weak)) void keyboard_pre_init_kb(void) { keyboard_pre_init_user(); }
181 /** \brief keyboard_post_init_user
186 __attribute__((weak)) void keyboard_post_init_user() {}
188 /** \brief keyboard_post_init_kb
193 __attribute__((weak)) void keyboard_post_init_kb(void) { keyboard_post_init_user(); }
195 /** \brief keyboard_setup
199 void keyboard_setup(void) {
200 #ifndef NO_JTAG_DISABLE
204 keyboard_pre_init_kb();
207 /** \brief is_keyboard_master
211 __attribute__((weak)) bool is_keyboard_master(void) { return true; }
213 /** \brief keyboard_init
217 void keyboard_init(void) {
223 #ifdef OLED_DRIVER_ENABLE
224 oled_init(OLED_ROTATION_0);
226 #ifdef PS2_MOUSE_ENABLE
229 #ifdef SERIAL_MOUSE_ENABLE
232 #ifdef ADB_MOUSE_ENABLE
235 #ifdef BOOTMAGIC_ENABLE
240 #ifdef BACKLIGHT_ENABLE
243 #ifdef RGBLIGHT_ENABLE
249 #ifdef FAUXCLICKY_ENABLE
252 #ifdef POINTING_DEVICE_ENABLE
253 pointing_device_init();
255 #if defined(NKRO_ENABLE) && defined(FORCE_NKRO)
256 keymap_config.nkro = 1;
257 eeconfig_update_keymap(keymap_config.raw);
259 keyboard_post_init_kb(); /* Always keep this last */
262 /** \brief Keyboard task: Do keyboard routine jobs
264 * Do routine keyboard jobs:
267 * * handle mouse movements
268 * * run visualizer code
269 * * handle midi commands
272 * This is repeatedly called as fast as possible.
274 void keyboard_task(void) {
275 static matrix_row_t matrix_prev[MATRIX_ROWS];
276 static uint8_t led_status = 0;
277 matrix_row_t matrix_row = 0;
278 matrix_row_t matrix_change = 0;
279 #ifdef QMK_KEYS_PER_SCAN
280 uint8_t keys_processed = 0;
283 #if defined(OLED_DRIVER_ENABLE) && !defined(OLED_DISABLE_TIMEOUT)
284 uint8_t ret = matrix_scan();
289 if (is_keyboard_master()) {
290 for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
291 matrix_row = matrix_get_row(r);
292 matrix_change = matrix_row ^ matrix_prev[r];
294 #ifdef MATRIX_HAS_GHOST
295 if (has_ghost_in_row(r, matrix_row)) {
299 if (debug_matrix) matrix_print();
300 for (uint8_t c = 0; c < MATRIX_COLS; c++) {
301 if (matrix_change & ((matrix_row_t)1 << c)) {
302 action_exec((keyevent_t){
303 .key = (keypos_t){.row = r, .col = c}, .pressed = (matrix_row & ((matrix_row_t)1 << c)), .time = (timer_read() | 1) /* time should not be 0 */
305 // record a processed key
306 matrix_prev[r] ^= ((matrix_row_t)1 << c);
307 #ifdef QMK_KEYS_PER_SCAN
308 // only jump out if we have processed "enough" keys.
309 if (++keys_processed >= QMK_KEYS_PER_SCAN)
311 // process a key per task call
312 goto MATRIX_LOOP_END;
318 // call with pseudo tick event when no real key event.
319 #ifdef QMK_KEYS_PER_SCAN
320 // we can get here with some keys processed now.
327 #ifdef DEBUG_MATRIX_SCAN_RATE
328 matrix_scan_perf_task();
335 #ifdef OLED_DRIVER_ENABLE
337 # ifndef OLED_DISABLE_TIMEOUT
338 // Wake up oled if user is using those fabulous keys!
343 #ifdef MOUSEKEY_ENABLE
344 // mousekey repeat & acceleration
348 #ifdef PS2_MOUSE_ENABLE
352 #ifdef SERIAL_MOUSE_ENABLE
356 #ifdef ADB_MOUSE_ENABLE
360 #ifdef SERIAL_LINK_ENABLE
361 serial_link_update();
364 #ifdef VISUALIZER_ENABLE
365 visualizer_update(default_layer_state, layer_state, visualizer_get_mods(), host_keyboard_leds());
368 #ifdef POINTING_DEVICE_ENABLE
369 pointing_device_task();
376 #ifdef VELOCIKEY_ENABLE
377 if (velocikey_enabled()) {
378 velocikey_decelerate();
383 if (led_status != host_keyboard_leds()) {
384 led_status = host_keyboard_leds();
385 keyboard_set_leds(led_status);
389 /** \brief keyboard set leds
393 void keyboard_set_leds(uint8_t leds) {
394 if (debug_keyboard) {
395 debug("keyboard_set_led: ");