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 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. static matrix_row_t matrix[MATRIX_ROWS];
  27. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  28. static uint8_t read_rows(void);
  29. static void init_rows(void);
  30. static void unselect_cols(void);
  31. static void select_col(uint8_t col);
  32. inline uint8_t matrix_rows(void)
  33. {
  34. return MATRIX_ROWS;
  35. }
  36. inline uint8_t matrix_cols(void)
  37. {
  38. return MATRIX_COLS;
  39. }
  40. void matrix_init(void)
  41. {
  42. unselect_cols();
  43. init_rows();
  44. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  45. matrix[i] = 0;
  46. matrix_debouncing[i] = 0;
  47. }
  48. }
  49. uint8_t matrix_scan(void)
  50. {
  51. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  52. select_col(col);
  53. _delay_us(3);
  54. uint8_t rows = read_rows();
  55. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  56. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  57. bool curr_bit = rows & (1<<row);
  58. if (prev_bit != curr_bit) {
  59. matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
  60. debouncing = DEBOUNCE;
  61. }
  62. }
  63. unselect_cols();
  64. }
  65. if (debouncing) {
  66. if (--debouncing) {
  67. _delay_ms(1);
  68. } else {
  69. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  70. matrix[i] = matrix_debouncing[i];
  71. }
  72. }
  73. }
  74. return 1;
  75. }
  76. bool matrix_is_modified(void)
  77. {
  78. if (debouncing) return false;
  79. return true;
  80. }
  81. inline bool matrix_is_on(uint8_t row, uint8_t col)
  82. {
  83. return (matrix[row] & ((matrix_row_t)1<<col));
  84. }
  85. inline matrix_row_t matrix_get_row(uint8_t row)
  86. {
  87. return matrix[row];
  88. }
  89. void matrix_print(void)
  90. {
  91. print("\nr/c 0123456789ABCDEF\n");
  92. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  93. xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
  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. /* Row pin configuration
  105. *
  106. * row: 0 1 2 3 4 5 6 7
  107. * pin: PB1 PC2 PB6 PB4 PB3 PB5 PB0 PB2
  108. *
  109. */
  110. static void init_rows(void)
  111. {
  112. DDRC &= ~0b00000100;
  113. DDRB &= ~0b01111111;
  114. PORTC |= 0b00000100;
  115. PORTB |= 0b01111111;
  116. }
  117. static uint8_t read_rows(void)
  118. {
  119. return (PINB&(1<<1) ? 0 : (1<<0)) |
  120. (PINC&(1<<2) ? 0 : (1<<1)) |
  121. (PINB&(1<<6) ? 0 : (1<<2)) |
  122. (PINB&(1<<4) ? 0 : (1<<3)) |
  123. (PINB&(1<<3) ? 0 : (1<<4)) |
  124. (PINB&(1<<5) ? 0 : (1<<5)) |
  125. (PINB&(1<<0) ? 0 : (1<<6)) |
  126. (PINB&(1<<2) ? 0 : (1<<7));
  127. }
  128. /* These columns uses two 74HC42 4 to 10 bit demultiplexers (low active).
  129. *
  130. * COL PD6 PD5 PD4 PD3 PD2 PD1
  131. * 10 1 1 0 0 0 0
  132. * 15 1 1 0 0 0 1
  133. * 8 1 1 0 0 1 0
  134. * 14 1 1 0 1 0 0
  135. * 6 1 1 0 1 0 1
  136. * 13 1 1 0 1 1 0
  137. * 12 1 1 1 0 0 0
  138. * 9 1 1 1 0 1 0
  139. * 11 1 1 1 1 0 0
  140. * 7 1 1 1 1 1 0
  141. *
  142. * COL PD1 PD2 PD3 PD4 PD5 PD6
  143. * 3 1 1 0 0 0 1
  144. * 4 1 1 0 0 1 0
  145. * 17 1 1 0 1 0 0
  146. * 16 1 1 0 1 1 0
  147. * 0 1 1 1 0 0 1
  148. * 5 1 1 1 0 1 0
  149. * 2 1 1 1 1 0 0
  150. * 1 1 1 1 1 1 0
  151. */
  152. static void unselect_cols(void)
  153. {
  154. DDRD |= 0b01111111;
  155. PORTD &= ~0b01111111;
  156. }
  157. static void select_col(uint8_t col)
  158. {
  159. switch (col) {
  160. case 0:
  161. PORTD |= (1<<6) | (1<<3) | (1<<2) | (1<<1);
  162. break;
  163. case 1:
  164. PORTD |= (1<<5) | (1<<4) | (1<<3) | (1<<2) | (1<<1);
  165. break;
  166. case 2:
  167. PORTD |= (1<<4) | (1<<3) | (1<<2) | (1<<1);
  168. break;
  169. case 3:
  170. PORTD |= (1<<6) | (1<<2) | (1<<1);
  171. break;
  172. case 4:
  173. PORTD |= (1<<5) | (1<<2) | (1<<1);
  174. break;
  175. case 5:
  176. PORTD |= (1<<5) | (1<<3) | (1<<2) | (1<<1);
  177. break;
  178. case 6:
  179. PORTD |= (1<<6) | (1<<5) | (1<<3) | (1<<1);
  180. break;
  181. case 7:
  182. PORTD |= (1<<6) | (1<<5) | (1<<4) | (1<<3) | (1<<2);
  183. break;
  184. case 8:
  185. PORTD |= (1<<6) | (1<<5) | (1<<2);
  186. break;
  187. case 9:
  188. PORTD |= (1<<6) | (1<<5) | (1<<4) | (1<<2);
  189. break;
  190. case 10:
  191. PORTD |= (1<<6) | (1<<5);
  192. break;
  193. case 11:
  194. PORTD |= (1<<6) | (1<<5) | (1<<4) | (1<<3);
  195. break;
  196. case 12:
  197. PORTD |= (1<<6) | (1<<5) | (1<<4);
  198. break;
  199. case 13:
  200. PORTD |= (1<<6) | (1<<5) | (1<<3) | (1<<2);
  201. break;
  202. case 14:
  203. PORTD |= (1<<6) | (1<<5) | (1<<3);
  204. break;
  205. case 15:
  206. PORTD |= (1<<6) | (1<<5) | (1<<1);
  207. break;
  208. case 16:
  209. PORTD |= (1<<5) | (1<<4) | (1<<2) | (1<<1);
  210. break;
  211. case 17:
  212. PORTD |= (1<<4) | (1<<2) | (1<<1);
  213. break;
  214. }
  215. }