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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. Copyright 2014 Ralf Schmitt <[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_fwkey(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-17
  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: 12 row: 3 for firmware.
  61. if(col == 12) {
  62. rows |= read_fwkey();
  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: PD0 PD1 PD2 PD3 PD5 PB7
  121. *
  122. * Firmware 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_fwkey(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. * col: 17
  172. * pin: PD4
  173. */
  174. static void unselect_cols(void)
  175. {
  176. DDRB |= (1<<5) | (1<<6);
  177. PORTB &= ~((1<<5) | (1<<6));
  178. DDRC |= (1<<6) | (1<<7);
  179. PORTC &= ~((1<<6) | (1<<7));
  180. DDRD |= (1<<4);
  181. PORTD &= ~(1<<4);
  182. DDRF |= (1<<0) | (1<<1);
  183. PORTF &= ~((1<<0) | (1<<1));
  184. }
  185. static void select_col(uint8_t col)
  186. {
  187. // Output high (DDR:1, PORT:1) to select
  188. switch (col) {
  189. case 0:
  190. PORTC |= (1<<6);
  191. break;
  192. case 1:
  193. PORTC |= (1<<6);
  194. PORTF |= (1<<0);
  195. break;
  196. case 2:
  197. PORTC |= (1<<6);
  198. PORTF |= (1<<1);
  199. break;
  200. case 3:
  201. PORTC |= (1<<6);
  202. PORTF |= (1<<0) | (1<<1);
  203. break;
  204. case 4:
  205. PORTC |= (1<<6);
  206. PORTC |= (1<<7);
  207. break;
  208. case 5:
  209. PORTC |= (1<<6);
  210. PORTF |= (1<<0);
  211. PORTC |= (1<<7);
  212. break;
  213. case 6:
  214. PORTC |= (1<<6);
  215. PORTF |= (1<<1);
  216. PORTC |= (1<<7);
  217. break;
  218. case 7:
  219. PORTC |= (1<<6);
  220. PORTF |= (1<<0) | (1<<1);
  221. PORTC |= (1<<7);
  222. break;
  223. case 8:
  224. PORTB |= (1<<6);
  225. break;
  226. case 9:
  227. PORTB |= (1<<6);
  228. PORTF |= (1<<0);
  229. break;
  230. case 10:
  231. PORTB |= (1<<6);
  232. PORTF |= (1<<1);
  233. break;
  234. case 11:
  235. PORTB |= (1<<6);
  236. PORTF |= (1<<0) | (1<<1);
  237. break;
  238. case 12:
  239. PORTB |= (1<<6);
  240. PORTC |= (1<<7);
  241. break;
  242. case 13:
  243. PORTB |= (1<<6);
  244. PORTF |= (1<<0);
  245. PORTC |= (1<<7);
  246. break;
  247. case 14:
  248. PORTB |= (1<<6);
  249. PORTF |= (1<<1);
  250. PORTC |= (1<<7);
  251. break;
  252. case 15:
  253. PORTB |= (1<<6);
  254. PORTF |= (1<<0) | (1<<1);
  255. PORTC |= (1<<7);
  256. break;
  257. case 16:
  258. PORTB |= (1<<5);
  259. break;
  260. case 17:
  261. PORTD |= (1<<4);
  262. break;
  263. }
  264. }