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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * scan matrix
  3. */
  4. #include <avr/io.h>
  5. #include <util/delay.h>
  6. #include "keymap.h"
  7. #include "matrix.h"
  8. // matrix is active low. (key on: 0/key off: 1)
  9. // row: Hi-Z(unselected)/low output(selected)
  10. // PD0, PC7, PD7, PF6, PD6, PD1, PD2, PC6, PF7
  11. // col: input w/pullup
  12. // PB0-PB7
  13. // matrix state buffer
  14. uint8_t *matrix;
  15. uint8_t *matrix_prev;
  16. static uint8_t _matrix0[MATRIX_ROWS];
  17. static uint8_t _matrix1[MATRIX_ROWS];
  18. static uint8_t read_col(void);
  19. static void unselect_rows(void);
  20. static void select_row(uint8_t row);
  21. // this must be called once before matrix_scan.
  22. void matrix_init(void)
  23. {
  24. // initialize row and col
  25. unselect_rows();
  26. DDRB = 0x00;
  27. PORTB = 0xFF;
  28. // initialize matrix state: all keys off
  29. for (int i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0xFF;
  30. for (int i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0xFF;
  31. matrix = _matrix0;
  32. matrix_prev = _matrix1;
  33. }
  34. uint8_t matrix_scan(void)
  35. {
  36. uint8_t row, state;
  37. uint8_t *tmp;
  38. tmp = matrix_prev;
  39. matrix_prev = matrix;
  40. matrix = tmp;
  41. for (row = 0; row < MATRIX_ROWS; row++) {
  42. select_row(row);
  43. _delay_us(30); // without this wait read unstable value.
  44. state = read_col();
  45. unselect_rows();
  46. matrix[row] = state;
  47. }
  48. return 1;
  49. }
  50. bool matrix_is_modified(void) {
  51. for (int i=0; i <MATRIX_ROWS; i++) {
  52. if (matrix[i] != matrix_prev[i])
  53. return true;
  54. }
  55. return false;
  56. }
  57. bool matrix_has_ghost(void) {
  58. for (int i=0; i <MATRIX_ROWS; i++) {
  59. if (matrix_has_ghost_in_row(i))
  60. return true;
  61. }
  62. return false;
  63. }
  64. bool matrix_has_ghost_in_row(uint8_t row) {
  65. uint8_t state = ~matrix[row];
  66. // no ghost exists in case less than 2 keys on
  67. if (((state - 1) & state) == 0)
  68. return false;
  69. // ghost exists in case same state as other row
  70. for (int i=0; i < MATRIX_ROWS; i++) {
  71. if (i == row) continue;
  72. if ((~matrix[i] & state) == state) return true;
  73. }
  74. return false;
  75. }
  76. static uint8_t read_col(void)
  77. {
  78. return PINB;
  79. }
  80. static void unselect_rows(void) {
  81. DDRD = 0x00;
  82. PORTD = 0x00;
  83. DDRC = 0x00;
  84. PORTC = 0x00;
  85. DDRF = 0x00;
  86. PORTF = 0x00;
  87. }
  88. static void select_row(uint8_t row)
  89. {
  90. switch (row) {
  91. case 0:
  92. DDRD = (1<<0);
  93. PORTD = 0x00;
  94. DDRC = 0x00;
  95. PORTC = 0x00;
  96. DDRF = 0x00;
  97. PORTF = 0x00;
  98. break;
  99. case 1:
  100. DDRD = 0x00;
  101. PORTD = 0x00;
  102. DDRC = (1<<7);
  103. PORTC = 0x00;
  104. DDRF = 0x00;
  105. PORTF = 0x00;
  106. break;
  107. case 2:
  108. DDRD = (1<<7);
  109. PORTD = 0x00;
  110. DDRC = 0x00;
  111. PORTC = 0x00;
  112. DDRF = 0x00;
  113. PORTF = 0x00;
  114. break;
  115. case 3:
  116. DDRD = 0x00;
  117. PORTD = 0x00;
  118. DDRC = 0x00;
  119. PORTC = 0x00;
  120. DDRF = (1<<6);
  121. PORTF = 0x00;
  122. break;
  123. case 4:
  124. DDRD = (1<<6);
  125. PORTD = 0x00;
  126. DDRC = 0x00;
  127. PORTC = 0x00;
  128. DDRF = 0x00;
  129. PORTF = 0x00;
  130. break;
  131. case 5:
  132. DDRD = (1<<1);
  133. PORTD = 0x00;
  134. DDRC = 0x00;
  135. PORTC = 0x00;
  136. DDRF = 0x00;
  137. PORTF = 0x00;
  138. break;
  139. case 6:
  140. DDRD = (1<<2);
  141. PORTD = 0x00;
  142. DDRC = 0x00;
  143. PORTC = 0x00;
  144. DDRF = 0x00;
  145. PORTF = 0x00;
  146. break;
  147. case 7:
  148. DDRD = 0x00;
  149. PORTD = 0x00;
  150. DDRC = (1<<6);
  151. PORTC = 0x00;
  152. DDRF = 0x00;
  153. PORTF = 0x00;
  154. break;
  155. case 8:
  156. DDRD = 0x00;
  157. PORTD = 0x00;
  158. DDRC = 0x00;
  159. PORTC = 0x00;
  160. DDRF = (1<<7);
  161. PORTF = 0x00;
  162. break;
  163. }
  164. }