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.

319 lines
7.8 KiB

2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
  1. /* Copyright 2018 Evy Dekkers
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #if defined(__AVR__)
  19. #include <avr/io.h>
  20. #endif
  21. #include "wait.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "util.h"
  25. #include "matrix.h"
  26. #include "timer.h"
  27. /* Set 0 if debouncing isn't needed */
  28. #ifndef DEBOUNCE
  29. # define DEBOUNCE 5
  30. #endif
  31. #define COL_SHIFTER ((uint32_t)1)
  32. static uint16_t debouncing_time;
  33. static bool debouncing = false;
  34. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  35. /* matrix state(1:on, 0:off) */
  36. static matrix_row_t matrix[MATRIX_ROWS];
  37. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  38. static void init_rows(void);
  39. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  40. static void unselect_cols(void);
  41. static void select_col(uint8_t col);
  42. __attribute__ ((weak))
  43. void matrix_init_user(void) {}
  44. __attribute__ ((weak))
  45. void matrix_scan_user(void) {}
  46. __attribute__ ((weak))
  47. void matrix_init_kb(void) {
  48. matrix_init_user();
  49. }
  50. __attribute__ ((weak))
  51. void matrix_scan_kb(void) {
  52. matrix_scan_user();
  53. }
  54. inline
  55. uint8_t matrix_rows(void) {
  56. return MATRIX_ROWS;
  57. }
  58. inline
  59. uint8_t matrix_cols(void) {
  60. return MATRIX_COLS;
  61. }
  62. void matrix_init(void) {
  63. unselect_cols();
  64. init_rows();
  65. // initialize matrix state: all keys off
  66. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  67. matrix[i] = 0;
  68. matrix_debouncing[i] = 0;
  69. }
  70. matrix_init_quantum();
  71. }
  72. uint8_t matrix_scan(void)
  73. {
  74. // Set col, read rows
  75. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  76. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  77. if (matrix_changed) {
  78. debouncing = true;
  79. debouncing_time = timer_read();
  80. }
  81. }
  82. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
  83. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  84. matrix[i] = matrix_debouncing[i];
  85. }
  86. debouncing = false;
  87. }
  88. matrix_scan_quantum();
  89. return 1;
  90. }
  91. inline
  92. bool matrix_is_on(uint8_t row, uint8_t col)
  93. {
  94. return (matrix[row] & ((matrix_row_t)1<<col));
  95. }
  96. inline
  97. matrix_row_t matrix_get_row(uint8_t row)
  98. {
  99. return matrix[row];
  100. }
  101. void matrix_print(void)
  102. {
  103. print("\nr/c 0123456789ABCDEFGHIJKLMNOPQRSTUV \n");
  104. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  105. print_hex8(row); print(": ");
  106. print_bin_reverse32(matrix_get_row(row));
  107. print("\n");
  108. }
  109. }
  110. uint8_t matrix_key_count(void)
  111. {
  112. uint8_t count = 0;
  113. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  114. count += bitpop32(matrix[i]);
  115. }
  116. return count;
  117. }
  118. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  119. {
  120. bool matrix_changed = false;
  121. // Select col and wait for col selecton to stabilize
  122. select_col(current_col);
  123. wait_us(30);
  124. // For each row...
  125. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  126. {
  127. // Store last value of row prior to reading
  128. matrix_row_t last_row_value = current_matrix[row_index];
  129. // Check row pin state
  130. // Use the otherwise unused row: 3, col: 0 for caps lock
  131. if (row_index == 2 && current_col == 2) {
  132. // Pin E2 uses active low
  133. if ((_SFR_IO8(E2 >> 4) & _BV(E2 & 0xF)) == 0)
  134. {
  135. // Pin LO, set col bit
  136. current_matrix[row_index] |= (COL_SHIFTER << current_col);
  137. }
  138. else
  139. {
  140. // Pin HI, clear col bit
  141. current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
  142. }
  143. }
  144. else {
  145. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)))
  146. {
  147. // Pin HI, set col bit
  148. current_matrix[row_index] |= (COL_SHIFTER << current_col);
  149. }
  150. else
  151. {
  152. // Pin LO, clear col bit
  153. current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
  154. }
  155. }
  156. // Determine if the matrix changed state
  157. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  158. {
  159. matrix_changed = true;
  160. }
  161. }
  162. // Unselect cols
  163. unselect_cols();
  164. return matrix_changed;
  165. }
  166. /* Row pin configuration
  167. * row: 0 1 2 3 4
  168. * pin: D0 D1 D2 D3 D5
  169. *
  170. * Caps lock uses its own pin E2
  171. */
  172. static void init_rows(void) {
  173. DDRD &= ~((1<<0)| (1<<1) | (1<<2) | (1<<3) | (1<<5)); // IN
  174. PORTD &= ~((1<<0)| (1<<1) | (1<<2) | (1<<3) | (1<<5)); // LO
  175. DDRE &= ~(1<<2); // IN
  176. PORTE |= (1<<2); // HI
  177. }
  178. /* Columns 0 - 16
  179. * col 0: B5
  180. * col 1: B6
  181. * These columns use a 74HC237D 3 to 8 bit demultiplexer.
  182. * A B C GL1
  183. * col / pin: PF0 PF1 PC7 PC6
  184. * 2: 0 0 0 1
  185. * 3: 1 0 0 1
  186. * 4: 0 1 0 1
  187. * 5: 1 1 0 1
  188. * 6: 0 0 1 1
  189. * 7: 1 0 1 1
  190. * 8: 0 1 1 1
  191. * 9: 1 1 1 1
  192. * col 10: E6
  193. * col 11: B0
  194. * col 12: B7
  195. * col 13: D4
  196. * col 14: D6
  197. * col 15: D7
  198. * col 16: B4
  199. */
  200. static void unselect_cols(void) {
  201. DDRB |= (1<<5) | (1<<6) | (1<<0) | (1<<7) | (1<<4); // OUT
  202. PORTB &= ~((1<<5) | (1<<6) | (1<<0) | (1<<7) | (1<<4)); // LO
  203. DDRD |= (1<<4) | (1<<6) | (1<<7); // OUT
  204. PORTD &= ~((1<<4) | (1<<6) | (1<<7)); // LO
  205. DDRE |= (1<<6); // OUT
  206. PORTE &= ~((1<<6)); // LO
  207. DDRF |= (1<<0) | (1<<1); // OUT
  208. PORTF &= ~((1<<0) | (1<<1)); // LO
  209. DDRC |= (1<<7) | (1<<6); // OUT
  210. PORTC &= ~((1<<7) | (1<<6)); // LO
  211. }
  212. static void select_col(uint8_t col)
  213. {
  214. switch (col) {
  215. case 0:
  216. PORTB |= (1<<5); // HI
  217. break;
  218. case 1:
  219. PORTB |= (1<<6); // HI
  220. break;
  221. case 2:
  222. PORTC |= (1<<6); // HI
  223. break;
  224. case 3:
  225. PORTC |= (1<<6); // HI
  226. PORTF |= (1<<0); // HI
  227. break;
  228. case 4:
  229. PORTC |= (1<<6); // HI
  230. PORTF |= (1<<1); // HI
  231. break;
  232. case 5:
  233. PORTC |= (1<<6); // HI
  234. PORTF |= (1<<0); // HI
  235. PORTF |= (1<<1); // HI
  236. break;
  237. case 6:
  238. PORTC |= (1<<6); // HI
  239. PORTC |= (1<<7); // HI
  240. break;
  241. case 7:
  242. PORTC |= (1<<6); // HI
  243. PORTF |= (1<<0); // HI
  244. PORTC |= (1<<7); // HI
  245. break;
  246. case 8:
  247. PORTC |= (1<<6); // HI
  248. PORTF |= (1<<1); // HI
  249. PORTC |= (1<<7); // HI
  250. break;
  251. case 9:
  252. PORTC |= (1<<6); // HI
  253. PORTF |= (1<<0); // HI
  254. PORTF |= (1<<1); // HI
  255. PORTC |= (1<<7); // HI
  256. break;
  257. case 10:
  258. PORTE |= (1<<6); // HI
  259. break;
  260. case 11:
  261. PORTB |= (1<<0); // HI
  262. break;
  263. case 12:
  264. PORTB |= (1<<7); // HI
  265. break;
  266. case 13:
  267. PORTD |= (1<<4); // HI
  268. break;
  269. case 14:
  270. PORTD |= (1<<6); // HI
  271. break;
  272. case 15:
  273. PORTD |= (1<<7); // HI
  274. break;
  275. case 16:
  276. PORTB |= (1<<4); // HI
  277. break;
  278. }
  279. }