matrix.c 5.8 KB

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