You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

449 lines
12 KiB

7 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
Merge ChibiOS and LUFA descriptor support (#2362) * Move lufa descriptor to protocol/usb_descriptor * Try to compile usb_descriptor on ChibiOS * Add lufa_utils for ChibiOS Lufa USB descriptors for ChibiOS * More lufa_util compatibility fixes * First compiling version of shared USB descriptor * Send the usb descriptors * Fix the CONSOLE output on ChibiOS * Add errors for unsupported interfaces * Enable support for vitual serial port USB descriptors * Implement virtual serial port for ChibiOS * Cleanup the lufa_utils Use the default lufa header files * Add raw hid support for ChibiOS This is completely untested * Enable midi compilation on ChibiOS * Move midi functionality out of lufa.c * Don't register sysex callback when not needed * ChibiOS compilation fixes * Update ChibiOS submodule * Fix the Midi USB descriptor It didn't work properly when both Midi and Virtual serial port was enabled. * Add MIDI support for ChibiOS * Fix USB descriptor strings on ChibiOS * Use serial usb driver for raw hid * Generalize the ChibiOS stream like drivers This makes the initialization much more simple and eliminates a lot of the code duplication. * Convert console output to chibios stream driver * Fixes for ChibiOS update * Update the ChibiOS contrib submodule To include the usb data toggle synchronization fixes * Fix duplicate reset enumeration on ChibiOS * Add missing include * Add number of endpoints check for ChibiOS * Enable serial USB driver on all keyboards * Add missing includes when API is enabled withot midi * Add another missing inlcude
6 years ago
  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 "keymap.h"
  18. #include "host.h"
  19. #include "led.h"
  20. #include "keycode.h"
  21. #include "timer.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "command.h"
  25. #include "util.h"
  26. #include "sendchar.h"
  27. #include "eeconfig.h"
  28. #include "backlight.h"
  29. #include "action_layer.h"
  30. #ifdef BOOTMAGIC_ENABLE
  31. # include "bootmagic.h"
  32. #else
  33. # include "magic.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 SERIAL_MOUSE_ENABLE
  42. # include "serial_mouse.h"
  43. #endif
  44. #ifdef ADB_MOUSE_ENABLE
  45. # include "adb.h"
  46. #endif
  47. #ifdef RGBLIGHT_ENABLE
  48. # include "rgblight.h"
  49. #endif
  50. #ifdef STENO_ENABLE
  51. # include "process_steno.h"
  52. #endif
  53. #ifdef FAUXCLICKY_ENABLE
  54. # include "fauxclicky.h"
  55. #endif
  56. #ifdef SERIAL_LINK_ENABLE
  57. # include "serial_link/system/serial_link.h"
  58. #endif
  59. #ifdef VISUALIZER_ENABLE
  60. # include "visualizer/visualizer.h"
  61. #endif
  62. #ifdef POINTING_DEVICE_ENABLE
  63. # include "pointing_device.h"
  64. #endif
  65. #ifdef MIDI_ENABLE
  66. # include "process_midi.h"
  67. #endif
  68. #ifdef QWIIC_KEYBOARD_ENABLE
  69. # include "qwiic/qwiic_keyboard.h"
  70. #endif
  71. #ifdef MATRIX_HAS_GHOST
  72. extern const uint16_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
  73. static matrix_row_t get_real_keys(uint8_t row, matrix_row_t rowdata){
  74. matrix_row_t out = 0;
  75. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  76. //read each key in the row data and check if the keymap defines it as a real key
  77. if (pgm_read_byte(&keymaps[0][row][col]) && (rowdata & (1<<col))){
  78. //this creates new row data, if a key is defined in the keymap, it will be set here
  79. out |= 1<<col;
  80. }
  81. }
  82. return out;
  83. }
  84. static inline bool popcount_more_than_one(matrix_row_t rowdata)
  85. {
  86. rowdata &= rowdata-1; //if there are less than two bits (keys) set, rowdata will become zero
  87. return rowdata;
  88. }
  89. static inline bool has_ghost_in_row(uint8_t row, matrix_row_t rowdata)
  90. {
  91. /* No ghost exists when less than 2 keys are down on the row.
  92. If there are "active" blanks in the matrix, the key can't be pressed by the user,
  93. there is no doubt as to which keys are really being pressed.
  94. The ghosts will be ignored, they are KC_NO. */
  95. rowdata = get_real_keys(row, rowdata);
  96. if ((popcount_more_than_one(rowdata)) == 0){
  97. return false;
  98. }
  99. /* Ghost occurs when the row shares a column line with other row,
  100. and two columns are read on each row. Blanks in the matrix don't matter,
  101. so they are filtered out.
  102. If there are two or more real keys pressed and they match columns with
  103. at least two of another row's real keys, the row will be ignored. Keep in mind,
  104. we are checking one row at a time, not all of them at once.
  105. */
  106. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  107. if (i != row && popcount_more_than_one(get_real_keys(i, matrix_get_row(i)) & rowdata)){
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. #endif
  114. /** \brief matrix_setup
  115. *
  116. * FIXME: needs doc
  117. */
  118. __attribute__ ((weak))
  119. void matrix_setup(void) {
  120. }
  121. /** \brief keyboard_setup
  122. *
  123. * FIXME: needs doc
  124. */
  125. void keyboard_setup(void) {
  126. matrix_setup();
  127. }
  128. /** \brief is_keyboard_master
  129. *
  130. * FIXME: needs doc
  131. */
  132. __attribute__((weak))
  133. bool is_keyboard_master(void) {
  134. return true;
  135. }
  136. /** \brief Get the number of currently connected matrices
  137. *
  138. * Implement this if you support multiple matrices, see qwiic_keyboard.c for an example
  139. * NOTE: It should return the number of additional matrices, the master is always implied.
  140. */
  141. __attribute__((weak))
  142. uint8_t multimatrix_get_num_matrices(void) {
  143. return 0;
  144. }
  145. /** \brief Get the number of columns of a connected matrix
  146. *
  147. * Implement this if you support multiple matrices, see qwiic_keyboard.c for an example
  148. * NOTE: The matrix index 0 is the first remote matrix, the function is not called for the master
  149. */
  150. __attribute__((weak))
  151. uint8_t multimatrix_get_num_cols(uint8_t matrix) {
  152. return 0;
  153. }
  154. /** \brief Get the number of rows of a connected matrix
  155. *
  156. * Implement this if you support multiple matrices, see qwiic_keyboard.c for an example
  157. * NOTE: The matrix index 0 is the first remote matrix, the function is not called for the master
  158. */
  159. __attribute__((weak))
  160. uint8_t multimatrix_get_num_rows(uint8_t matrix) {
  161. return 0;
  162. }
  163. /** \brief Get the row status of a connected matrix
  164. *
  165. * Implement this if you support multiple matrices, see qwiic_keyboard.c for an example
  166. * NOTE: The matrix index 0 is the first remote matrix, the function is not called for the master
  167. */
  168. __attribute__((weak))
  169. uint32_t multimatrix_get_row(uint8_t matrix, uint8_t row) {
  170. return 0;
  171. }
  172. /** \brief Get the row cache of a connected matrix
  173. *
  174. * Implement this if you support multiple matrices, see qwiic_keyboard.c for an example
  175. * NOTE: The matrix index 0 is the first remote matrix, the function is not called for the master
  176. */
  177. __attribute__((weak))
  178. uint32_t multimatrix_get_row_cache(uint8_t matrix, uint8_t row) {
  179. return 0;
  180. }
  181. /** \brief Set the row cache of a connected matrix
  182. *
  183. * Implement this if you support multiple matrices, see qwiic_keyboard.c for an example
  184. * NOTE: The matrix index 0 is the first remote matrix, the function is not called for the master
  185. */
  186. __attribute__((weak))
  187. void multimatrix_set_row_cache(uint8_t matrix, uint8_t row, uint32_t value) {
  188. }
  189. /** \brief Get the number of currently connected matrices
  190. *
  191. * For normal keyboards this usually returns 1, but for multi-matrix keyboards this will
  192. * return the total number of connected keyboards/modules including the master
  193. */
  194. uint8_t keyboard_get_num_matrices(void) {
  195. return 1 + multimatrix_get_num_matrices();
  196. }
  197. /** \brief Get the number of columns of a connected matrix
  198. *
  199. * Specify the matrix index to query connected multi-matrix keyboards/modules
  200. * 0 is always the master
  201. */
  202. uint8_t keyboard_get_num_cols(uint8_t matrix) {
  203. if (matrix == 0) {
  204. return MATRIX_COLS;
  205. } else {
  206. return multimatrix_get_num_cols(matrix - 1);
  207. }
  208. }
  209. /** \brief Get the number of rows of a connected matrix
  210. *
  211. * Specify the matrix index to query connected multi-matrix keyboards/modules
  212. * 0 is always the master
  213. */
  214. uint8_t keyboard_get_num_rows(uint8_t matrix) {
  215. if (matrix == 0) {
  216. return MATRIX_ROWS;
  217. } else {
  218. return multimatrix_get_num_rows(matrix - 1);
  219. }
  220. }
  221. /** \brief Get the row status of a connected matrix
  222. *
  223. * Specify the matrix index to query connected multi-matrix keyboards/modules
  224. * 0 is always the master
  225. */
  226. uint32_t keyboard_get_row(uint8_t matrix, uint8_t row) {
  227. if (matrix == 0) {
  228. return matrix_get_row(row);
  229. } else {
  230. return multimatrix_get_row(matrix - 1, row);
  231. }
  232. }
  233. static matrix_row_t matrix_prev[MATRIX_ROWS];
  234. static uint32_t get_row_cache(uint8_t matrix, uint8_t row) {
  235. if (matrix == 0) {
  236. return matrix_prev[row];
  237. } else {
  238. return multimatrix_get_row_cache(matrix - 1, row);
  239. }
  240. }
  241. static void set_row_cache(uint8_t matrix, uint8_t row, uint32_t value) {
  242. if (matrix == 0) {
  243. matrix_prev[row] = value;
  244. } else {
  245. return multimatrix_set_row_cache(matrix - 1, row, value);
  246. }
  247. }
  248. /** \brief keyboard_init
  249. *
  250. * FIXME: needs doc
  251. */
  252. void keyboard_init(void) {
  253. timer_init();
  254. matrix_init();
  255. #ifdef PS2_MOUSE_ENABLE
  256. ps2_mouse_init();
  257. #endif
  258. #ifdef SERIAL_MOUSE_ENABLE
  259. serial_mouse_init();
  260. #endif
  261. #ifdef ADB_MOUSE_ENABLE
  262. adb_mouse_init();
  263. #endif
  264. #ifdef BOOTMAGIC_ENABLE
  265. bootmagic();
  266. #else
  267. magic();
  268. #endif
  269. #ifdef BACKLIGHT_ENABLE
  270. backlight_init();
  271. #endif
  272. #ifdef RGBLIGHT_ENABLE
  273. rgblight_init();
  274. #endif
  275. #ifdef STENO_ENABLE
  276. steno_init();
  277. #endif
  278. #ifdef FAUXCLICKY_ENABLE
  279. fauxclicky_init();
  280. #endif
  281. #ifdef POINTING_DEVICE_ENABLE
  282. pointing_device_init();
  283. #endif
  284. #if defined(NKRO_ENABLE) && defined(FORCE_NKRO)
  285. keymap_config.nkro = 1;
  286. #endif
  287. #ifdef QWIIC_KEYBOARD_ENABLE
  288. qwiic_keyboard_init();
  289. #endif
  290. }
  291. /** \brief Keyboard task: Do keyboard routine jobs
  292. *
  293. * Do routine keyboard jobs:
  294. *
  295. * * scan matrix
  296. * * handle mouse movements
  297. * * run visualizer code
  298. * * handle midi commands
  299. * * light LEDs
  300. *
  301. * This is repeatedly called as fast as possible.
  302. */
  303. void keyboard_task(void)
  304. {
  305. static uint8_t led_status = 0;
  306. uint32_t matrix_row = 0;
  307. uint32_t matrix_change = 0;
  308. #ifdef QMK_KEYS_PER_SCAN
  309. uint8_t keys_processed = 0;
  310. #endif
  311. matrix_scan();
  312. if (is_keyboard_master()) {
  313. for (uint8_t m = 0; m < keyboard_get_num_matrices(); m++) {
  314. uint8_t num_cols = keyboard_get_num_cols(m);
  315. uint8_t num_rows = keyboard_get_num_rows(m);
  316. for (uint8_t r = 0; r < num_rows; r++) {
  317. matrix_row = keyboard_get_row(m, r);
  318. uint32_t row_cache = get_row_cache(m, r);
  319. matrix_change = matrix_row ^ row_cache;
  320. if (matrix_change) {
  321. #ifdef MATRIX_HAS_GHOST
  322. //NOTE: The we support ghosting only for the main matrix, since it's only useful for old keyboards without diodes
  323. if (has_ghost_in_row(r, matrix_row)) {
  324. /* Keep track of whether ghosted status has changed for
  325. * debugging. But don't update matrix_prev until un-ghosted, or
  326. * the last key would be lost.
  327. */
  328. //if (debug_matrix && matrix_ghost[r] != matrix_row) {
  329. // matrix_print();
  330. //}
  331. //matrix_ghost[r] = matrix_row;
  332. continue;
  333. }
  334. //matrix_ghost[r] = matrix_row;
  335. #endif
  336. if (debug_matrix) matrix_print();
  337. for (uint8_t c = 0; c < num_cols; c++) {
  338. if (matrix_change & (uint32_t)(1u<<c)) {
  339. action_exec((keyevent_t){
  340. // The main matrix is always 0
  341. .key = (keymatrix_t){.pos = (keypos_t){ .row = r, .col = c }, .matrix = m},
  342. .pressed = (matrix_row & ((uint32_t)1u<<c)),
  343. .time = (timer_read() | 1) /* time should not be 0 */
  344. });
  345. // record a processed key
  346. row_cache ^= (uint32_t)(1u<<c);
  347. set_row_cache(m, r, row_cache);
  348. #ifdef QMK_KEYS_PER_SCAN
  349. // only jump out if we have processed "enough" keys.
  350. if (++keys_processed >= QMK_KEYS_PER_SCAN)
  351. #endif
  352. // process a key per task call
  353. goto MATRIX_LOOP_END;
  354. }
  355. }
  356. }
  357. }
  358. }
  359. }
  360. // call with pseudo tick event when no real key event.
  361. #ifdef QMK_KEYS_PER_SCAN
  362. // we can get here with some keys processed now.
  363. if (!keys_processed)
  364. #endif
  365. action_exec(TICK);
  366. MATRIX_LOOP_END:
  367. #ifdef MOUSEKEY_ENABLE
  368. // mousekey repeat & acceleration
  369. mousekey_task();
  370. #endif
  371. #ifdef PS2_MOUSE_ENABLE
  372. ps2_mouse_task();
  373. #endif
  374. #ifdef SERIAL_MOUSE_ENABLE
  375. serial_mouse_task();
  376. #endif
  377. #ifdef ADB_MOUSE_ENABLE
  378. adb_mouse_task();
  379. #endif
  380. #ifdef SERIAL_LINK_ENABLE
  381. serial_link_update();
  382. #endif
  383. #ifdef VISUALIZER_ENABLE
  384. visualizer_update(default_layer_state, layer_state, visualizer_get_mods(), host_keyboard_leds());
  385. #endif
  386. #ifdef POINTING_DEVICE_ENABLE
  387. pointing_device_task();
  388. #endif
  389. #ifdef MIDI_ENABLE
  390. midi_task();
  391. #endif
  392. #ifdef QWIIC_KEYBOARD_ENABLE
  393. qwiic_keyboard_task();
  394. #endif
  395. // update LED
  396. if (led_status != host_keyboard_leds()) {
  397. led_status = host_keyboard_leds();
  398. keyboard_set_leds(led_status);
  399. }
  400. }
  401. /** \brief keyboard set leds
  402. *
  403. * FIXME: needs doc
  404. */
  405. void keyboard_set_leds(uint8_t leds)
  406. {
  407. if (debug_keyboard) { debug("keyboard_set_led: "); debug_hex8(leds); debug("\n"); }
  408. led_set(leds);
  409. }