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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. switch (state) {
  121. case INIT:
  122. switch (code) {
  123. case 0xE0:
  124. state = E0;
  125. break;
  126. case 0xE1:
  127. state = E1;
  128. break;
  129. default: // normal key make
  130. if (code < 0x80 && code != 0x00) {
  131. xprintf("make: %X\r\n", code);
  132. matrix_make(code);
  133. } else if (code > 0x80 && code < 0xFF && code != 0x00) {
  134. xprintf("break %X\r\n", code);
  135. matrix_break(code - 0x80);
  136. }
  137. state = INIT;
  138. }
  139. break;
  140. case E0: // E0-Prefixed
  141. switch (code) { //move these codes to unused places on the matrix
  142. case 0x2A:
  143. state = E0_2A;
  144. break;
  145. case 0xB7:
  146. state = E0_B7;
  147. break;
  148. default:
  149. if (code < 0x80 && code != 0x00) {
  150. matrix_make(move_codes(code));
  151. } else if (code > 0x80 && code < 0xFF && code != 0x00) {
  152. matrix_break(move_codes(code - 0x80));
  153. }
  154. state = INIT;
  155. }
  156. break;
  157. case E0_2A:
  158. if(code == 0xE0)
  159. state = E0_2A_E0;
  160. else
  161. state = INIT;
  162. break;
  163. case E0_2A_E0:
  164. if(code == 0x37)
  165. matrix_make(PRINT_SCREEN);
  166. else
  167. state = INIT;
  168. break;
  169. case E0_B7:
  170. if(code == 0xE0)
  171. state = E0_B7;
  172. else
  173. state = INIT;
  174. break;
  175. case E0_B7_E0:
  176. if(code == 0xAA)
  177. matrix_break(PRINT_SCREEN);
  178. else
  179. state = INIT;
  180. break;
  181. case E1:
  182. if (code == 0x1D)
  183. state = E1_1D;
  184. else
  185. state = INIT;
  186. break;
  187. case E1_1D:
  188. if(code == 0x45)
  189. state = E1_1D_45;
  190. else
  191. state = INIT;
  192. break;
  193. case E1_1D_45:
  194. if(code == 0xE1)
  195. state = E1_1D_45_E1;
  196. else
  197. state = INIT;
  198. break;
  199. case E1_1D_45_E1:
  200. if(code == 0x9D)
  201. state = E1_1D_45_E1_9D;
  202. else
  203. state = INIT;
  204. break;
  205. case E1_1D_45_E1_9D:
  206. if(code == 0xC5)
  207. matrix_make(PAUSE);
  208. else
  209. state = INIT;
  210. break;
  211. default:
  212. state = INIT;
  213. }
  214. return 1;
  215. }
  216. bool matrix_is_modified(void)
  217. {
  218. return is_modified;
  219. }
  220. inline
  221. bool matrix_has_ghost(void)
  222. {
  223. #ifdef MATRIX_HAS_GHOST
  224. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  225. if (matrix_has_ghost_in_row(i))
  226. return true;
  227. }
  228. #endif
  229. return false;
  230. }
  231. inline
  232. bool matrix_is_on(uint8_t row, uint8_t col)
  233. {
  234. return (matrix[row] & (1<<col));
  235. }
  236. inline
  237. uint8_t matrix_get_row(uint8_t row)
  238. {
  239. return matrix[row];
  240. }
  241. void matrix_print(void)
  242. {
  243. print("\nr/c 01234567\n");
  244. for (uint8_t row = 0; row < matrix_rows(); row++) {
  245. phex(row); print(": ");
  246. pbin_reverse(matrix_get_row(row));
  247. #ifdef MATRIX_HAS_GHOST
  248. if (matrix_has_ghost_in_row(row)) {
  249. print(" <ghost");
  250. }
  251. #endif
  252. print("\n");
  253. }
  254. }
  255. uint8_t matrix_key_count(void)
  256. {
  257. uint8_t count = 0;
  258. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  259. count += bitpop(matrix[i]);
  260. }
  261. return count;
  262. }
  263. #ifdef MATRIX_HAS_GHOST
  264. inline
  265. static bool matrix_has_ghost_in_row(uint8_t row)
  266. {
  267. // no ghost exists in case less than 2 keys on
  268. if (((matrix[row] - 1) & matrix[row]) == 0)
  269. return false;
  270. // ghost exists in case same state as other row
  271. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  272. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  273. return true;
  274. }
  275. return false;
  276. }
  277. #endif
  278. inline
  279. static void matrix_make(uint8_t code)
  280. {
  281. if (!matrix_is_on(ROW(code), COL(code))) {
  282. matrix[ROW(code)] |= 1<<COL(code);
  283. is_modified = true;
  284. }
  285. }
  286. inline
  287. static void matrix_break(uint8_t code)
  288. {
  289. if (matrix_is_on(ROW(code), COL(code))) {
  290. matrix[ROW(code)] &= ~(1<<COL(code));
  291. is_modified = true;
  292. }
  293. }
  294. inline
  295. static void matrix_clear(void)
  296. {
  297. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  298. }