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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

matrix.c 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. Copyright 2013 Mathias Andersson <[email protected]>
  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. #include <avr/io.h>
  17. #include <util/delay.h>
  18. #include "print.h"
  19. #include "debug.h"
  20. #include "util.h"
  21. #include "matrix.h"
  22. #ifndef DEBOUNCE
  23. # define DEBOUNCE 0
  24. #endif
  25. static uint8_t debouncing = DEBOUNCE;
  26. /* matrix state(1:on, 0:off) */
  27. static matrix_row_t matrix[MATRIX_ROWS];
  28. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  29. static uint8_t read_rows(void);
  30. static uint8_t read_caps(void);
  31. static void init_rows(void);
  32. static void unselect_cols(void);
  33. static void select_col(uint8_t col);
  34. inline
  35. uint8_t matrix_rows(void)
  36. {
  37. return MATRIX_ROWS;
  38. }
  39. inline
  40. uint8_t matrix_cols(void)
  41. {
  42. return MATRIX_COLS;
  43. }
  44. void matrix_init(void)
  45. {
  46. unselect_cols();
  47. init_rows();
  48. // initialize matrix state: all keys off
  49. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  50. matrix[i] = 0;
  51. matrix_debouncing[i] = 0;
  52. }
  53. }
  54. uint8_t matrix_scan(void)
  55. {
  56. for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-16
  57. select_col(col);
  58. _delay_us(3); // TODO: Determine the correct value needed here.
  59. uint8_t rows = read_rows();
  60. // Use the otherwise unused col: 0 row: 3 for caps lock.
  61. if(col == 0) {
  62. rows |= read_caps();
  63. }
  64. for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-5
  65. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  66. bool curr_bit = rows & (1<<row);
  67. if (prev_bit != curr_bit) {
  68. matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
  69. if (debouncing) {
  70. dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
  71. }
  72. debouncing = DEBOUNCE;
  73. }
  74. }
  75. unselect_cols();
  76. }
  77. if (debouncing) {
  78. if (--debouncing) {
  79. _delay_ms(1);
  80. } else {
  81. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  82. matrix[i] = matrix_debouncing[i];
  83. }
  84. }
  85. }
  86. return 1;
  87. }
  88. bool matrix_is_modified(void)
  89. {
  90. if (debouncing) return false;
  91. return true;
  92. }
  93. inline
  94. bool matrix_is_on(uint8_t row, uint8_t col)
  95. {
  96. return (matrix[row] & ((matrix_row_t)1<<col));
  97. }
  98. inline
  99. matrix_row_t matrix_get_row(uint8_t row)
  100. {
  101. return matrix[row];
  102. }
  103. void matrix_print(void)
  104. {
  105. print("\nr/c 0123456789ABCDEF\n");
  106. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  107. xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
  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. /* Row pin configuration
  119. * row: 0 1 2 3 4 5
  120. * pin: D0 D1 D2 D3 D5 B7
  121. *
  122. * Caps lock uses its own pin PE2
  123. */
  124. static void init_rows(void)
  125. {
  126. // Input (DDR:0, PORT:0)
  127. DDRD &= ~0b00101111;
  128. PORTD &= ~0b00101111;
  129. DDRB &= ~(1<<7);
  130. PORTB &= ~(1<<7);
  131. // Input with pull-up (DDR:0, PORT:1)
  132. DDRE &= ~(1<<2);
  133. PORTE |= (1<<2);
  134. }
  135. static uint8_t read_rows(void)
  136. {
  137. return (PIND&(1<<0) ? (1<<0) : 0) |
  138. (PIND&(1<<1) ? (1<<1) : 0) |
  139. (PIND&(1<<2) ? (1<<2) : 0) |
  140. (PIND&(1<<3) ? (1<<3) : 0) |
  141. (PIND&(1<<5) ? (1<<4) : 0) |
  142. (PINB&(1<<7) ? (1<<5) : 0);
  143. }
  144. static uint8_t read_caps(void)
  145. {
  146. return PINE&(1<<2) ? 0 : (1<<3);
  147. }
  148. /* Columns 0 - 15
  149. * These columns uses two 74HC237D 3 to 8 bit demultiplexers.
  150. * col / pin: PC6 PB6 PF0 PF1 PC7
  151. * 0: 1 0 0 0 0
  152. * 1: 1 0 1 0 0
  153. * 2: 1 0 0 1 0
  154. * 3: 1 0 1 1 0
  155. * 4: 1 0 0 0 1
  156. * 5: 1 0 1 0 1
  157. * 6: 1 0 0 1 1
  158. * 7: 1 0 1 1 1
  159. * 8: 0 1 0 0 0
  160. * 9: 0 1 1 0 0
  161. * 10: 0 1 0 1 0
  162. * 11: 0 1 1 1 0
  163. * 12: 0 1 0 0 1
  164. * 13: 0 1 1 0 1
  165. * 14: 0 1 0 1 1
  166. * 15: 0 1 1 1 1
  167. *
  168. * col: 16
  169. * pin: PB5
  170. */
  171. static void unselect_cols(void)
  172. {
  173. DDRB |= (1<<5) | (1<<6);
  174. PORTB &= ~((1<<5) | (1<<6));
  175. DDRC |= (1<<6) | (1<<7);
  176. PORTC &= ~((1<<6) | (1<<7));
  177. DDRF |= (1<<0) | (1<<1);
  178. PORTF &= ~((1<<0) | (1<<1));
  179. }
  180. static void select_col(uint8_t col)
  181. {
  182. // Output high (DDR:1, PORT:1) to select
  183. switch (col) {
  184. case 0:
  185. PORTC |= (1<<6);
  186. break;
  187. case 1:
  188. PORTC |= (1<<6);
  189. PORTF |= (1<<0);
  190. break;
  191. case 2:
  192. PORTC |= (1<<6);
  193. PORTF |= (1<<1);
  194. break;
  195. case 3:
  196. PORTC |= (1<<6);
  197. PORTF |= (1<<0) | (1<<1);
  198. break;
  199. case 4:
  200. PORTC |= (1<<6);
  201. PORTC |= (1<<7);
  202. break;
  203. case 5:
  204. PORTC |= (1<<6);
  205. PORTF |= (1<<0);
  206. PORTC |= (1<<7);
  207. break;
  208. case 6:
  209. PORTC |= (1<<6);
  210. PORTF |= (1<<1);
  211. PORTC |= (1<<7);
  212. break;
  213. case 7:
  214. PORTC |= (1<<6);
  215. PORTF |= (1<<0) | (1<<1);
  216. PORTC |= (1<<7);
  217. break;
  218. case 8:
  219. PORTB |= (1<<6);
  220. break;
  221. case 9:
  222. PORTB |= (1<<6);
  223. PORTF |= (1<<0);
  224. break;
  225. case 10:
  226. PORTB |= (1<<6);
  227. PORTF |= (1<<1);
  228. break;
  229. case 11:
  230. PORTB |= (1<<6);
  231. PORTF |= (1<<0) | (1<<1);
  232. break;
  233. case 12:
  234. PORTB |= (1<<6);
  235. PORTC |= (1<<7);
  236. break;
  237. case 13:
  238. PORTB |= (1<<6);
  239. PORTF |= (1<<0);
  240. PORTC |= (1<<7);
  241. break;
  242. case 14:
  243. PORTB |= (1<<6);
  244. PORTF |= (1<<1);
  245. PORTC |= (1<<7);
  246. break;
  247. case 15:
  248. PORTB |= (1<<6);
  249. PORTF |= (1<<0) | (1<<1);
  250. PORTC |= (1<<7);
  251. break;
  252. case 16:
  253. PORTB |= (1<<5);
  254. break;
  255. }
  256. }