keyboard.c 16 KB

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