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"
90 // Only enable this if console is enabled to print to
91 #if defined(DEBUG_MATRIX_SCAN_RATE) && defined(CONSOLE_ENABLE)
92 static uint32_t matrix_timer
= 0;
93 static uint32_t matrix_scan_count
= 0;
95 void matrix_scan_perf_task(void) {
98 uint32_t timer_now
= timer_read32();
99 if (TIMER_DIFF_32(timer_now
, matrix_timer
) > 1000) {
100 dprintf("matrix scan frequency: %d\n", matrix_scan_count
);
102 matrix_timer
= timer_now
;
103 matrix_scan_count
= 0;
107 # define matrix_scan_perf_task()
110 #ifdef MATRIX_HAS_GHOST
111 extern const uint16_t keymaps
[][MATRIX_ROWS
][MATRIX_COLS
];
112 static matrix_row_t
get_real_keys(uint8_t row
, matrix_row_t rowdata
) {
113 matrix_row_t out
= 0;
114 for (uint8_t col
= 0; col
< MATRIX_COLS
; col
++) {
115 // read each key in the row data and check if the keymap defines it as a real key
116 if (pgm_read_byte(&keymaps
[0][row
][col
]) && (rowdata
& (1 << col
))) {
117 // this creates new row data, if a key is defined in the keymap, it will be set here
124 static inline bool popcount_more_than_one(matrix_row_t rowdata
) {
125 rowdata
&= rowdata
- 1; // if there are less than two bits (keys) set, rowdata will become zero
129 static inline bool has_ghost_in_row(uint8_t row
, matrix_row_t rowdata
) {
130 /* No ghost exists when less than 2 keys are down on the row.
131 If there are "active" blanks in the matrix, the key can't be pressed by the user,
132 there is no doubt as to which keys are really being pressed.
133 The ghosts will be ignored, they are KC_NO. */
134 rowdata
= get_real_keys(row
, rowdata
);
135 if ((popcount_more_than_one(rowdata
)) == 0) {
138 /* Ghost occurs when the row shares a column line with other row,
139 and two columns are read on each row. Blanks in the matrix don't matter,
140 so they are filtered out.
141 If there are two or more real keys pressed and they match columns with
142 at least two of another row's real keys, the row will be ignored. Keep in mind,
143 we are checking one row at a time, not all of them at once.
145 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
146 if (i
!= row
&& popcount_more_than_one(get_real_keys(i
, matrix_get_row(i
)) & rowdata
)) {
155 void disable_jtag(void) {
156 // To use PF4-7 (PC2-5 on ATmega32A), disable JTAG by writing JTD bit twice within four cycles.
157 #if (defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U4__))
160 #elif defined(__AVR_ATmega32A__)
166 /** \brief matrix_setup
170 __attribute__((weak
)) void matrix_setup(void) {}
172 /** \brief keyboard_pre_init_user
176 __attribute__((weak
)) void keyboard_pre_init_user(void) {}
178 /** \brief keyboard_pre_init_kb
182 __attribute__((weak
)) void keyboard_pre_init_kb(void) { keyboard_pre_init_user(); }
184 /** \brief keyboard_post_init_user
189 __attribute__((weak
)) void keyboard_post_init_user() {}
191 /** \brief keyboard_post_init_kb
196 __attribute__((weak
)) void keyboard_post_init_kb(void) { keyboard_post_init_user(); }
198 /** \brief keyboard_setup
202 void keyboard_setup(void) {
203 #ifndef NO_JTAG_DISABLE
207 keyboard_pre_init_kb();
210 /** \brief is_keyboard_master
214 __attribute__((weak
)) bool is_keyboard_master(void) { return true; }
216 /** \brief keyboard_init
220 void keyboard_init(void) {
229 #ifdef OLED_DRIVER_ENABLE
230 oled_init(OLED_ROTATION_0
);
232 #ifdef PS2_MOUSE_ENABLE
235 #ifdef SERIAL_MOUSE_ENABLE
238 #ifdef ADB_MOUSE_ENABLE
241 #ifdef BOOTMAGIC_ENABLE
246 #ifdef BACKLIGHT_ENABLE
249 #ifdef RGBLIGHT_ENABLE
255 #ifdef FAUXCLICKY_ENABLE
258 #ifdef POINTING_DEVICE_ENABLE
259 pointing_device_init();
261 #if defined(NKRO_ENABLE) && defined(FORCE_NKRO)
262 keymap_config
.nkro
= 1;
263 eeconfig_update_keymap(keymap_config
.raw
);
265 keyboard_post_init_kb(); /* Always keep this last */
268 /** \brief Keyboard task: Do keyboard routine jobs
270 * Do routine keyboard jobs:
273 * * handle mouse movements
274 * * run visualizer code
275 * * handle midi commands
278 * This is repeatedly called as fast as possible.
280 void keyboard_task(void) {
281 static matrix_row_t matrix_prev
[MATRIX_ROWS
];
282 static uint8_t led_status
= 0;
283 matrix_row_t matrix_row
= 0;
284 matrix_row_t matrix_change
= 0;
285 #ifdef QMK_KEYS_PER_SCAN
286 uint8_t keys_processed
= 0;
289 #if defined(OLED_DRIVER_ENABLE) && !defined(OLED_DISABLE_TIMEOUT)
290 uint8_t ret
= matrix_scan();
295 if (is_keyboard_master()) {
296 for (uint8_t r
= 0; r
< MATRIX_ROWS
; r
++) {
297 matrix_row
= matrix_get_row(r
);
298 matrix_change
= matrix_row
^ matrix_prev
[r
];
300 #ifdef MATRIX_HAS_GHOST
301 if (has_ghost_in_row(r
, matrix_row
)) {
305 if (debug_matrix
) matrix_print();
306 matrix_row_t col_mask
= 1;
307 for (uint8_t c
= 0; c
< MATRIX_COLS
; c
++, col_mask
<<= 1) {
308 if (matrix_change
& col_mask
) {
309 action_exec((keyevent_t
){
310 .key
= (keypos_t
){.row
= r
, .col
= c
}, .pressed
= (matrix_row
& col_mask
), .time
= (timer_read() | 1) /* time should not be 0 */
312 // record a processed key
313 matrix_prev
[r
] ^= col_mask
;
314 #ifdef QMK_KEYS_PER_SCAN
315 // only jump out if we have processed "enough" keys.
316 if (++keys_processed
>= QMK_KEYS_PER_SCAN
)
318 // process a key per task call
319 goto MATRIX_LOOP_END
;
325 // call with pseudo tick event when no real key event.
326 #ifdef QMK_KEYS_PER_SCAN
327 // we can get here with some keys processed now.
334 #ifdef DEBUG_MATRIX_SCAN_RATE
335 matrix_scan_perf_task();
342 #ifdef OLED_DRIVER_ENABLE
344 # ifndef OLED_DISABLE_TIMEOUT
345 // Wake up oled if user is using those fabulous keys!
350 #ifdef MOUSEKEY_ENABLE
351 // mousekey repeat & acceleration
355 #ifdef PS2_MOUSE_ENABLE
359 #ifdef SERIAL_MOUSE_ENABLE
363 #ifdef ADB_MOUSE_ENABLE
367 #ifdef SERIAL_LINK_ENABLE
368 serial_link_update();
371 #ifdef VISUALIZER_ENABLE
372 visualizer_update(default_layer_state
, layer_state
, visualizer_get_mods(), host_keyboard_leds());
375 #ifdef POINTING_DEVICE_ENABLE
376 pointing_device_task();
383 #ifdef VELOCIKEY_ENABLE
384 if (velocikey_enabled()) {
385 velocikey_decelerate();
390 if (led_status
!= host_keyboard_leds()) {
391 led_status
= host_keyboard_leds();
392 keyboard_set_leds(led_status
);
396 /** \brief keyboard set leds
400 void keyboard_set_leds(uint8_t leds
) {
401 if (debug_keyboard
) {
402 debug("keyboard_set_led: ");