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

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