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

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