keyboard.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 "keyboard.h"
  16. #include "matrix.h"
  17. #include "debounce.h"
  18. #include "keymap.h"
  19. #include "host.h"
  20. #include "led.h"
  21. #include "keycode.h"
  22. #include "timer.h"
  23. #include "print.h"
  24. #include "debug.h"
  25. #include "command.h"
  26. #include "util.h"
  27. #include "sendchar.h"
  28. #include "eeconfig.h"
  29. #include "backlight.h"
  30. #include "action_layer.h"
  31. #ifdef BOOTMAGIC_ENABLE
  32. # include "bootmagic.h"
  33. #else
  34. # include "magic.h"
  35. #endif
  36. #ifdef MOUSEKEY_ENABLE
  37. # include "mousekey.h"
  38. #endif
  39. #ifdef PS2_MOUSE_ENABLE
  40. # include "ps2_mouse.h"
  41. #endif
  42. #ifdef SERIAL_MOUSE_ENABLE
  43. # include "serial_mouse.h"
  44. #endif
  45. #ifdef ADB_MOUSE_ENABLE
  46. # include "adb.h"
  47. #endif
  48. #ifdef RGBLIGHT_ENABLE
  49. # include "rgblight.h"
  50. #endif
  51. #ifdef STENO_ENABLE
  52. # include "process_steno.h"
  53. #endif
  54. #ifdef FAUXCLICKY_ENABLE
  55. # include "fauxclicky.h"
  56. #endif
  57. #ifdef SERIAL_LINK_ENABLE
  58. # include "serial_link/system/serial_link.h"
  59. #endif
  60. #ifdef VISUALIZER_ENABLE
  61. # include "visualizer/visualizer.h"
  62. #endif
  63. #ifdef POINTING_DEVICE_ENABLE
  64. # include "pointing_device.h"
  65. #endif
  66. #ifdef MIDI_ENABLE
  67. # include "process_midi.h"
  68. #endif
  69. #ifdef HD44780_ENABLE
  70. # include "hd44780.h"
  71. #endif
  72. #ifdef MATRIX_HAS_GHOST
  73. extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
  74. static matrix_row_t get_real_keys(uint8_t row, matrix_row_t rowdata){
  75. matrix_row_t out = 0;
  76. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  77. //read each key in the row data and check if the keymap defines it as a real key
  78. if (pgm_read_byte(&keymaps[0][row][col]) && (rowdata & (1<<col))){
  79. //this creates new row data, if a key is defined in the keymap, it will be set here
  80. out |= 1<<col;
  81. }
  82. }
  83. return out;
  84. }
  85. static inline bool popcount_more_than_one(matrix_row_t rowdata)
  86. {
  87. rowdata &= rowdata-1; //if there are less than two bits (keys) set, rowdata will become zero
  88. return rowdata;
  89. }
  90. static inline bool has_ghost_in_row(uint8_t row, matrix_row_t rowdata)
  91. {
  92. /* No ghost exists when less than 2 keys are down on the row.
  93. If there are "active" blanks in the matrix, the key can't be pressed by the user,
  94. there is no doubt as to which keys are really being pressed.
  95. The ghosts will be ignored, they are KC_NO. */
  96. rowdata = get_real_keys(row, rowdata);
  97. if ((popcount_more_than_one(rowdata)) == 0){
  98. return false;
  99. }
  100. /* Ghost occurs when the row shares a column line with other row,
  101. and two columns are read on each row. Blanks in the matrix don't matter,
  102. so they are filtered out.
  103. If there are two or more real keys pressed and they match columns with
  104. at least two of another row's real keys, the row will be ignored. Keep in mind,
  105. we are checking one row at a time, not all of them at once.
  106. */
  107. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  108. if (i != row && popcount_more_than_one(get_real_keys(i, matrix_get_row(i)) & rowdata)){
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. #endif
  115. /** \brief matrix_setup
  116. *
  117. * FIXME: needs doc
  118. */
  119. __attribute__ ((weak))
  120. void matrix_setup(void) {
  121. }
  122. /** \brief keyboard_setup
  123. *
  124. * FIXME: needs doc
  125. */
  126. void keyboard_setup(void) {
  127. matrix_setup();
  128. }
  129. /** \brief is_keyboard_master
  130. *
  131. * FIXME: needs doc
  132. */
  133. __attribute__((weak))
  134. bool is_keyboard_master(void) {
  135. return true;
  136. }
  137. /** \brief keyboard_init
  138. *
  139. * FIXME: needs doc
  140. */
  141. void keyboard_init(void) {
  142. timer_init();
  143. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  144. #if (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
  145. MCUCR |= _BV(JTD);
  146. MCUCR |= _BV(JTD);
  147. #endif
  148. matrix_init();
  149. matrix_debounce_init();
  150. #ifdef PS2_MOUSE_ENABLE
  151. ps2_mouse_init();
  152. #endif
  153. #ifdef SERIAL_MOUSE_ENABLE
  154. serial_mouse_init();
  155. #endif
  156. #ifdef ADB_MOUSE_ENABLE
  157. adb_mouse_init();
  158. #endif
  159. #ifdef BOOTMAGIC_ENABLE
  160. bootmagic();
  161. #else
  162. magic();
  163. #endif
  164. #ifdef BACKLIGHT_ENABLE
  165. backlight_init();
  166. #endif
  167. #ifdef RGBLIGHT_ENABLE
  168. rgblight_init();
  169. #endif
  170. #ifdef STENO_ENABLE
  171. steno_init();
  172. #endif
  173. #ifdef FAUXCLICKY_ENABLE
  174. fauxclicky_init();
  175. #endif
  176. #ifdef POINTING_DEVICE_ENABLE
  177. pointing_device_init();
  178. #endif
  179. #if defined(NKRO_ENABLE) && defined(FORCE_NKRO)
  180. keymap_config.nkro = 1;
  181. #endif
  182. }
  183. /** \brief Keyboard task: Do keyboard routine jobs
  184. *
  185. * Do routine keyboard jobs:
  186. *
  187. * * scan matrix
  188. * * handle mouse movements
  189. * * run visualizer code
  190. * * handle midi commands
  191. * * light LEDs
  192. *
  193. * This is repeatedly called as fast as possible.
  194. */
  195. void keyboard_task(void)
  196. {
  197. static matrix_row_t matrix_prev[MATRIX_ROWS];
  198. static uint8_t led_status = 0;
  199. matrix_row_t matrix_row = 0;
  200. matrix_row_t matrix_change = 0;
  201. #ifdef QMK_KEYS_PER_SCAN
  202. uint8_t keys_processed = 0;
  203. #endif
  204. matrix_scan();
  205. matrix_debounce();
  206. if (is_keyboard_master()) {
  207. for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
  208. matrix_row = matrix_debounce_get_row(r);
  209. matrix_change = matrix_row ^ matrix_prev[r];
  210. if (matrix_change) {
  211. #ifdef MATRIX_HAS_GHOST
  212. if (has_ghost_in_row(r, matrix_row)) continue;
  213. #endif
  214. if (debug_matrix) matrix_print();
  215. for (uint8_t c = 0; c < MATRIX_COLS; c++) {
  216. if (matrix_change & ((matrix_row_t)1<<c)) {
  217. action_exec((keyevent_t){
  218. .key = (keypos_t){ .row = r, .col = c },
  219. .pressed = (matrix_row & ((matrix_row_t)1<<c)),
  220. .time = (timer_read() | 1) /* time should not be 0 */
  221. });
  222. // record a processed key
  223. matrix_prev[r] ^= ((matrix_row_t)1<<c);
  224. #ifdef QMK_KEYS_PER_SCAN
  225. // only jump out if we have processed "enough" keys.
  226. if (++keys_processed >= QMK_KEYS_PER_SCAN)
  227. #endif
  228. // process a key per task call
  229. goto MATRIX_LOOP_END;
  230. }
  231. }
  232. }
  233. }
  234. }
  235. // call with pseudo tick event when no real key event.
  236. #ifdef QMK_KEYS_PER_SCAN
  237. // we can get here with some keys processed now.
  238. if (!keys_processed)
  239. #endif
  240. action_exec(TICK);
  241. MATRIX_LOOP_END:
  242. #ifdef MOUSEKEY_ENABLE
  243. // mousekey repeat & acceleration
  244. mousekey_task();
  245. #endif
  246. #ifdef PS2_MOUSE_ENABLE
  247. ps2_mouse_task();
  248. #endif
  249. #ifdef SERIAL_MOUSE_ENABLE
  250. serial_mouse_task();
  251. #endif
  252. #ifdef ADB_MOUSE_ENABLE
  253. adb_mouse_task();
  254. #endif
  255. #ifdef SERIAL_LINK_ENABLE
  256. serial_link_update();
  257. #endif
  258. #ifdef VISUALIZER_ENABLE
  259. visualizer_update(default_layer_state, layer_state, visualizer_get_mods(), host_keyboard_leds());
  260. #endif
  261. #ifdef POINTING_DEVICE_ENABLE
  262. pointing_device_task();
  263. #endif
  264. #ifdef MIDI_ENABLE
  265. midi_task();
  266. #endif
  267. // update LED
  268. if (led_status != host_keyboard_leds()) {
  269. led_status = host_keyboard_leds();
  270. keyboard_set_leds(led_status);
  271. }
  272. }
  273. /** \brief keyboard set leds
  274. *
  275. * FIXME: needs doc
  276. */
  277. void keyboard_set_leds(uint8_t leds)
  278. {
  279. if (debug_keyboard) { debug("keyboard_set_led: "); debug_hex8(leds); debug("\n"); }
  280. led_set(leds);
  281. }