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 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. Copyright 2011 Jun Wako <[email protected]>
  3. Copyright 2016 Ethan Apodaca <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include "action.h"
  18. #include "print.h"
  19. #include "util.h"
  20. #include "debug.h"
  21. #include "xt.h"
  22. #include "matrix.h"
  23. static void matrix_make(uint8_t code);
  24. static void matrix_break(uint8_t code);
  25. static void matrix_clear(void);
  26. #ifdef MATRIX_HAS_GHOST
  27. static bool matrix_has_ghost_in_row(uint8_t row);
  28. #endif
  29. static uint8_t matrix[MATRIX_ROWS];
  30. #define ROW(code) (code>>3)
  31. #define COL(code) (code&0x07)
  32. // matrix positions for exceptional keys
  33. #define PRINT_SCREEN (0x7C)
  34. #define PAUSE (0x7D)
  35. static bool is_modified = false;
  36. inline
  37. uint8_t matrix_rows(void)
  38. {
  39. return MATRIX_ROWS;
  40. }
  41. inline
  42. uint8_t matrix_cols(void)
  43. {
  44. return MATRIX_COLS;
  45. }
  46. void matrix_init(void)
  47. {
  48. debug_enable = true;
  49. xt_host_init();
  50. // initialize matrix state: all keys off
  51. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  52. return;
  53. }
  54. static uint8_t move_codes(uint8_t code) {
  55. switch(code) {
  56. case 0x10:
  57. code += 0x5E;
  58. break;
  59. case 0x19:
  60. code += 0x41;
  61. break;
  62. case 0x1C:
  63. case 0x1D:
  64. code += 0x38;
  65. break;
  66. case 0x20:
  67. case 0x21:
  68. case 0x22:
  69. case 0x24:
  70. code += 0x40;
  71. break;
  72. case 0x2E:
  73. case 0x30:
  74. case 0x32:
  75. code += 0x44;
  76. break;
  77. case 0x35:
  78. case 0x38:
  79. code += 0x21;
  80. break;
  81. case 0x47:
  82. case 0x48:
  83. case 0x49:
  84. case 0x4B:
  85. case 0x4D:
  86. case 0x4F:
  87. case 0x50:
  88. case 0x51:
  89. case 0x52:
  90. case 0x53:
  91. code += 0x28;
  92. break;
  93. }
  94. return code;
  95. }
  96. uint8_t matrix_scan(void)
  97. {
  98. // scan code reading states
  99. static enum {
  100. INIT,
  101. E0,
  102. E0_2A,
  103. E0_2A_E0,
  104. E0_B7,
  105. E0_B7_E0,
  106. // print screen
  107. E1,
  108. E1_1D,
  109. E1_1D_45,
  110. E1_1D_45_E1,
  111. E1_1D_45_E1_9D,
  112. // pause
  113. } state = INIT;
  114. is_modified = false;
  115. // 'pseudo break code' hack
  116. if (matrix_is_on(ROW(PAUSE), COL(PAUSE))) {
  117. matrix_break(PAUSE);
  118. }
  119. uint8_t code = xt_host_recv();
  120. if (code) xprintf("%X\r\n", code);
  121. switch (state) {
  122. case INIT:
  123. switch (code) {
  124. case 0xE0:
  125. state = E0;
  126. break;
  127. case 0xE1:
  128. state = E1;
  129. break;
  130. case 0x00:
  131. break;
  132. default: // normal key make
  133. if (code < 0x80) {
  134. xprintf("make: %X\r\n", code);
  135. matrix_make(code);
  136. } else if (code > 0x80 && code < 0xFF) {
  137. xprintf("break %X\r\n", code);
  138. matrix_break(code - 0x80);
  139. } else {
  140. matrix_clear();
  141. clear_keyboard();
  142. xprintf("unexpected scan code at INIT: %02X\n", code);
  143. }
  144. state = INIT;
  145. }
  146. break;
  147. case E0: // E0-Prefixed
  148. switch (code) { //move these codes to unused places on the matrix
  149. case 0x2A:
  150. state = E0_2A;
  151. break;
  152. case 0xB7:
  153. state = E0_B7;
  154. break;
  155. case 0x00:
  156. state = INIT;
  157. break;
  158. default:
  159. if (code < 0x80) {
  160. matrix_make(move_codes(code));
  161. } else if (code > 0x80 && code < 0xFF) {
  162. matrix_break(move_codes(code - 0x80));
  163. } else {
  164. matrix_clear();
  165. clear_keyboard();
  166. xprintf("unexpected scan code at E0: %02X\n", code);
  167. }
  168. state = INIT;
  169. }
  170. break;
  171. case E0_2A:
  172. switch (code) {
  173. case 0xE0:
  174. state = E0_2A_E0;
  175. break;
  176. default:
  177. state = INIT;
  178. }
  179. break;
  180. case E0_2A_E0:
  181. switch (code) {
  182. case 0x37:
  183. matrix_make(PRINT_SCREEN);
  184. break;
  185. default:
  186. state = INIT;
  187. }
  188. break;
  189. case E0_B7:
  190. switch (code) {
  191. case 0xE0:
  192. state = E0_B7;
  193. break;
  194. default:
  195. state = INIT;
  196. }
  197. break;
  198. case E0_B7_E0:
  199. switch (code) {
  200. case 0xAA:
  201. matrix_break(PRINT_SCREEN);
  202. break;
  203. default:
  204. state = INIT;
  205. }
  206. break;
  207. case E1:
  208. switch (code) {
  209. case 0x1D:
  210. state = E1_1D;
  211. break;
  212. default:
  213. state = INIT;
  214. }
  215. break;
  216. case E1_1D:
  217. switch (code) {
  218. case 0x45:
  219. state = E1_1D_45;
  220. break;
  221. default:
  222. state = INIT;
  223. }
  224. break;
  225. case E1_1D_45:
  226. switch (code) {
  227. case 0xE1:
  228. state = E1_1D_45_E1;
  229. break;
  230. default:
  231. state = INIT;
  232. }
  233. break;
  234. case E1_1D_45_E1:
  235. switch (code) {
  236. case 0x9D:
  237. state = E1_1D_45_E1_9D;
  238. break;
  239. default:
  240. state = INIT;
  241. }
  242. break;
  243. case E1_1D_45_E1_9D:
  244. switch (code) {
  245. case 0xC5:
  246. matrix_make(PAUSE);
  247. break;
  248. default:
  249. state = INIT;
  250. }
  251. break;
  252. default:
  253. state = INIT;
  254. }
  255. // TODO: request RESEND when error occurs?
  256. /*
  257. if (PS2_IS_FAILED(ps2_error)) {
  258. uint8_t ret = ps2_host_send(PS2_RESEND);
  259. xprintf("Resend: %02X\n", ret);
  260. }
  261. */
  262. return 1;
  263. }
  264. bool matrix_is_modified(void)
  265. {
  266. return is_modified;
  267. }
  268. inline
  269. bool matrix_has_ghost(void)
  270. {
  271. #ifdef MATRIX_HAS_GHOST
  272. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  273. if (matrix_has_ghost_in_row(i))
  274. return true;
  275. }
  276. #endif
  277. return false;
  278. }
  279. inline
  280. bool matrix_is_on(uint8_t row, uint8_t col)
  281. {
  282. return (matrix[row] & (1<<col));
  283. }
  284. inline
  285. uint8_t matrix_get_row(uint8_t row)
  286. {
  287. return matrix[row];
  288. }
  289. void matrix_print(void)
  290. {
  291. print("\nr/c 01234567\n");
  292. for (uint8_t row = 0; row < matrix_rows(); row++) {
  293. phex(row); print(": ");
  294. pbin_reverse(matrix_get_row(row));
  295. #ifdef MATRIX_HAS_GHOST
  296. if (matrix_has_ghost_in_row(row)) {
  297. print(" <ghost");
  298. }
  299. #endif
  300. print("\n");
  301. }
  302. }
  303. uint8_t matrix_key_count(void)
  304. {
  305. uint8_t count = 0;
  306. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  307. count += bitpop(matrix[i]);
  308. }
  309. return count;
  310. }
  311. #ifdef MATRIX_HAS_GHOST
  312. inline
  313. static bool matrix_has_ghost_in_row(uint8_t row)
  314. {
  315. // no ghost exists in case less than 2 keys on
  316. if (((matrix[row] - 1) & matrix[row]) == 0)
  317. return false;
  318. // ghost exists in case same state as other row
  319. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  320. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  321. return true;
  322. }
  323. return false;
  324. }
  325. #endif
  326. inline
  327. static void matrix_make(uint8_t code)
  328. {
  329. if (!matrix_is_on(ROW(code), COL(code))) {
  330. matrix[ROW(code)] |= 1<<COL(code);
  331. is_modified = true;
  332. }
  333. }
  334. inline
  335. static void matrix_break(uint8_t code)
  336. {
  337. if (matrix_is_on(ROW(code), COL(code))) {
  338. matrix[ROW(code)] &= ~(1<<COL(code));
  339. is_modified = true;
  340. }
  341. }
  342. inline
  343. static void matrix_clear(void)
  344. {
  345. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  346. }