matrix.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /*
  2. Copyright 2017 Mathias Andersson <wraul@dbox.se>
  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. /* Set 0 if debouncing isn't needed */
  26. #ifndef DEBOUNCING_DELAY
  27. # define DEBOUNCING_DELAY 5
  28. #endif
  29. #define COL_SHIFTER ((uint32_t)1)
  30. static uint16_t debouncing_time;
  31. static bool debouncing = false;
  32. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  33. /* matrix state(1:on, 0:off) */
  34. static matrix_row_t matrix[MATRIX_ROWS];
  35. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  36. static void init_rows(void);
  37. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  38. static void unselect_cols(void);
  39. static void select_col(uint8_t col);
  40. inline
  41. uint8_t matrix_rows(void) {
  42. return MATRIX_ROWS;
  43. }
  44. inline
  45. uint8_t matrix_cols(void) {
  46. return MATRIX_COLS;
  47. }
  48. void matrix_init(void) {
  49. unselect_cols();
  50. init_rows();
  51. // initialize matrix state: all keys off
  52. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  53. matrix[i] = 0;
  54. matrix_debouncing[i] = 0;
  55. }
  56. matrix_init_quantum();
  57. }
  58. uint8_t matrix_scan(void)
  59. {
  60. // Set col, read rows
  61. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  62. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  63. if (matrix_changed) {
  64. debouncing = true;
  65. debouncing_time = timer_read();
  66. }
  67. }
  68. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
  69. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  70. matrix[i] = matrix_debouncing[i];
  71. }
  72. debouncing = false;
  73. }
  74. matrix_scan_quantum();
  75. return 1;
  76. }
  77. inline
  78. bool matrix_is_on(uint8_t row, uint8_t col)
  79. {
  80. return (matrix[row] & ((matrix_row_t)1<col));
  81. }
  82. inline
  83. matrix_row_t matrix_get_row(uint8_t row)
  84. {
  85. return matrix[row];
  86. }
  87. void matrix_print(void)
  88. {
  89. print("\nr/c 0123456789ABCDEFGHIJKLMNOPQRSTUV\n");
  90. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  91. phex(row); print(": ");
  92. print_bin_reverse32(matrix_get_row(row));
  93. print("\n");
  94. }
  95. }
  96. uint8_t matrix_key_count(void)
  97. {
  98. uint8_t count = 0;
  99. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  100. count += bitpop32(matrix[i]);
  101. }
  102. return count;
  103. }
  104. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  105. {
  106. bool matrix_changed = false;
  107. // Select col and wait for col selecton to stabilize
  108. select_col(current_col);
  109. wait_us(30);
  110. // For each row...
  111. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  112. {
  113. // Store last value of row prior to reading
  114. matrix_row_t last_row_value = current_matrix[row_index];
  115. // Check row pin state
  116. // Use the otherwise unused row: 3, col: 0 for caps lock
  117. if (row_index == 3 && current_col == 0) {
  118. // Pin E2 uses active low
  119. if ((_SFR_IO8(E2 >> 4) & _BV(E2 & 0xF)) == 0)
  120. {
  121. // Pin LO, set col bit
  122. current_matrix[row_index] |= (COL_SHIFTER << current_col);
  123. }
  124. else
  125. {
  126. // Pin HI, clear col bit
  127. current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
  128. }
  129. }
  130. else {
  131. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)))
  132. {
  133. // Pin HI, set col bit
  134. current_matrix[row_index] |= (COL_SHIFTER << current_col);
  135. }
  136. else
  137. {
  138. // Pin LO, clear col bit
  139. current_matrix[row_index] &= ~(COL_SHIFTER << current_col);
  140. }
  141. }
  142. // Determine if the matrix changed state
  143. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  144. {
  145. matrix_changed = true;
  146. }
  147. }
  148. // Unselect cols
  149. unselect_cols();
  150. return matrix_changed;
  151. }
  152. /* Row pin configuration
  153. * row: 0 1 2 3 4 5
  154. * pin: D0 D1 D2 D3 D5 B7
  155. *
  156. * Caps lock uses its own pin E2
  157. */
  158. static void init_rows(void)
  159. {
  160. DDRD &= ~((1<<0)| (1<<1) | (1<<2) | (1<<3) | (1<<5)); // IN
  161. PORTD &= ~((1<<0)| (1<<1) | (1<<2) | (1<<3) | (1<<5)); // LO
  162. DDRB &= ~(1<<7); // IN
  163. PORTB &= ~(1<<7); // LO
  164. DDRE &= ~(1<<2); // IN
  165. PORTE |= (1<<2); // HI
  166. }
  167. /* Columns 0 - 15
  168. * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
  169. * col / pin: PC6 PB6 PF0 PF1 PC7
  170. * 0: 1 0 0 0 0
  171. * 1: 1 0 1 0 0
  172. * 2: 1 0 0 1 0
  173. * 3: 1 0 1 1 0
  174. * 4: 1 0 0 0 1
  175. * 5: 1 0 1 0 1
  176. * 6: 1 0 0 1 1
  177. * 7: 1 0 1 1 1
  178. * 8: 0 1 0 0 0
  179. * 9: 0 1 1 0 0
  180. * 10: 0 1 0 1 0
  181. * 11: 0 1 1 1 0
  182. * 12: 0 1 0 0 1
  183. * 13: 0 1 1 0 1
  184. * 14: 0 1 0 1 1
  185. * 15: 0 1 1 1 1
  186. *
  187. * col: 16
  188. * pin: PB5
  189. */
  190. static void unselect_cols(void)
  191. {
  192. DDRB |= (1<<5) | (1<<6); // OUT
  193. PORTB &= ~((1<<5) | (1<<6)); // LO
  194. DDRC |= (1<<6) | (1<<7); // OUT
  195. PORTC &= ~((1<<6) | (1<<7)); // LO
  196. DDRF |= (1<<0) | (1<<1); // OUT
  197. PORTF &= ~((1<<0) | (1<<1)); // LO
  198. }
  199. static void select_col(uint8_t col)
  200. {
  201. switch (col) {
  202. case 0:
  203. PORTC |= (1<<6); // HI
  204. break;
  205. case 1:
  206. PORTC |= (1<<6); // HI
  207. PORTF |= (1<<0); // HI
  208. break;
  209. case 2:
  210. PORTC |= (1<<6); // HI
  211. PORTF |= (1<<1); // HI
  212. break;
  213. case 3:
  214. PORTC |= (1<<6); // HI
  215. PORTF |= (1<<0) | (1<<1); // HI
  216. break;
  217. case 4:
  218. PORTC |= (1<<6); // HI
  219. PORTC |= (1<<7); // HI
  220. break;
  221. case 5:
  222. PORTC |= (1<<6); // HI
  223. PORTF |= (1<<0); // HI
  224. PORTC |= (1<<7); // HI
  225. break;
  226. case 6:
  227. PORTC |= (1<<6); // HI
  228. PORTF |= (1<<1); // HI
  229. PORTC |= (1<<7); // HI
  230. break;
  231. case 7:
  232. PORTC |= (1<<6); // HI
  233. PORTF |= (1<<0) | (1<<1); // HI
  234. PORTC |= (1<<7); // HI
  235. break;
  236. case 8:
  237. PORTB |= (1<<6); // HI
  238. break;
  239. case 9:
  240. PORTB |= (1<<6); // HI
  241. PORTF |= (1<<0); // HI
  242. break;
  243. case 10:
  244. PORTB |= (1<<6); // HI
  245. PORTF |= (1<<1); // HI
  246. break;
  247. case 11:
  248. PORTB |= (1<<6); // HI
  249. PORTF |= (1<<0) | (1<<1); // HI
  250. break;
  251. case 12:
  252. PORTB |= (1<<6); // HI
  253. PORTC |= (1<<7); // HI
  254. break;
  255. case 13:
  256. PORTB |= (1<<6); // HI
  257. PORTF |= (1<<0); // HI
  258. PORTC |= (1<<7); // HI
  259. break;
  260. case 14:
  261. PORTB |= (1<<6); // HI
  262. PORTF |= (1<<1); // HI
  263. PORTC |= (1<<7); // HI
  264. break;
  265. case 15:
  266. PORTB |= (1<<6); // HI
  267. PORTF |= (1<<0) | (1<<1); // HI
  268. PORTC |= (1<<7); // HI
  269. break;
  270. case 16:
  271. PORTB |= (1<<5); // HI
  272. break;
  273. }
  274. }