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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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: PC2 PB1 PB2 PB3 PC7 PB4 PB5 PB6
  108. *
  109. */
  110. static void init_rows(void)
  111. {
  112. DDRC &= ~0b10000100;
  113. DDRB &= ~0b01111110;
  114. PORTC |= 0b10000100;
  115. PORTB |= 0b01111110;
  116. }
  117. static uint8_t read_rows(void)
  118. {
  119. return (PINC&(1<<2) ? 0 : (1<<0)) |
  120. (PINB&(1<<1) ? 0 : (1<<1)) |
  121. (PINB&(1<<2) ? 0 : (1<<2)) |
  122. (PINB&(1<<3) ? 0 : (1<<3)) |
  123. (PINC&(1<<7) ? 0 : (1<<4)) |
  124. (PINB&(1<<4) ? 0 : (1<<5)) |
  125. (PINB&(1<<5) ? 0 : (1<<6)) |
  126. (PINB&(1<<6) ? 0 : (1<<7));
  127. }
  128. /* These columns uses two 74HC42 4 to 10 bit demultiplexers (low active).
  129. *
  130. * COL PD1 PD0 PD2 PD6 PD5 PD4
  131. * 12 1 1 0 0 0 0
  132. * 11 1 1 0 0 0 1
  133. * 10 1 1 0 0 1 0
  134. * 9 1 1 0 0 1 1
  135. * 8 1 1 0 1 0 0
  136. * 7 1 1 0 1 0 1
  137. * 6 1 1 0 1 1 0
  138. * 5 1 1 0 1 1 1
  139. * 4 1 1 1 0 0 0
  140. * 3 1 1 1 0 0 1
  141. * COL PD2 PD6 PD1 PD0 PD5 PD4
  142. * 2 1 1 0 0 0 0
  143. * 1 1 1 0 0 0 1
  144. * 0 1 1 0 0 1 0
  145. * 17 1 1 0 0 1 1
  146. * 16 1 1 0 1 0 0
  147. * 1 1 0 1 0 1
  148. * 1 1 0 1 1 0
  149. * 15 1 1 0 1 1 1
  150. * 14 1 1 1 0 0 0
  151. * 13 1 1 1 0 0 1
  152. */
  153. static void unselect_cols(void)
  154. {
  155. DDRD |= 0b01110111;
  156. PORTD &= ~0b01110111;
  157. }
  158. static void select_col(uint8_t col)
  159. {
  160. switch (col) {
  161. case 0:
  162. PORTD |= (1<<5) | (1<<6) | (1<<2);
  163. break;
  164. case 1:
  165. PORTD |= (1<<4) | (1<<6) | (1<<2);
  166. break;
  167. case 2:
  168. PORTD |= (1<<6) | (1<<2);
  169. break;
  170. case 3:
  171. PORTD |= (1<<4) | (1<<2) | (1<<0) | (1<<1);
  172. break;
  173. case 4:
  174. PORTD |= (1<<2) | (1<<0) | (1<<1);
  175. break;
  176. case 5:
  177. PORTD |= (1<<4) | (1<<5) | (1<<6) | (1<<0) | (1<<1);
  178. break;
  179. case 6:
  180. PORTD |= (1<<5) | (1<<6) | (1<<0) | (1<<1);
  181. break;
  182. case 7:
  183. PORTD |= (1<<4) | (1<<6) | (1<<0) | (1<<1);
  184. break;
  185. case 8:
  186. PORTD |= (1<<6) | (1<<0) | (1<<1);
  187. break;
  188. case 9:
  189. PORTD |= (1<<4) | (1<<5) | (1<<0) | (1<<1);
  190. break;
  191. case 10:
  192. PORTD |= (1<<5) | (1<<0) | (1<<1);
  193. break;
  194. case 11:
  195. PORTD |= (1<<4) | (1<<0) | (1<<1);
  196. break;
  197. case 12:
  198. PORTD |= (1<<0) | (1<<1);
  199. break;
  200. case 13:
  201. PORTD |= (1<<4) | (1<<1) | (1<<6) | (1<<2);
  202. break;
  203. case 14:
  204. PORTD |= (1<<1) | (1<<6) | (1<<2);
  205. break;
  206. case 15:
  207. PORTD |= (1<<4) | (1<<5) | (1<<0) | (1<<6) | (1<<2);
  208. break;
  209. case 16:
  210. PORTD |= (1<<0) | (1<<6) | (1<<2);
  211. break;
  212. case 17:
  213. PORTD |= (1<<4) | (1<<5) | (1<<6) | (1<<2);
  214. break;
  215. }
  216. }