matrix.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. Copyright 2011 Jun Wako <wakojun@gmail.com>
  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 "util.h"
  20. #include "debug.h"
  21. #include "ps2.h"
  22. #include "matrix.h"
  23. #define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  24. #define print_matrix_header() print("\nr/c 01234567\n")
  25. static void matrix_make(uint8_t code);
  26. static void matrix_break(uint8_t code);
  27. /*
  28. * Matrix Array usage:
  29. * 'Scan Code Set 3' is assigned into 17x8 cell matrix.
  30. *
  31. * 8bit wide
  32. * +---------+
  33. * 0| |
  34. * :| | 0x00-0x87
  35. * ;| |
  36. * 17| |
  37. * +---------+
  38. */
  39. static uint8_t matrix[MATRIX_ROWS];
  40. #define ROW(code) (code>>3)
  41. #define COL(code) (code&0x07)
  42. __attribute__ ((weak))
  43. void matrix_init_user(void) {
  44. }
  45. void matrix_init(void)
  46. {
  47. debug_enable = true;
  48. //debug_matrix = true;
  49. //debug_keyboard = true;
  50. //debug_mouse = false;
  51. ps2_host_init();
  52. // initialize matrix state: all keys off
  53. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  54. matrix_init_user();
  55. return;
  56. }
  57. uint8_t matrix_scan(void)
  58. {
  59. // scan code reading states
  60. static enum {
  61. RESET,
  62. RESET_RESPONSE,
  63. KBD_ID0,
  64. KBD_ID1,
  65. CONFIG,
  66. READY,
  67. F0_BREAK,
  68. } state = RESET;
  69. uint8_t code;
  70. if ((code = ps2_host_recv())) {
  71. dprintf("r%02X ", code);
  72. }
  73. switch (state) {
  74. case RESET:
  75. dprint("wFF ");
  76. if (ps2_host_send(0xFF) == 0xFA) {
  77. dprint("[ack]\nRESET_RESPONSE: ");
  78. state = RESET_RESPONSE;
  79. }
  80. break;
  81. case RESET_RESPONSE:
  82. if (code == 0xAA) {
  83. dprint("[ok]\nKBD_ID: ");
  84. state = KBD_ID0;
  85. } else if (code) {
  86. dprint("err\nRESET: ");
  87. state = RESET;
  88. }
  89. break;
  90. // after reset receive keyboard ID(2 bytes)
  91. case KBD_ID0:
  92. if (code) {
  93. state = KBD_ID1;
  94. }
  95. break;
  96. case KBD_ID1:
  97. if (code) {
  98. dprint("\nCONFIG: ");
  99. state = CONFIG;
  100. }
  101. break;
  102. case CONFIG:
  103. dprint("wF8 ");
  104. if (ps2_host_send(0xF8) == 0xFA) {
  105. dprint("[ack]\nREADY\n");
  106. state = READY;
  107. }
  108. break;
  109. case READY:
  110. switch (code) {
  111. case 0x00:
  112. break;
  113. case 0xF0:
  114. state = F0_BREAK;
  115. dprint(" ");
  116. break;
  117. default: // normal key make
  118. if (code < 0x88) {
  119. matrix_make(code);
  120. } else {
  121. dprintf("unexpected scan code at READY: %02X\n", code);
  122. }
  123. state = READY;
  124. dprint("\n");
  125. }
  126. break;
  127. case F0_BREAK: // Break code
  128. switch (code) {
  129. case 0x00:
  130. break;
  131. default:
  132. if (code < 0x88) {
  133. matrix_break(code);
  134. } else {
  135. dprintf("unexpected scan code at F0: %02X\n", code);
  136. }
  137. state = READY;
  138. dprint("\n");
  139. }
  140. break;
  141. }
  142. return 1;
  143. }
  144. inline
  145. uint8_t matrix_get_row(uint8_t row)
  146. {
  147. return matrix[row];
  148. }
  149. inline
  150. static void matrix_make(uint8_t code)
  151. {
  152. if (!matrix_is_on(ROW(code), COL(code))) {
  153. matrix[ROW(code)] |= 1<<COL(code);
  154. }
  155. }
  156. inline
  157. static void matrix_break(uint8_t code)
  158. {
  159. if (matrix_is_on(ROW(code), COL(code))) {
  160. matrix[ROW(code)] &= ~(1<<COL(code));
  161. }
  162. }
  163. bool matrix_is_on(uint8_t row, uint8_t col)
  164. {
  165. return (matrix_get_row(row) & (1<<col));
  166. }
  167. void matrix_print(void)
  168. {
  169. #if (MATRIX_COLS <= 8)
  170. print("r/c 01234567\n");
  171. #elif (MATRIX_COLS <= 16)
  172. print("r/c 0123456789ABCDEF\n");
  173. #elif (MATRIX_COLS <= 32)
  174. print("r/c 0123456789ABCDEF0123456789ABCDEF\n");
  175. #endif
  176. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  177. #if (MATRIX_COLS <= 8)
  178. xprintf("%02X: %08b%s\n", row, bitrev(matrix_get_row(row)),
  179. #elif (MATRIX_COLS <= 16)
  180. xprintf("%02X: %016b%s\n", row, bitrev16(matrix_get_row(row)),
  181. #elif (MATRIX_COLS <= 32)
  182. xprintf("%02X: %032b%s\n", row, bitrev32(matrix_get_row(row)),
  183. #endif
  184. #ifdef MATRIX_HAS_GHOST
  185. matrix_has_ghost_in_row(row) ? " <ghost" : ""
  186. #else
  187. ""
  188. #endif
  189. );
  190. }
  191. }
  192. #ifdef MATRIX_HAS_GHOST
  193. __attribute__ ((weak))
  194. bool matrix_has_ghost_in_row(uint8_t row)
  195. {
  196. matrix_row_t matrix_row = matrix_get_row(row);
  197. // No ghost exists when less than 2 keys are down on the row
  198. if (((matrix_row - 1) & matrix_row) == 0)
  199. return false;
  200. // Ghost occurs when the row shares column line with other row
  201. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  202. if (i != row && (matrix_get_row(i) & matrix_row))
  203. return true;
  204. }
  205. return false;
  206. }
  207. #endif