2
0

matrix.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. Copyright 2011 Jun Wako <wakojun@gmail.com>
  3. Copyright 2016 Ethan Apodaca <papodaca@gmail.com>
  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 uint8_t matrix[MATRIX_ROWS];
  26. #define ROW(code) (code>>3)
  27. #define COL(code) (code&0x07)
  28. __attribute__ ((weak))
  29. void matrix_init_kb(void) {
  30. matrix_init_user();
  31. }
  32. __attribute__ ((weak))
  33. void matrix_scan_kb(void) {
  34. matrix_scan_user();
  35. }
  36. __attribute__ ((weak))
  37. void matrix_init_user(void) {
  38. }
  39. __attribute__ ((weak))
  40. void matrix_scan_user(void) {
  41. }
  42. void matrix_init(void)
  43. {
  44. debug_enable = true;
  45. xt_host_init();
  46. // initialize matrix state: all keys off
  47. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  48. matrix_init_quantum();
  49. }
  50. // convert E0-escaped codes into unused area
  51. static uint8_t move_e0code(uint8_t code) {
  52. switch(code) {
  53. // Original IBM XT keyboard has these keys
  54. case 0x37: return 0x54; // Print Screen
  55. case 0x46: return 0x55; // Ctrl + Pause
  56. case 0x1C: return 0x6F; // Keypad Enter
  57. case 0x35: return 0x7F; // Keypad /
  58. // Any XT keyobard with these keys?
  59. // http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/translate.pdf
  60. // https://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
  61. case 0x5B: return 0x5A; // Left GUI
  62. case 0x5C: return 0x5B; // Right GUI
  63. case 0x5D: return 0x5C; // Application
  64. case 0x5E: return 0x5D; // Power(not used)
  65. case 0x5F: return 0x5E; // Sleep(not used)
  66. case 0x63: return 0x5F; // Wake (not used)
  67. case 0x48: return 0x60; // Up
  68. case 0x4B: return 0x61; // Left
  69. case 0x50: return 0x62; // Down
  70. case 0x4D: return 0x63; // Right
  71. case 0x52: return 0x71; // Insert
  72. case 0x53: return 0x72; // Delete
  73. case 0x47: return 0x74; // Home
  74. case 0x4F: return 0x75; // End
  75. case 0x49: return 0x77; // Home
  76. case 0x51: return 0x78; // End
  77. case 0x1D: return 0x7A; // Right Ctrl
  78. case 0x38: return 0x7C; // Right Alt
  79. }
  80. return 0x00;
  81. }
  82. uint8_t matrix_scan(void)
  83. {
  84. static enum {
  85. INIT,
  86. E0,
  87. // Pause: E1 1D 45, E1 9D C5
  88. E1,
  89. E1_1D,
  90. E1_9D,
  91. } state = INIT;
  92. uint8_t code = xt_host_recv();
  93. if (!code) return 0;
  94. xprintf("%02X ", code);
  95. switch (state) {
  96. case INIT:
  97. switch (code) {
  98. case 0xE0:
  99. state = E0;
  100. break;
  101. case 0xE1:
  102. state = E1;
  103. break;
  104. default:
  105. if (code < 0x80)
  106. matrix_make(code);
  107. else
  108. matrix_break(code & 0x7F);
  109. break;
  110. }
  111. break;
  112. case E0:
  113. switch (code) {
  114. case 0x2A:
  115. case 0xAA:
  116. case 0x36:
  117. case 0xB6:
  118. //ignore fake shift
  119. state = INIT;
  120. break;
  121. default:
  122. if (code < 0x80)
  123. matrix_make(move_e0code(code));
  124. else
  125. matrix_break(move_e0code(code & 0x7F));
  126. state = INIT;
  127. break;
  128. }
  129. break;
  130. case E1:
  131. switch (code) {
  132. case 0x1D:
  133. state = E1_1D;
  134. break;
  135. case 0x9D:
  136. state = E1_9D;
  137. break;
  138. default:
  139. state = INIT;
  140. break;
  141. }
  142. break;
  143. case E1_1D:
  144. switch (code) {
  145. case 0x45:
  146. matrix_make(0x55);
  147. break;
  148. default:
  149. state = INIT;
  150. break;
  151. }
  152. break;
  153. case E1_9D:
  154. switch (code) {
  155. case 0x45:
  156. matrix_break(0x55);
  157. break;
  158. default:
  159. state = INIT;
  160. break;
  161. }
  162. break;
  163. default:
  164. state = INIT;
  165. }
  166. matrix_scan_quantum();
  167. return 1;
  168. }
  169. inline
  170. uint8_t matrix_get_row(uint8_t row)
  171. {
  172. return matrix[row];
  173. }
  174. inline
  175. static void matrix_make(uint8_t code)
  176. {
  177. if (!matrix_is_on(ROW(code), COL(code))) {
  178. matrix[ROW(code)] |= 1<<COL(code);
  179. }
  180. }
  181. inline
  182. static void matrix_break(uint8_t code)
  183. {
  184. if (matrix_is_on(ROW(code), COL(code))) {
  185. matrix[ROW(code)] &= ~(1<<COL(code));
  186. }
  187. }
  188. void matrix_clear(void)
  189. {
  190. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  191. }
  192. bool matrix_is_on(uint8_t row, uint8_t col)
  193. {
  194. return (matrix_get_row(row) & (1<<col));
  195. }
  196. #if (MATRIX_COLS <= 8)
  197. # define print_matrix_header() print("\nr/c 01234567\n")
  198. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  199. #elif (MATRIX_COLS <= 16)
  200. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  201. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  202. #elif (MATRIX_COLS <= 32)
  203. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  204. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  205. #endif
  206. void matrix_print(void)
  207. {
  208. print_matrix_header();
  209. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  210. phex(row); print(": ");
  211. print_matrix_row(row);
  212. print("\n");
  213. }
  214. }
  215. /*
  216. XT Scancodes
  217. ============
  218. - http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/translate.pdf
  219. - https://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
  220. 01-53: Normal codes used in original XT keyboard
  221. 54-7F: Not used in original XT keyboard
  222. 0 1 2 3 4 5 6 7 8 9 A B C D E F
  223. 50 - - - - * * x x x x * * * o o o
  224. 60 * * * * x x x x x x x x x x x *
  225. 70 x * * x * * x * * x * x * x x *
  226. -: codes existed in original XT keyboard
  227. *: E0-escaped codes converted into unused code area(internal use in TMK)
  228. x: Non-espcaped codes(not used in real keyboards probably, for CodeSet2-CodeSet1 translation purpose)
  229. o: reserved
  230. Usage in TMK:
  231. 00 (reserved) DO NOT USE
  232. 54 PrintScr*
  233. 55 Pause*
  234. 56 Euro2
  235. 57 F11
  236. 58 F12
  237. 59 Keypad =
  238. 5A LGUI*
  239. 5B RGUI*
  240. 5C APP*
  241. 5D (reserved)
  242. 5E (reserved)
  243. 5F (reserved)
  244. 60 cursor*
  245. 61 cursor*
  246. 62 cursor*
  247. 63 cursor*
  248. 64 F13
  249. 65 F14
  250. 66 F15
  251. 67 F16
  252. 68 F17
  253. 69 F18
  254. 6A F19
  255. 6B F20
  256. 6C F21
  257. 6D F22
  258. 6E F23
  259. 6F Keypad Enter*
  260. 70 KANA
  261. 71 nav*
  262. 72 nav*
  263. 73 RO
  264. 74 nav*
  265. 75 nav*
  266. 76 F24
  267. 77 nav*
  268. 78 nav*
  269. 79 HENKAN
  270. 7A RCTL*
  271. 7B MUHENKAN
  272. 7C RALT*
  273. 7D JPY
  274. 7E Keypad ,
  275. 7F Keypad / *
  276. */