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

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