matrix.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. Copyright 2012 Jun Wako
  3. Copyright 2014 Jack Humbert
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <avr/io.h>
  18. #include "wait.h"
  19. #include "print.h"
  20. #include "debug.h"
  21. #include "util.h"
  22. #include "matrix.h"
  23. #ifdef MATRIX_HAS_GHOST
  24. # error "The universal matrix.c file cannot be used for this keyboard."
  25. #endif
  26. #ifndef DEBOUNCING_DELAY
  27. # define DEBOUNCING_DELAY 5
  28. #endif
  29. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  30. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  31. /* matrix state */
  32. #if DIODE_DIRECTION == COL2ROW
  33. static matrix_row_t matrix[MATRIX_ROWS];
  34. #else
  35. static matrix_col_t matrix[MATRIX_COLS];
  36. #endif
  37. static int8_t debouncing_delay = -1;
  38. #if DIODE_DIRECTION == COL2ROW
  39. static void toggle_row(uint8_t row);
  40. static matrix_row_t read_cols(void);
  41. #else
  42. static void toggle_col(uint8_t col);
  43. static matrix_col_t read_rows(void);
  44. #endif
  45. __attribute__ ((weak))
  46. void matrix_init_quantum(void) {
  47. matrix_init_kb();
  48. }
  49. __attribute__ ((weak))
  50. void matrix_scan_quantum(void) {
  51. matrix_scan_kb();
  52. }
  53. __attribute__ ((weak))
  54. void matrix_init_kb(void) {
  55. matrix_init_user();
  56. }
  57. __attribute__ ((weak))
  58. void matrix_scan_kb(void) {
  59. matrix_scan_user();
  60. }
  61. __attribute__ ((weak))
  62. void matrix_init_user(void) {
  63. }
  64. __attribute__ ((weak))
  65. void matrix_scan_user(void) {
  66. }
  67. uint8_t matrix_rows(void) {
  68. return MATRIX_ROWS;
  69. }
  70. uint8_t matrix_cols(void) {
  71. return MATRIX_COLS;
  72. }
  73. // void matrix_power_up(void) {
  74. // #if DIODE_DIRECTION == COL2ROW
  75. // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
  76. // /* DDRxn */
  77. // _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
  78. // toggle_row(r);
  79. // }
  80. // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
  81. // /* PORTxn */
  82. // _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
  83. // }
  84. // #else
  85. // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
  86. // /* DDRxn */
  87. // _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
  88. // toggle_col(c);
  89. // }
  90. // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
  91. // /* PORTxn */
  92. // _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
  93. // }
  94. // #endif
  95. // }
  96. void matrix_init(void) {
  97. /* frees PORTF by setting the JTD bit twice within four cycles */
  98. #ifdef __AVR_ATmega32U4__
  99. MCUCR |= _BV(JTD);
  100. MCUCR |= _BV(JTD);
  101. #endif
  102. /* initializes the I/O pins */
  103. #if DIODE_DIRECTION == COL2ROW
  104. for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
  105. /* DDRxn */
  106. _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
  107. toggle_row(r);
  108. }
  109. for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
  110. /* PORTxn */
  111. _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
  112. }
  113. #else
  114. for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
  115. /* DDRxn */
  116. _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
  117. toggle_col(c);
  118. }
  119. for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
  120. /* PORTxn */
  121. _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
  122. }
  123. #endif
  124. matrix_init_quantum();
  125. }
  126. #if DIODE_DIRECTION == COL2ROW
  127. uint8_t matrix_scan(void) {
  128. static matrix_row_t debouncing_matrix[MATRIX_ROWS];
  129. for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
  130. toggle_row(r);
  131. matrix_row_t state = read_cols();
  132. if (debouncing_matrix[r] != state) {
  133. debouncing_matrix[r] = state;
  134. debouncing_delay = DEBOUNCING_DELAY;
  135. }
  136. toggle_row(r);
  137. }
  138. if (debouncing_delay >= 0) {
  139. dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
  140. --debouncing_delay;
  141. if (debouncing_delay >= 0) {
  142. wait_ms(1);
  143. }
  144. else {
  145. for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
  146. matrix[r] = debouncing_matrix[r];
  147. }
  148. }
  149. }
  150. matrix_scan_quantum();
  151. return 1;
  152. }
  153. static void toggle_row(uint8_t row) {
  154. /* PINxn */
  155. _SFR_IO8((row_pins[row] >> 4)) = _BV(row_pins[row] & 0xF);
  156. }
  157. static matrix_row_t read_cols(void) {
  158. matrix_row_t state = 0;
  159. for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
  160. /* PINxn */
  161. if (!(_SFR_IO8((col_pins[c] >> 4)) & _BV(col_pins[c] & 0xF))) {
  162. state |= (matrix_row_t)1 << c;
  163. }
  164. }
  165. return state;
  166. }
  167. matrix_row_t matrix_get_row(uint8_t row) {
  168. return matrix[row];
  169. }
  170. #else
  171. uint8_t matrix_scan(void) {
  172. static matrix_col_t debouncing_matrix[MATRIX_COLS];
  173. for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
  174. toggle_col(c);
  175. matrix_col_t state = read_rows();
  176. if (debouncing_matrix[c] != state) {
  177. debouncing_matrix[c] = state;
  178. debouncing_delay = DEBOUNCING_DELAY;
  179. }
  180. toggle_col(c);
  181. }
  182. if (debouncing_delay >= 0) {
  183. dprintf("Debouncing delay remaining: %X\n", debouncing_delay);
  184. --debouncing_delay;
  185. if (debouncing_delay >= 0) {
  186. wait_ms(1);
  187. }
  188. else {
  189. for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
  190. matrix[c] = debouncing_matrix[c];
  191. }
  192. }
  193. }
  194. matrix_scan_quantum();
  195. return 1;
  196. }
  197. static void toggle_col(uint8_t col) {
  198. /* PINxn */
  199. _SFR_IO8((col_pins[col] >> 4)) = _BV(col_pins[col] & 0xF);
  200. }
  201. static matrix_col_t read_rows(void) {
  202. matrix_col_t state = 0;
  203. for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
  204. /* PINxn */
  205. if (!(_SFR_IO8((row_pins[r] >> 4)) & _BV(row_pins[r] & 0xF))) {
  206. state |= (matrix_col_t)1 << r;
  207. }
  208. }
  209. return state;
  210. }
  211. matrix_row_t matrix_get_row(uint8_t row) {
  212. matrix_row_t state = 0;
  213. matrix_col_t mask = (matrix_col_t)1 << row;
  214. for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
  215. if (matrix[c] & mask) {
  216. state |= (matrix_row_t)1 << c;
  217. }
  218. }
  219. return state;
  220. }
  221. #endif
  222. bool matrix_is_modified(void) {
  223. if (debouncing_delay >= 0) return false;
  224. return true;
  225. }
  226. bool matrix_is_on(uint8_t row, uint8_t col) {
  227. return matrix_get_row(row) & (matrix_row_t)1 << col;
  228. }
  229. void matrix_print(void) {
  230. dprintln("Human-readable matrix state:");
  231. for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
  232. dprintf("State of row %X: %016b\n", r, bitrev16(matrix_get_row(r)));
  233. }
  234. }
  235. uint8_t matrix_key_count(void) {
  236. uint8_t count = 0;
  237. for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
  238. count += bitpop16(matrix_get_row(r));
  239. }
  240. return count;
  241. }