matrix.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  2. Copyright 2012-2017 Jun Wako, Jack Humbert
  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 <stdbool.h>
  16. #if defined(__AVR__)
  17. #include <avr/io.h>
  18. #endif
  19. #include "wait.h"
  20. #include "print.h"
  21. #include "debug.h"
  22. #include "util.h"
  23. #include "matrix.h"
  24. #include "timer.h"
  25. #if (MATRIX_COLS <= 8)
  26. # define print_matrix_header() print("\nr/c 01234567\n")
  27. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  28. # define matrix_bitpop(i) bitpop(matrix[i])
  29. # define ROW_SHIFTER ((uint8_t)1)
  30. #elif (MATRIX_COLS <= 16)
  31. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  32. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  33. # define matrix_bitpop(i) bitpop16(matrix[i])
  34. # define ROW_SHIFTER ((uint16_t)1)
  35. #elif (MATRIX_COLS <= 32)
  36. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  37. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  38. # define matrix_bitpop(i) bitpop32(matrix[i])
  39. # define ROW_SHIFTER ((uint32_t)1)
  40. #endif
  41. #ifdef MATRIX_MASKED
  42. extern const matrix_row_t matrix_mask[];
  43. #endif
  44. #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  45. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  46. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  47. #endif
  48. /* matrix state(1:on, 0:off) */
  49. static matrix_row_t matrix[MATRIX_ROWS];
  50. #if (DIODE_DIRECTION == COL2ROW)
  51. static void init_cols(void);
  52. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  53. static void unselect_rows(void);
  54. static void select_row(uint8_t row);
  55. static void unselect_row(uint8_t row);
  56. #elif (DIODE_DIRECTION == ROW2COL)
  57. static void init_rows(void);
  58. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  59. static void unselect_cols(void);
  60. static void unselect_col(uint8_t col);
  61. static void select_col(uint8_t col);
  62. #endif
  63. __attribute__ ((weak))
  64. void matrix_init_quantum(void) {
  65. matrix_init_kb();
  66. }
  67. __attribute__ ((weak))
  68. void matrix_scan_quantum(void) {
  69. matrix_scan_kb();
  70. }
  71. __attribute__ ((weak))
  72. void matrix_init_kb(void) {
  73. matrix_init_user();
  74. }
  75. __attribute__ ((weak))
  76. void matrix_scan_kb(void) {
  77. matrix_scan_user();
  78. }
  79. __attribute__ ((weak))
  80. void matrix_init_user(void) {
  81. }
  82. __attribute__ ((weak))
  83. void matrix_scan_user(void) {
  84. }
  85. inline
  86. uint8_t matrix_rows(void) {
  87. return MATRIX_ROWS;
  88. }
  89. inline
  90. uint8_t matrix_cols(void) {
  91. return MATRIX_COLS;
  92. }
  93. void matrix_init(void) {
  94. // initialize row and col
  95. #if (DIODE_DIRECTION == COL2ROW)
  96. unselect_rows();
  97. init_cols();
  98. #elif (DIODE_DIRECTION == ROW2COL)
  99. unselect_cols();
  100. init_rows();
  101. #endif
  102. // initialize matrix state: all keys off
  103. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  104. matrix[i] = 0;
  105. }
  106. matrix_init_quantum();
  107. }
  108. uint8_t matrix_scan(void)
  109. {
  110. #if (DIODE_DIRECTION == COL2ROW)
  111. // Set row, read cols
  112. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  113. read_cols_on_row(matrix, current_row);
  114. }
  115. #elif (DIODE_DIRECTION == ROW2COL)
  116. // Set col, read rows
  117. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  118. read_rows_on_col(matrix, current_col);
  119. }
  120. #endif
  121. matrix_scan_quantum();
  122. return 1;
  123. }
  124. //Deprecated.
  125. bool matrix_is_modified(void)
  126. {
  127. return true;
  128. }
  129. inline
  130. bool matrix_is_on(uint8_t row, uint8_t col)
  131. {
  132. return (matrix[row] & ((matrix_row_t)1<col));
  133. }
  134. inline
  135. matrix_row_t matrix_get_row(uint8_t row)
  136. {
  137. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  138. // switch blocker installed and the switch is always pressed.
  139. #ifdef MATRIX_MASKED
  140. return matrix[row] & matrix_mask[row];
  141. #else
  142. return matrix[row];
  143. #endif
  144. }
  145. void matrix_print(void)
  146. {
  147. print_matrix_header();
  148. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  149. phex(row); print(": ");
  150. print_matrix_row(row);
  151. print("\n");
  152. }
  153. }
  154. uint8_t matrix_key_count(void)
  155. {
  156. uint8_t count = 0;
  157. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  158. count += matrix_bitpop(i);
  159. }
  160. return count;
  161. }
  162. #if (DIODE_DIRECTION == COL2ROW)
  163. static void init_cols(void)
  164. {
  165. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  166. uint8_t pin = col_pins[x];
  167. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  168. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  169. }
  170. }
  171. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  172. {
  173. // Store last value of row prior to reading
  174. matrix_row_t last_row_value = current_matrix[current_row];
  175. // Clear data in matrix row
  176. current_matrix[current_row] = 0;
  177. // Select row and wait for row selecton to stabilize
  178. select_row(current_row);
  179. wait_us(30);
  180. // For each col...
  181. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  182. // Select the col pin to read (active low)
  183. uint8_t pin = col_pins[col_index];
  184. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  185. // Populate the matrix row with the state of the col pin
  186. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  187. }
  188. // Unselect row
  189. unselect_row(current_row);
  190. return (last_row_value != current_matrix[current_row]);
  191. }
  192. static void select_row(uint8_t row)
  193. {
  194. uint8_t pin = row_pins[row];
  195. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  196. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  197. }
  198. static void unselect_row(uint8_t row)
  199. {
  200. uint8_t pin = row_pins[row];
  201. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  202. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  203. }
  204. static void unselect_rows(void)
  205. {
  206. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  207. uint8_t pin = row_pins[x];
  208. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  209. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  210. }
  211. }
  212. #elif (DIODE_DIRECTION == ROW2COL)
  213. static void init_rows(void)
  214. {
  215. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  216. uint8_t pin = row_pins[x];
  217. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  218. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  219. }
  220. }
  221. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  222. {
  223. bool matrix_changed = false;
  224. // Select col and wait for col selecton to stabilize
  225. select_col(current_col);
  226. wait_us(30);
  227. // For each row...
  228. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  229. {
  230. // Store last value of row prior to reading
  231. matrix_row_t last_row_value = current_matrix[row_index];
  232. // Check row pin state
  233. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  234. {
  235. // Pin LO, set col bit
  236. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  237. }
  238. else
  239. {
  240. // Pin HI, clear col bit
  241. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  242. }
  243. // Determine if the matrix changed state
  244. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  245. {
  246. matrix_changed = true;
  247. }
  248. }
  249. // Unselect col
  250. unselect_col(current_col);
  251. return matrix_changed;
  252. }
  253. static void select_col(uint8_t col)
  254. {
  255. uint8_t pin = col_pins[col];
  256. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  257. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  258. }
  259. static void unselect_col(uint8_t col)
  260. {
  261. uint8_t pin = col_pins[col];
  262. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  263. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  264. }
  265. static void unselect_cols(void)
  266. {
  267. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  268. uint8_t pin = col_pins[x];
  269. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  270. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  271. }
  272. }
  273. #endif