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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. Copyright 2012 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. #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. /*
  23. * Happy Buckling Keyboard(IBM Model M mod)
  24. *
  25. * Pin usage:
  26. * COL: PD0-7
  27. * ROW: PB0-7, PF4-7
  28. */
  29. #ifndef DEBOUNCE
  30. # define DEBOUNCE 10
  31. #endif
  32. static uint8_t debouncing = DEBOUNCE;
  33. /* matrix state(1:on, 0:off) */
  34. static matrix_row_t matrix[MATRIX_ROWS];
  35. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  36. #ifdef MATRIX_HAS_GHOST
  37. static bool matrix_has_ghost_in_row(uint8_t row);
  38. #endif
  39. static matrix_row_t read_cols(void);
  40. static void unselect_rows(void);
  41. static void select_row(uint8_t row);
  42. inline
  43. uint8_t matrix_rows(void)
  44. {
  45. return MATRIX_ROWS;
  46. }
  47. inline
  48. uint8_t matrix_cols(void)
  49. {
  50. return MATRIX_COLS;
  51. }
  52. void matrix_init(void)
  53. {
  54. // JTAG disable for PORT F. write JTD bit twice within four cycles.
  55. MCUCR |= (1<<JTD);
  56. MCUCR |= (1<<JTD);
  57. // initialize rows
  58. unselect_rows();
  59. // initialize columns to input with pull-up(DDR:0, PORT:1)
  60. DDRD = 0x00;
  61. PORTD = 0xFF;
  62. // initialize matrix state: all keys off
  63. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  64. matrix[i] = 0;
  65. matrix_debouncing[i] = 0;
  66. }
  67. }
  68. uint8_t matrix_scan(void)
  69. {
  70. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  71. select_row(i);
  72. _delay_us(30); // without this wait read unstable value.
  73. matrix_row_t cols = read_cols();
  74. if (matrix_debouncing[i] != cols) {
  75. matrix_debouncing[i] = cols;
  76. if (debouncing) {
  77. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  78. }
  79. debouncing = DEBOUNCE;
  80. }
  81. unselect_rows();
  82. }
  83. if (debouncing) {
  84. if (--debouncing) {
  85. _delay_ms(1);
  86. } else {
  87. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  88. matrix[i] = matrix_debouncing[i];
  89. }
  90. }
  91. }
  92. return 1;
  93. }
  94. bool matrix_is_modified(void)
  95. {
  96. if (debouncing) return false;
  97. return true;
  98. }
  99. inline
  100. bool matrix_is_on(uint8_t row, uint8_t col)
  101. {
  102. return (matrix[row] & ((matrix_row_t)1<<col));
  103. }
  104. inline
  105. matrix_row_t matrix_get_row(uint8_t row)
  106. {
  107. return matrix[row];
  108. }
  109. void matrix_print(void)
  110. {
  111. print("\nr/c 01234567\n");
  112. for (uint8_t row = 0; row < matrix_rows(); row++) {
  113. phex(row); print(": ");
  114. pbin_reverse(matrix_get_row(row));
  115. #ifdef MATRIX_HAS_GHOST
  116. if (matrix_has_ghost_in_row(row)) {
  117. print(" <ghost");
  118. }
  119. #endif
  120. print("\n");
  121. }
  122. }
  123. #ifdef MATRIX_HAS_GHOST
  124. inline
  125. static bool matrix_has_ghost_in_row(uint8_t row)
  126. {
  127. // no ghost exists in case less than 2 keys on
  128. if (((matrix[row] - 1) & matrix[row]) == 0)
  129. return false;
  130. // ghost exists in case same state as other row
  131. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  132. if (i != row && (matrix[i] & matrix[row]))
  133. return true;
  134. }
  135. return false;
  136. }
  137. #endif
  138. inline
  139. static matrix_row_t read_cols(void)
  140. {
  141. return ~PIND;
  142. }
  143. inline
  144. static void unselect_rows(void)
  145. {
  146. // Hi-Z(DDR:0, PORT:0) to unselect
  147. DDRB &= ~0b11111111;
  148. PORTB &= ~0b11111111;
  149. DDRF &= ~0b11110000;
  150. PORTF &= ~0b11110000;
  151. }
  152. inline
  153. static void select_row(uint8_t row)
  154. {
  155. // Output low(DDR:1, PORT:0) to select
  156. switch (row) {
  157. case 0:
  158. case 1:
  159. case 2:
  160. case 3:
  161. case 4:
  162. case 5:
  163. case 6:
  164. case 7:
  165. DDRB |= (1<<row);
  166. PORTB &= ~(1<<row);
  167. break;
  168. case 8:
  169. DDRF |= (1<<4);
  170. PORTF &= ~(1<<4);
  171. break;
  172. case 9:
  173. case 10:
  174. case 11:
  175. DDRF |= (1<<(row-4));
  176. PORTF &= ~(1<<(row-4));
  177. break;
  178. }
  179. }