Keyboard firmwares for Atmel AVR and Cortex-M
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.

matrix.c 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. Copyright 2011 Jun Wako <[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. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <avr/io.h>
  20. #include <util/delay.h>
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #if (MATRIX_COLS > 16)
  26. # error "MATRIX_COLS must not exceed 16"
  27. #endif
  28. #if (MATRIX_ROWS > 255)
  29. # error "MATRIX_ROWS must not exceed 255"
  30. #endif
  31. #ifndef DEBOUNCE
  32. # define DEBOUNCE 5
  33. #endif
  34. static uint8_t debouncing = DEBOUNCE;
  35. // matrix state buffer(1:on, 0:off)
  36. static matrix_row_t *matrix;
  37. static matrix_row_t *matrix_debouncing;
  38. static matrix_row_t matrix0[MATRIX_ROWS];
  39. static matrix_row_t matrix1[MATRIX_ROWS];
  40. #ifdef MATRIX_HAS_GHOST
  41. static bool matrix_has_ghost_in_row(uint8_t row);
  42. #endif
  43. static matrix_row_t read_col(void);
  44. static void unselect_rows(void);
  45. static void select_row(uint8_t row);
  46. inline
  47. uint8_t matrix_rows(void)
  48. {
  49. return MATRIX_ROWS;
  50. }
  51. inline
  52. uint8_t matrix_cols(void)
  53. {
  54. return MATRIX_COLS;
  55. }
  56. void matrix_init(void)
  57. {
  58. // initialize row and col
  59. unselect_rows();
  60. // Input with pull-up(DDR:0, PORT:1)
  61. DDRB = 0x00;
  62. PORTB = 0xFF;
  63. // initialize matrix state: all keys off
  64. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  65. matrix0[i] = 0;
  66. matrix1[i] = 0;
  67. }
  68. matrix = matrix0;
  69. matrix_debouncing = matrix1;
  70. }
  71. uint8_t matrix_scan(void)
  72. {
  73. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  74. unselect_rows();
  75. select_row(i);
  76. _delay_us(30); // without this wait read unstable value.
  77. if (matrix[i] != read_col()) {
  78. matrix[i] = read_col();
  79. if (debouncing) {
  80. debug("bounce!: "); debug_hex(debouncing); print("\n");
  81. }
  82. debouncing = DEBOUNCE;
  83. }
  84. }
  85. unselect_rows();
  86. if (debouncing) {
  87. if (--debouncing) {
  88. _delay_ms(1);
  89. } else {
  90. matrix_row_t *tmp = matrix;
  91. matrix = matrix_debouncing;
  92. matrix_debouncing = tmp;
  93. }
  94. }
  95. return 1;
  96. }
  97. bool matrix_is_modified(void)
  98. {
  99. if (debouncing) return false;
  100. return true;
  101. }
  102. inline
  103. bool matrix_is_on(uint8_t row, uint8_t col)
  104. {
  105. return (matrix[row] & (1<<col));
  106. }
  107. inline
  108. matrix_row_t matrix_get_row(uint8_t row)
  109. {
  110. return matrix[row];
  111. }
  112. void matrix_print(void)
  113. {
  114. print("\nr/c 01234567\n");
  115. for (uint8_t row = 0; row < matrix_rows(); row++) {
  116. phex(row); print(": ");
  117. #if (MATRIX_COLS <= 8)
  118. pbin_reverse(matrix_get_row(row));
  119. #else
  120. pbin_reverse16(matrix_get_row(row));
  121. #endif
  122. #ifdef MATRIX_HAS_GHOST
  123. if (matrix_has_ghost_in_row(row)) {
  124. print(" <ghost");
  125. }
  126. #endif
  127. print("\n");
  128. }
  129. }
  130. uint8_t matrix_key_count(void)
  131. {
  132. uint8_t count = 0;
  133. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  134. #if (MATRIX_COLS <= 8)
  135. count += bitpop(matrix[i]);
  136. #else
  137. count += bitpop16(matrix[i]);
  138. #endif
  139. }
  140. return count;
  141. }
  142. #ifdef MATRIX_HAS_GHOST
  143. inline
  144. static bool matrix_has_ghost_in_row(uint8_t row)
  145. {
  146. // no ghost exists in case less than 2 keys on
  147. if (((matrix[row] - 1) & matrix[row]) == 0)
  148. return false;
  149. // ghost exists in case same state as other row
  150. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  151. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  152. return true;
  153. }
  154. return false;
  155. }
  156. #endif
  157. inline
  158. static matrix_row_t read_col(void)
  159. {
  160. return ~PINB;
  161. }
  162. inline
  163. static void unselect_rows(void)
  164. {
  165. // Hi-Z(DDR:0, PORT:0) to unselect
  166. DDRC &= ~0b01000000; // PC: 6
  167. PORTC &= ~0b01000000;
  168. DDRD &= ~0b11100111; // PD: 7,6,5,2,1,0
  169. PORTD &= ~0b11100111;
  170. DDRF &= ~0b11000000; // PF: 7,6
  171. PORTF &= ~0b11000000;
  172. }
  173. inline
  174. static void select_row(uint8_t row)
  175. {
  176. // Output low(DDR:1, PORT:0) to select
  177. // row: 0 1 2 3 4 5 6 7 8
  178. // pin: PD0, PD5, PD7, PF6, PD6, PD1, PD2, PC6, PF7
  179. switch (row) {
  180. case 0:
  181. DDRD |= (1<<0);
  182. PORTD &= ~(1<<0);
  183. break;
  184. case 1:
  185. DDRD |= (1<<5);
  186. PORTD &= ~(1<<5);
  187. break;
  188. case 2:
  189. DDRD |= (1<<7);
  190. PORTD &= ~(1<<7);
  191. break;
  192. case 3:
  193. DDRF |= (1<<6);
  194. PORTF &= ~(1<<6);
  195. break;
  196. case 4:
  197. DDRD |= (1<<6);
  198. PORTD &= ~(1<<6);
  199. break;
  200. case 5:
  201. DDRD |= (1<<1);
  202. PORTD &= ~(1<<1);
  203. break;
  204. case 6:
  205. DDRD |= (1<<2);
  206. PORTD &= ~(1<<2);
  207. break;
  208. case 7:
  209. DDRC |= (1<<6);
  210. PORTC &= ~(1<<6);
  211. break;
  212. case 8:
  213. DDRF |= (1<<7);
  214. PORTF &= ~(1<<7);
  215. break;
  216. }
  217. }