keyboard.c
18581 bytes
1/*
2Copyright 2011, 2012, 2013 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#include <stdint.h>
19#include "keyboard.h"
20#include "keycode_config.h"
21#include "matrix.h"
22#include "keymap_introspection.h"
23#include "host.h"
24#include "led.h"
25#include "keycode.h"
26#include "timer.h"
27#include "sync_timer.h"
28#include "print.h"
29#include "debug.h"
30#include "command.h"
31#include "util.h"
32#include "sendchar.h"
33#include "eeconfig.h"
34#include "action_layer.h"
35#ifdef BOOTMAGIC_ENABLE
36# include "bootmagic.h"
37#endif
38#ifdef AUDIO_ENABLE
39# include "audio.h"
40#endif
41#if defined(AUDIO_ENABLE) || (defined(MIDI_ENABLE) && defined(MIDI_BASIC))
42# include "process_music.h"
43#endif
44#ifdef BACKLIGHT_ENABLE
45# include "backlight.h"
46#endif
47#ifdef MOUSEKEY_ENABLE
48# include "mousekey.h"
49#endif
50#ifdef PS2_MOUSE_ENABLE
51# include "ps2_mouse.h"
52#endif
53#ifdef RGBLIGHT_ENABLE
54# include "rgblight.h"
55#endif
56#ifdef LED_MATRIX_ENABLE
57# include "led_matrix.h"
58#endif
59#ifdef RGB_MATRIX_ENABLE
60# include "rgb_matrix.h"
61#endif
62#ifdef ENCODER_ENABLE
63# include "encoder.h"
64#endif
65#ifdef HAPTIC_ENABLE
66# include "haptic.h"
67#endif
68#ifdef AUTO_SHIFT_ENABLE
69# include "process_auto_shift.h"
70#endif
71#ifdef COMBO_ENABLE
72# include "process_combo.h"
73#endif
74#ifdef TAP_DANCE_ENABLE
75# include "process_tap_dance.h"
76#endif
77#ifdef STENO_ENABLE
78# include "process_steno.h"
79#endif
80#ifdef KEY_OVERRIDE_ENABLE
81# include "process_key_override.h"
82#endif
83#ifdef SECURE_ENABLE
84# include "secure.h"
85#endif
86#ifdef POINTING_DEVICE_ENABLE
87# include "pointing_device.h"
88#endif
89#ifdef MIDI_ENABLE
90# include "process_midi.h"
91#endif
92#ifdef JOYSTICK_ENABLE
93# include "joystick.h"
94#endif
95#ifdef HD44780_ENABLE
96# include "hd44780.h"
97#endif
98#ifdef OLED_ENABLE
99# include "oled_driver.h"
100#endif
101#ifdef ST7565_ENABLE
102# include "st7565.h"
103#endif
104#ifdef VIA_ENABLE
105# include "via.h"
106#endif
107#ifdef DIP_SWITCH_ENABLE
108# include "dip_switch.h"
109#endif
110#ifdef EEPROM_DRIVER
111# include "eeprom_driver.h"
112#endif
113#if defined(CRC_ENABLE)
114# include "crc.h"
115#endif
116#ifdef VIRTSER_ENABLE
117# include "virtser.h"
118#endif
119#ifdef SLEEP_LED_ENABLE
120# include "sleep_led.h"
121#endif
122#ifdef SPLIT_KEYBOARD
123# include "split_util.h"
124#endif
125#ifdef BLUETOOTH_ENABLE
126# include "bluetooth.h"
127#endif
128#ifdef CAPS_WORD_ENABLE
129# include "caps_word.h"
130#endif
131#ifdef LEADER_ENABLE
132# include "leader.h"
133#endif
134#ifdef UNICODE_COMMON_ENABLE
135# include "unicode.h"
136#endif
137#ifdef WPM_ENABLE
138# include "wpm.h"
139#endif
140#ifdef OS_DETECTION_ENABLE
141# include "os_detection.h"
142#endif
143#ifdef LAYER_LOCK_ENABLE
144# include "layer_lock.h"
145#endif
146
147static uint32_t last_input_modification_time = 0;
148uint32_t last_input_activity_time(void) {
149 return last_input_modification_time;
150}
151uint32_t last_input_activity_elapsed(void) {
152 return sync_timer_elapsed32(last_input_modification_time);
153}
154
155static uint32_t last_matrix_modification_time = 0;
156uint32_t last_matrix_activity_time(void) {
157 return last_matrix_modification_time;
158}
159uint32_t last_matrix_activity_elapsed(void) {
160 return sync_timer_elapsed32(last_matrix_modification_time);
161}
162void last_matrix_activity_trigger(void) {
163 last_matrix_modification_time = last_input_modification_time = sync_timer_read32();
164}
165
166static uint32_t last_encoder_modification_time = 0;
167uint32_t last_encoder_activity_time(void) {
168 return last_encoder_modification_time;
169}
170uint32_t last_encoder_activity_elapsed(void) {
171 return sync_timer_elapsed32(last_encoder_modification_time);
172}
173void last_encoder_activity_trigger(void) {
174 last_encoder_modification_time = last_input_modification_time = sync_timer_read32();
175}
176
177static uint32_t last_pointing_device_modification_time = 0;
178uint32_t last_pointing_device_activity_time(void) {
179 return last_pointing_device_modification_time;
180}
181uint32_t last_pointing_device_activity_elapsed(void) {
182 return sync_timer_elapsed32(last_pointing_device_modification_time);
183}
184void last_pointing_device_activity_trigger(void) {
185 last_pointing_device_modification_time = last_input_modification_time = sync_timer_read32();
186}
187
188void set_activity_timestamps(uint32_t matrix_timestamp, uint32_t encoder_timestamp, uint32_t pointing_device_timestamp) {
189 last_matrix_modification_time = matrix_timestamp;
190 last_encoder_modification_time = encoder_timestamp;
191 last_pointing_device_modification_time = pointing_device_timestamp;
192 last_input_modification_time = MAX(matrix_timestamp, MAX(encoder_timestamp, pointing_device_timestamp));
193}
194
195// Only enable this if console is enabled to print to
196#if defined(DEBUG_MATRIX_SCAN_RATE)
197static uint32_t matrix_timer = 0;
198static uint32_t matrix_scan_count = 0;
199static uint32_t last_matrix_scan_count = 0;
200
201void matrix_scan_perf_task(void) {
202 matrix_scan_count++;
203
204 uint32_t timer_now = timer_read32();
205 if (TIMER_DIFF_32(timer_now, matrix_timer) >= 1000) {
206# if defined(CONSOLE_ENABLE)
207 dprintf("matrix scan frequency: %lu\n", matrix_scan_count);
208# endif
209 last_matrix_scan_count = matrix_scan_count;
210 matrix_timer = timer_now;
211 matrix_scan_count = 0;
212 }
213}
214
215uint32_t get_matrix_scan_rate(void) {
216 return last_matrix_scan_count;
217}
218#else
219# define matrix_scan_perf_task()
220#endif
221
222#ifdef MATRIX_HAS_GHOST
223static matrix_row_t get_real_keys(uint8_t row, matrix_row_t rowdata) {
224 matrix_row_t out = 0;
225 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
226 // read each key in the row data and check if the keymap defines it as a real key
227 if (keycode_at_keymap_location(0, row, col) && (rowdata & (((matrix_row_t)1) << col))) {
228 // this creates new row data, if a key is defined in the keymap, it will be set here
229 out |= ((matrix_row_t)1) << col;
230 }
231 }
232 return out;
233}
234
235static inline bool popcount_more_than_one(matrix_row_t rowdata) {
236 rowdata &= rowdata - 1; // if there are less than two bits (keys) set, rowdata will become zero
237 return rowdata;
238}
239
240static inline bool has_ghost_in_row(uint8_t row, matrix_row_t rowdata) {
241 /* No ghost exists when less than 2 keys are down on the row.
242 If there are "active" blanks in the matrix, the key can't be pressed by the user,
243 there is no doubt as to which keys are really being pressed.
244 The ghosts will be ignored, they are KC_NO. */
245 rowdata = get_real_keys(row, rowdata);
246 if ((popcount_more_than_one(rowdata)) == 0) {
247 return false;
248 }
249 /* Ghost occurs when the row shares a column line with other row,
250 and two columns are read on each row. Blanks in the matrix don't matter,
251 so they are filtered out.
252 If there are two or more real keys pressed and they match columns with
253 at least two of another row's real keys, the row will be ignored. Keep in mind,
254 we are checking one row at a time, not all of them at once.
255 */
256 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
257 if (i != row && popcount_more_than_one(get_real_keys(i, matrix_get_row(i)) & rowdata)) {
258 return true;
259 }
260 }
261 return false;
262}
263
264#else
265
266static inline bool has_ghost_in_row(uint8_t row, matrix_row_t rowdata) {
267 return false;
268}
269
270#endif
271
272/** \brief matrix_setup
273 *
274 * FIXME: needs doc
275 */
276__attribute__((weak)) void matrix_setup(void) {}
277
278/** \brief keyboard_pre_init_user
279 *
280 * FIXME: needs doc
281 */
282__attribute__((weak)) void keyboard_pre_init_user(void) {}
283
284/** \brief keyboard_pre_init_kb
285 *
286 * FIXME: needs doc
287 */
288__attribute__((weak)) void keyboard_pre_init_kb(void) {
289 keyboard_pre_init_user();
290}
291
292/** \brief keyboard_post_init_user
293 *
294 * FIXME: needs doc
295 */
296
297__attribute__((weak)) void keyboard_post_init_user(void) {}
298
299/** \brief keyboard_post_init_kb
300 *
301 * FIXME: needs doc
302 */
303
304__attribute__((weak)) void keyboard_post_init_kb(void) {
305 keyboard_post_init_user();
306}
307
308/** \brief matrix_can_read
309 *
310 * Allows overriding when matrix scanning operations should be executed.
311 */
312__attribute__((weak)) bool matrix_can_read(void) {
313 return true;
314}
315
316/** \brief keyboard_setup
317 *
318 * FIXME: needs doc
319 */
320void keyboard_setup(void) {
321 print_set_sendchar(sendchar);
322#ifdef EEPROM_DRIVER
323 eeprom_driver_init();
324#endif
325 matrix_setup();
326 keyboard_pre_init_kb();
327}
328
329#ifndef SPLIT_KEYBOARD
330
331/** \brief is_keyboard_master
332 *
333 * FIXME: needs doc
334 */
335__attribute__((weak)) bool is_keyboard_master(void) {
336 return true;
337}
338
339/** \brief is_keyboard_left
340 *
341 * FIXME: needs doc
342 */
343__attribute__((weak)) bool is_keyboard_left(void) {
344 return true;
345}
346
347#endif
348
349/** \brief should_process_keypress
350 *
351 * Override this function if you have a condition where keypresses processing should change:
352 * - splits where the slave side needs to process for rgb/oled functionality
353 */
354__attribute__((weak)) bool should_process_keypress(void) {
355 return is_keyboard_master();
356}
357
358/** \brief housekeeping_task_kb
359 *
360 * Override this function if you have a need to execute code for every keyboard main loop iteration.
361 * This is specific to keyboard-level functionality.
362 */
363__attribute__((weak)) void housekeeping_task_kb(void) {}
364
365/** \brief housekeeping_task_user
366 *
367 * Override this function if you have a need to execute code for every keyboard main loop iteration.
368 * This is specific to user/keymap-level functionality.
369 */
370__attribute__((weak)) void housekeeping_task_user(void) {}
371
372/** \brief housekeeping_task
373 *
374 * Invokes hooks for executing code after QMK is done after each loop iteration.
375 */
376void housekeeping_task(void) {
377 housekeeping_task_kb();
378 housekeeping_task_user();
379}
380
381/** \brief quantum_init
382 *
383 * Init global state
384 */
385void quantum_init(void) {
386 /* check signature */
387 if (!eeconfig_is_enabled()) {
388 eeconfig_init();
389 }
390
391 /* init globals */
392 debug_config.raw = eeconfig_read_debug();
393 keymap_config.raw = eeconfig_read_keymap();
394
395#ifdef BOOTMAGIC_ENABLE
396 bootmagic();
397#endif
398
399 /* read here just incase bootmagic process changed its value */
400 layer_state_t default_layer = (layer_state_t)eeconfig_read_default_layer();
401 default_layer_set(default_layer);
402
403 /* Also initialize layer state to trigger callback functions for layer_state */
404 layer_state_set_kb((layer_state_t)layer_state);
405}
406
407/** \brief keyboard_init
408 *
409 * FIXME: needs doc
410 */
411void keyboard_init(void) {
412 timer_init();
413 sync_timer_init();
414#ifdef VIA_ENABLE
415 via_init();
416#endif
417#ifdef SPLIT_KEYBOARD
418 split_pre_init();
419#endif
420#ifdef ENCODER_ENABLE
421 encoder_init();
422#endif
423 matrix_init();
424 quantum_init();
425 led_init_ports();
426#ifdef BACKLIGHT_ENABLE
427 backlight_init_ports();
428#endif
429#ifdef AUDIO_ENABLE
430 audio_init();
431#endif
432#ifdef LED_MATRIX_ENABLE
433 led_matrix_init();
434#endif
435#ifdef RGB_MATRIX_ENABLE
436 rgb_matrix_init();
437#endif
438#if defined(UNICODE_COMMON_ENABLE)
439 unicode_input_mode_init();
440#endif
441#if defined(CRC_ENABLE)
442 crc_init();
443#endif
444#ifdef OLED_ENABLE
445 oled_init(OLED_ROTATION_0);
446#endif
447#ifdef ST7565_ENABLE
448 st7565_init(DISPLAY_ROTATION_0);
449#endif
450#ifdef PS2_MOUSE_ENABLE
451 ps2_mouse_init();
452#endif
453#ifdef BACKLIGHT_ENABLE
454 backlight_init();
455#endif
456#ifdef RGBLIGHT_ENABLE
457 rgblight_init();
458#endif
459#ifdef STENO_ENABLE_ALL
460 steno_init();
461#endif
462#if defined(NKRO_ENABLE) && defined(FORCE_NKRO)
463 keymap_config.nkro = 1;
464 eeconfig_update_keymap(keymap_config.raw);
465#endif
466#ifdef DIP_SWITCH_ENABLE
467 dip_switch_init();
468#endif
469#ifdef JOYSTICK_ENABLE
470 joystick_init();
471#endif
472#ifdef SLEEP_LED_ENABLE
473 sleep_led_init();
474#endif
475#ifdef VIRTSER_ENABLE
476 virtser_init();
477#endif
478#ifdef SPLIT_KEYBOARD
479 split_post_init();
480#endif
481#ifdef POINTING_DEVICE_ENABLE
482 // init after split init
483 pointing_device_init();
484#endif
485#ifdef BLUETOOTH_ENABLE
486 bluetooth_init();
487#endif
488#ifdef HAPTIC_ENABLE
489 haptic_init();
490#endif
491
492#if defined(DEBUG_MATRIX_SCAN_RATE) && defined(CONSOLE_ENABLE)
493 debug_enable = true;
494#endif
495
496 keyboard_post_init_kb(); /* Always keep this last */
497}
498
499/** \brief key_event_task
500 *
501 * This function is responsible for calling into other systems when they need to respond to electrical switch press events.
502 * This is differnet than keycode events as no layer processing, or filtering occurs.
503 */
504void switch_events(uint8_t row, uint8_t col, bool pressed) {
505#if defined(LED_MATRIX_ENABLE)
506 led_matrix_handle_key_event(row, col, pressed);
507#endif
508#if defined(RGB_MATRIX_ENABLE)
509 rgb_matrix_handle_key_event(row, col, pressed);
510#endif
511}
512
513/**
514 * @brief Generates a tick event at a maximum rate of 1KHz that drives the
515 * internal QMK state machine.
516 */
517static inline void generate_tick_event(void) {
518 static uint16_t last_tick = 0;
519 const uint16_t now = timer_read();
520 if (TIMER_DIFF_16(now, last_tick) != 0) {
521 action_exec(MAKE_TICK_EVENT);
522 last_tick = now;
523 }
524}
525
526/**
527 * @brief This task scans the keyboards matrix and processes any key presses
528 * that occur.
529 *
530 * @return true Matrix did change
531 * @return false Matrix didn't change
532 */
533static bool matrix_task(void) {
534 if (!matrix_can_read()) {
535 generate_tick_event();
536 return false;
537 }
538
539 static matrix_row_t matrix_previous[MATRIX_ROWS];
540
541 matrix_scan();
542 bool matrix_changed = false;
543 for (uint8_t row = 0; row < MATRIX_ROWS && !matrix_changed; row++) {
544 matrix_changed |= matrix_previous[row] ^ matrix_get_row(row);
545 }
546
547 matrix_scan_perf_task();
548
549 // Short-circuit the complete matrix processing if it is not necessary
550 if (!matrix_changed) {
551 generate_tick_event();
552 return matrix_changed;
553 }
554
555 if (debug_config.matrix) {
556 matrix_print();
557 }
558
559 const bool process_keypress = should_process_keypress();
560
561 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
562 const matrix_row_t current_row = matrix_get_row(row);
563 const matrix_row_t row_changes = current_row ^ matrix_previous[row];
564
565 if (!row_changes || has_ghost_in_row(row, current_row)) {
566 continue;
567 }
568
569 matrix_row_t col_mask = 1;
570 for (uint8_t col = 0; col < MATRIX_COLS; col++, col_mask <<= 1) {
571 if (row_changes & col_mask) {
572 const bool key_pressed = current_row & col_mask;
573
574 if (process_keypress) {
575 action_exec(MAKE_KEYEVENT(row, col, key_pressed));
576 }
577
578 switch_events(row, col, key_pressed);
579 }
580 }
581
582 matrix_previous[row] = current_row;
583 }
584
585 return matrix_changed;
586}
587
588/** \brief Tasks previously located in matrix_scan_quantum
589 *
590 * TODO: rationalise against keyboard_task and current split role
591 */
592void quantum_task(void) {
593#ifdef SPLIT_KEYBOARD
594 // some tasks should only run on master
595 if (!is_keyboard_master()) return;
596#endif
597
598#if defined(AUDIO_ENABLE) && defined(AUDIO_INIT_DELAY)
599 // There are some tasks that need to be run a little bit
600 // after keyboard startup, or else they will not work correctly
601 // because of interaction with the USB device state, which
602 // may still be in flux...
603 //
604 // At the moment the only feature that needs this is the
605 // startup song.
606 static bool delayed_tasks_run = false;
607 static uint16_t delayed_task_timer = 0;
608 if (!delayed_tasks_run) {
609 if (!delayed_task_timer) {
610 delayed_task_timer = timer_read();
611 } else if (timer_elapsed(delayed_task_timer) > 300) {
612 audio_startup();
613 delayed_tasks_run = true;
614 }
615 }
616#endif
617
618#if defined(AUDIO_ENABLE) && !defined(NO_MUSIC_MODE)
619 music_task();
620#endif
621
622#ifdef KEY_OVERRIDE_ENABLE
623 key_override_task();
624#endif
625
626#ifdef SEQUENCER_ENABLE
627 sequencer_task();
628#endif
629
630#ifdef TAP_DANCE_ENABLE
631 tap_dance_task();
632#endif
633
634#ifdef COMBO_ENABLE
635 combo_task();
636#endif
637
638#ifdef LEADER_ENABLE
639 leader_task();
640#endif
641
642#ifdef WPM_ENABLE
643 decay_wpm();
644#endif
645
646#ifdef DIP_SWITCH_ENABLE
647 dip_switch_task();
648#endif
649
650#ifdef AUTO_SHIFT_ENABLE
651 autoshift_matrix_scan();
652#endif
653
654#ifdef CAPS_WORD_ENABLE
655 caps_word_task();
656#endif
657
658#ifdef SECURE_ENABLE
659 secure_task();
660#endif
661
662#ifdef LAYER_LOCK_ENABLE
663 layer_lock_task();
664#endif
665}
666
667/** \brief Main task that is repeatedly called as fast as possible. */
668void keyboard_task(void) {
669 __attribute__((unused)) bool activity_has_occurred = false;
670 if (matrix_task()) {
671 last_matrix_activity_trigger();
672 activity_has_occurred = true;
673 }
674
675 quantum_task();
676
677#if defined(SPLIT_WATCHDOG_ENABLE)
678 split_watchdog_task();
679#endif
680
681#if defined(RGBLIGHT_ENABLE)
682 rgblight_task();
683#endif
684
685#ifdef LED_MATRIX_ENABLE
686 led_matrix_task();
687#endif
688#ifdef RGB_MATRIX_ENABLE
689 rgb_matrix_task();
690#endif
691
692#if defined(BACKLIGHT_ENABLE)
693# if defined(BACKLIGHT_PIN) || defined(BACKLIGHT_PINS)
694 backlight_task();
695# endif
696#endif
697
698#ifdef ENCODER_ENABLE
699 if (encoder_task()) {
700 last_encoder_activity_trigger();
701 activity_has_occurred = true;
702 }
703#endif
704
705#ifdef POINTING_DEVICE_ENABLE
706 if (pointing_device_task()) {
707 last_pointing_device_activity_trigger();
708 activity_has_occurred = true;
709 }
710#endif
711
712#ifdef OLED_ENABLE
713 oled_task();
714# if OLED_TIMEOUT > 0
715 // Wake up oled if user is using those fabulous keys or spinning those encoders!
716 if (activity_has_occurred) oled_on();
717# endif
718#endif
719
720#ifdef ST7565_ENABLE
721 st7565_task();
722# if ST7565_TIMEOUT > 0
723 // Wake up display if user is using those fabulous keys or spinning those encoders!
724 if (activity_has_occurred) st7565_on();
725# endif
726#endif
727
728#ifdef MOUSEKEY_ENABLE
729 // mousekey repeat & acceleration
730 mousekey_task();
731#endif
732
733#ifdef PS2_MOUSE_ENABLE
734 ps2_mouse_task();
735#endif
736
737#ifdef MIDI_ENABLE
738 midi_task();
739#endif
740
741#ifdef JOYSTICK_ENABLE
742 joystick_task();
743#endif
744
745#ifdef BLUETOOTH_ENABLE
746 bluetooth_task();
747#endif
748
749#ifdef HAPTIC_ENABLE
750 haptic_task();
751#endif
752
753 led_task();
754
755#ifdef OS_DETECTION_ENABLE
756 os_detection_task();
757#endif
758}