Keyboard firmwares for Atmel AVR and Cortex-M
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.

matrix.c 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. #ifdef MATRIX_HAS_GHOST
  26. static bool matrix_has_ghost_in_row(uint8_t row);
  27. #endif
  28. static uint8_t matrix[MATRIX_ROWS];
  29. #define ROW(code) (code>>3)
  30. #define COL(code) (code&0x07)
  31. // matrix positions for exceptional keys
  32. #define PRINT_SCREEN (0x7C)
  33. #define PAUSE (0x7D)
  34. static bool is_modified = false;
  35. inline
  36. uint8_t matrix_rows(void)
  37. {
  38. return MATRIX_ROWS;
  39. }
  40. inline
  41. uint8_t matrix_cols(void)
  42. {
  43. return MATRIX_COLS;
  44. }
  45. void matrix_init(void)
  46. {
  47. debug_enable = true;
  48. xt_host_init();
  49. // initialize matrix state: all keys off
  50. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  51. return;
  52. }
  53. static uint8_t move_codes(uint8_t code) {
  54. switch(code) {
  55. case 0x10:
  56. code += 0x5E;
  57. break;
  58. case 0x19:
  59. code += 0x41;
  60. break;
  61. case 0x1C:
  62. case 0x1D:
  63. code += 0x38;
  64. break;
  65. case 0x20:
  66. case 0x21:
  67. case 0x22:
  68. case 0x24:
  69. code += 0x40;
  70. break;
  71. case 0x2E:
  72. case 0x30:
  73. case 0x32:
  74. code += 0x44;
  75. break;
  76. case 0x35:
  77. case 0x38:
  78. code += 0x21;
  79. break;
  80. case 0x47:
  81. case 0x48:
  82. case 0x49:
  83. case 0x4B:
  84. case 0x4D:
  85. case 0x4F:
  86. case 0x50:
  87. case 0x51:
  88. case 0x52:
  89. case 0x53:
  90. code += 0x28;
  91. break;
  92. }
  93. return code;
  94. }
  95. uint8_t matrix_scan(void)
  96. {
  97. // scan code reading states
  98. static enum {
  99. INIT,
  100. E0,
  101. E0_2A,
  102. E0_2A_E0,
  103. E0_B7,
  104. E0_B7_E0,
  105. // print screen
  106. E1,
  107. E1_1D,
  108. E1_1D_45,
  109. E1_1D_45_E1,
  110. E1_1D_45_E1_9D,
  111. // pause
  112. } state = INIT;
  113. is_modified = false;
  114. // 'pseudo break code' hack
  115. if (matrix_is_on(ROW(PAUSE), COL(PAUSE))) {
  116. matrix_break(PAUSE);
  117. }
  118. uint8_t code = xt_host_recv();
  119. switch (state) {
  120. case INIT:
  121. switch (code) {
  122. case 0xE0:
  123. state = E0;
  124. break;
  125. case 0xE1:
  126. state = E1;
  127. break;
  128. default: // normal key make
  129. if (code < 0x80 && code != 0x00) {
  130. xprintf("make: %X\r\n", code);
  131. matrix_make(code);
  132. } else if (code > 0x80 && code < 0xFF && code != 0x00) {
  133. xprintf("break %X\r\n", code);
  134. matrix_break(code - 0x80);
  135. }
  136. state = INIT;
  137. }
  138. break;
  139. case E0: // E0-Prefixed
  140. switch (code) { //move these codes to unused places on the matrix
  141. case 0x2A:
  142. state = E0_2A;
  143. break;
  144. case 0xB7:
  145. state = E0_B7;
  146. break;
  147. default:
  148. if (code < 0x80 && code != 0x00) {
  149. matrix_make(move_codes(code));
  150. } else if (code > 0x80 && code < 0xFF && code != 0x00) {
  151. matrix_break(move_codes(code - 0x80));
  152. }
  153. state = INIT;
  154. }
  155. break;
  156. case E0_2A:
  157. if(code == 0xE0)
  158. state = E0_2A_E0;
  159. else
  160. state = INIT;
  161. break;
  162. case E0_2A_E0:
  163. if(code == 0x37)
  164. matrix_make(PRINT_SCREEN);
  165. else
  166. state = INIT;
  167. break;
  168. case E0_B7:
  169. if(code == 0xE0)
  170. state = E0_B7;
  171. else
  172. state = INIT;
  173. break;
  174. case E0_B7_E0:
  175. if(code == 0xAA)
  176. matrix_break(PRINT_SCREEN);
  177. else
  178. state = INIT;
  179. break;
  180. case E1:
  181. if (code == 0x1D)
  182. state = E1_1D;
  183. else
  184. state = INIT;
  185. break;
  186. case E1_1D:
  187. if(code == 0x45)
  188. state = E1_1D_45;
  189. else
  190. state = INIT;
  191. break;
  192. case E1_1D_45:
  193. if(code == 0xE1)
  194. state = E1_1D_45_E1;
  195. else
  196. state = INIT;
  197. break;
  198. case E1_1D_45_E1:
  199. if(code == 0x9D)
  200. state = E1_1D_45_E1_9D;
  201. else
  202. state = INIT;
  203. break;
  204. case E1_1D_45_E1_9D:
  205. if(code == 0xC5)
  206. matrix_make(PAUSE);
  207. else
  208. state = INIT;
  209. break;
  210. default:
  211. state = INIT;
  212. }
  213. return 1;
  214. }
  215. bool matrix_is_modified(void)
  216. {
  217. return is_modified;
  218. }
  219. inline
  220. bool matrix_has_ghost(void)
  221. {
  222. #ifdef MATRIX_HAS_GHOST
  223. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  224. if (matrix_has_ghost_in_row(i))
  225. return true;
  226. }
  227. #endif
  228. return false;
  229. }
  230. inline
  231. bool matrix_is_on(uint8_t row, uint8_t col)
  232. {
  233. return (matrix[row] & (1<<col));
  234. }
  235. inline
  236. uint8_t matrix_get_row(uint8_t row)
  237. {
  238. return matrix[row];
  239. }
  240. void matrix_print(void)
  241. {
  242. print("\nr/c 01234567\n");
  243. for (uint8_t row = 0; row < matrix_rows(); row++) {
  244. phex(row); print(": ");
  245. pbin_reverse(matrix_get_row(row));
  246. #ifdef MATRIX_HAS_GHOST
  247. if (matrix_has_ghost_in_row(row)) {
  248. print(" <ghost");
  249. }
  250. #endif
  251. print("\n");
  252. }
  253. }
  254. uint8_t matrix_key_count(void)
  255. {
  256. uint8_t count = 0;
  257. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  258. count += bitpop(matrix[i]);
  259. }
  260. return count;
  261. }
  262. #ifdef MATRIX_HAS_GHOST
  263. inline
  264. static bool matrix_has_ghost_in_row(uint8_t row)
  265. {
  266. // no ghost exists in case less than 2 keys on
  267. if (((matrix[row] - 1) & matrix[row]) == 0)
  268. return false;
  269. // ghost exists in case same state as other row
  270. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  271. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  272. return true;
  273. }
  274. return false;
  275. }
  276. #endif
  277. inline
  278. static void matrix_make(uint8_t code)
  279. {
  280. if (!matrix_is_on(ROW(code), COL(code))) {
  281. matrix[ROW(code)] |= 1<<COL(code);
  282. is_modified = true;
  283. }
  284. }
  285. inline
  286. static void matrix_break(uint8_t code)
  287. {
  288. if (matrix_is_on(ROW(code), COL(code))) {
  289. matrix[ROW(code)] &= ~(1<<COL(code));
  290. is_modified = true;
  291. }
  292. }
  293. void matrix_clear(void)
  294. {
  295. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  296. }