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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. Copyright 2011 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 "util.h"
  20. #include "debug.h"
  21. #include "ps2.h"
  22. #include "matrix.h"
  23. static void matrix_make(uint8_t code);
  24. static void matrix_break(uint8_t code);
  25. #ifdef MATRIX_HAS_GHOST
  26. static bool matrix_has_ghost_in_row(uint8_t row);
  27. #endif
  28. /*
  29. * Matrix Array usage:
  30. * 'Scan Code Set 3' is assigned into 17x8 cell matrix.
  31. *
  32. * 8bit wide
  33. * +---------+
  34. * 0| |
  35. * :| | 0x00-0x87
  36. * ;| |
  37. * 17| |
  38. * +---------+
  39. */
  40. static uint8_t matrix[MATRIX_ROWS];
  41. #define ROW(code) (code>>3)
  42. #define COL(code) (code&0x07)
  43. static bool is_modified = false;
  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. debug_enable = true;
  57. //debug_matrix = true;
  58. //debug_keyboard = true;
  59. //debug_mouse = false;
  60. ps2_host_init();
  61. // initialize matrix state: all keys off
  62. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  63. return;
  64. }
  65. uint8_t matrix_scan(void)
  66. {
  67. // scan code reading states
  68. static enum {
  69. RESET,
  70. RESET_RESPONSE,
  71. KBD_ID0,
  72. KBD_ID1,
  73. CONFIG,
  74. READY,
  75. F0,
  76. } state = RESET;
  77. is_modified = false;
  78. uint8_t code;
  79. if ((code = ps2_host_recv())) {
  80. debug("r"); debug_hex(code); debug(" ");
  81. }
  82. switch (state) {
  83. case RESET:
  84. debug("wFF ");
  85. if (ps2_host_send(0xFF) == 0xFA) {
  86. debug("[ack]\nRESET_RESPONSE: ");
  87. state = RESET_RESPONSE;
  88. }
  89. break;
  90. case RESET_RESPONSE:
  91. if (code == 0xAA) {
  92. debug("[ok]\nKBD_ID: ");
  93. state = KBD_ID0;
  94. } else if (code) {
  95. debug("err\nRESET: ");
  96. state = RESET;
  97. }
  98. break;
  99. // after reset receive keyboad ID(2 bytes)
  100. case KBD_ID0:
  101. if (code) {
  102. state = KBD_ID1;
  103. }
  104. break;
  105. case KBD_ID1:
  106. if (code) {
  107. debug("\nCONFIG: ");
  108. state = CONFIG;
  109. }
  110. break;
  111. case CONFIG:
  112. debug("wF8 ");
  113. if (ps2_host_send(0xF8) == 0xFA) {
  114. debug("[ack]\nREADY\n");
  115. state = READY;
  116. }
  117. break;
  118. case READY:
  119. switch (code) {
  120. case 0x00:
  121. break;
  122. case 0xF0:
  123. state = F0;
  124. debug(" ");
  125. break;
  126. default: // normal key make
  127. if (code < 0x88) {
  128. matrix_make(code);
  129. } else {
  130. debug("unexpected scan code at READY: "); debug_hex(code); debug("\n");
  131. }
  132. state = READY;
  133. debug("\n");
  134. }
  135. break;
  136. case F0: // Break code
  137. switch (code) {
  138. case 0x00:
  139. break;
  140. default:
  141. if (code < 0x88) {
  142. matrix_break(code);
  143. } else {
  144. debug("unexpected scan code at F0: "); debug_hex(code); debug("\n");
  145. }
  146. state = READY;
  147. debug("\n");
  148. }
  149. break;
  150. }
  151. return 1;
  152. }
  153. bool matrix_is_modified(void)
  154. {
  155. return is_modified;
  156. }
  157. inline
  158. bool matrix_has_ghost(void)
  159. {
  160. #ifdef MATRIX_HAS_GHOST
  161. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  162. if (matrix_has_ghost_in_row(i))
  163. return true;
  164. }
  165. #endif
  166. return false;
  167. }
  168. inline
  169. bool matrix_is_on(uint8_t row, uint8_t col)
  170. {
  171. return (matrix[row] & (1<<col));
  172. }
  173. inline
  174. uint8_t matrix_get_row(uint8_t row)
  175. {
  176. return matrix[row];
  177. }
  178. void matrix_print(void)
  179. {
  180. print("\nr/c 01234567\n");
  181. for (uint8_t row = 0; row < matrix_rows(); row++) {
  182. phex(row); print(": ");
  183. pbin_reverse(matrix_get_row(row));
  184. #ifdef MATRIX_HAS_GHOST
  185. if (matrix_has_ghost_in_row(row)) {
  186. print(" <ghost");
  187. }
  188. #endif
  189. print("\n");
  190. }
  191. }
  192. uint8_t matrix_key_count(void)
  193. {
  194. uint8_t count = 0;
  195. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  196. count += bitpop(matrix[i]);
  197. }
  198. return count;
  199. }
  200. #ifdef MATRIX_HAS_GHOST
  201. inline
  202. static bool matrix_has_ghost_in_row(uint8_t row)
  203. {
  204. // no ghost exists in case less than 2 keys on
  205. if (((matrix[row] - 1) & matrix[row]) == 0)
  206. return false;
  207. // ghost exists in case same state as other row
  208. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  209. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  210. return true;
  211. }
  212. return false;
  213. }
  214. #endif
  215. inline
  216. static void matrix_make(uint8_t code)
  217. {
  218. if (!matrix_is_on(ROW(code), COL(code))) {
  219. matrix[ROW(code)] |= 1<<COL(code);
  220. is_modified = true;
  221. }
  222. }
  223. inline
  224. static void matrix_break(uint8_t code)
  225. {
  226. if (matrix_is_on(ROW(code), COL(code))) {
  227. matrix[ROW(code)] &= ~(1<<COL(code));
  228. is_modified = true;
  229. }
  230. }