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

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