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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

matrix.c 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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_is_on(uint8_t row, uint8_t col)
  127. {
  128. return (matrix[row] & (1<<col));
  129. }
  130. inline
  131. #if (MATRIX_COLS <= 8)
  132. uint8_t matrix_get_row(uint8_t row)
  133. #else
  134. uint16_t matrix_get_row(uint8_t row)
  135. #endif
  136. {
  137. return matrix[row];
  138. }
  139. void matrix_print(void)
  140. {
  141. print("\nr/c 01234567\n");
  142. for (uint8_t row = 0; row < matrix_rows(); row++) {
  143. phex(row); print(": ");
  144. #if (MATRIX_COLS <= 8)
  145. pbin_reverse(matrix_get_row(row));
  146. #else
  147. pbin_reverse16(matrix_get_row(row));
  148. #endif
  149. #ifdef MATRIX_HAS_GHOST
  150. if (matrix_has_ghost_in_row(row)) {
  151. print(" <ghost");
  152. }
  153. #endif
  154. print("\n");
  155. }
  156. }
  157. uint8_t matrix_key_count(void)
  158. {
  159. uint8_t count = 0;
  160. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  161. #if (MATRIX_COLS <= 8)
  162. count += bitpop(matrix[i]);
  163. #else
  164. count += bitpop16(matrix[i]);
  165. #endif
  166. }
  167. return count;
  168. }
  169. #ifdef MATRIX_HAS_GHOST
  170. inline
  171. static bool matrix_has_ghost_in_row(uint8_t row)
  172. {
  173. // no ghost exists in case less than 2 keys on
  174. if (((matrix[row] - 1) & matrix[row]) == 0)
  175. return false;
  176. // ghost exists in case same state as other row
  177. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  178. //if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  179. if (i != row && (matrix[i] & matrix[row]))
  180. return true;
  181. }
  182. return false;
  183. }
  184. #endif
  185. inline
  186. static uint8_t read_col(void)
  187. {
  188. return PIND;
  189. }
  190. inline
  191. static void unselect_rows(void)
  192. {
  193. // Hi-Z(DDR:0, PORT:0) to unselect
  194. DDRB &= ~0b11111111;
  195. PORTB &= ~0b11111111;
  196. DDRF &= ~0b11110000;
  197. PORTF &= ~0b11110000;
  198. }
  199. inline
  200. static void select_row(uint8_t row)
  201. {
  202. // Output low(DDR:1, PORT:0) to select
  203. switch (row) {
  204. case 0:
  205. case 1:
  206. case 2:
  207. case 3:
  208. case 4:
  209. case 5:
  210. case 6:
  211. case 7:
  212. DDRB |= (1<<row);
  213. PORTB &= ~(1<<row);
  214. break;
  215. case 8:
  216. DDRF |= (1<<4);
  217. PORTF &= ~(1<<4);
  218. break;
  219. case 9:
  220. case 10:
  221. case 11:
  222. DDRF |= (1<<(row-4));
  223. PORTF &= ~(1<<(row-4));
  224. break;
  225. }
  226. }