logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (8250B)


  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. void matrix_init(void) {
  42. debug_enable = true;
  43. xt_host_init();
  44. // initialize matrix state: all keys off
  45. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  46. matrix[i] = 0x00;
  47. }
  48. matrix_init_kb();
  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 keyboard 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. static enum {
  84. XT_STATE_INIT,
  85. XT_STATE_E0,
  86. // Pause: E1 1D 45, E1 9D C5
  87. XT_STATE_E1,
  88. XT_STATE_E1_1D,
  89. XT_STATE_E1_9D,
  90. } state = XT_STATE_INIT;
  91. uint8_t code = xt_host_recv();
  92. if (!code) {
  93. return 0;
  94. }
  95. xprintf("%02X ", code);
  96. switch (state) {
  97. case XT_STATE_INIT:
  98. switch (code) {
  99. case 0xE0:
  100. state = XT_STATE_E0;
  101. break;
  102. case 0xE1:
  103. state = XT_STATE_E1;
  104. break;
  105. default:
  106. if (code < 0x80) {
  107. matrix_make(code);
  108. } else {
  109. matrix_break(code & 0x7F);
  110. }
  111. break;
  112. }
  113. break;
  114. case XT_STATE_E0:
  115. switch (code) {
  116. case 0x2A:
  117. case 0xAA:
  118. case 0x36:
  119. case 0xB6:
  120. //ignore fake shift
  121. state = XT_STATE_INIT;
  122. break;
  123. default:
  124. if (code < 0x80) {
  125. matrix_make(move_e0code(code));
  126. } else {
  127. matrix_break(move_e0code(code & 0x7F));
  128. }
  129. state = XT_STATE_INIT;
  130. break;
  131. }
  132. break;
  133. case XT_STATE_E1:
  134. switch (code) {
  135. case 0x1D:
  136. state = XT_STATE_E1_1D;
  137. break;
  138. case 0x9D:
  139. state = XT_STATE_E1_9D;
  140. break;
  141. default:
  142. state = XT_STATE_INIT;
  143. break;
  144. }
  145. break;
  146. case XT_STATE_E1_1D:
  147. switch (code) {
  148. case 0x45:
  149. matrix_make(0x55);
  150. break;
  151. default:
  152. state = XT_STATE_INIT;
  153. break;
  154. }
  155. break;
  156. case XT_STATE_E1_9D:
  157. switch (code) {
  158. case 0x45:
  159. matrix_break(0x55);
  160. break;
  161. default:
  162. state = XT_STATE_INIT;
  163. break;
  164. }
  165. break;
  166. default:
  167. state = XT_STATE_INIT;
  168. }
  169. matrix_scan_kb();
  170. return 1;
  171. }
  172. inline
  173. uint8_t matrix_get_row(uint8_t row) {
  174. return matrix[row];
  175. }
  176. inline static void matrix_make(uint8_t code) {
  177. if (!matrix_is_on(ROW(code), COL(code))) {
  178. matrix[ROW(code)] |= 1 << COL(code);
  179. }
  180. }
  181. inline static void matrix_break(uint8_t code) {
  182. if (matrix_is_on(ROW(code), COL(code))) {
  183. matrix[ROW(code)] &= ~(1 << COL(code));
  184. }
  185. }
  186. void matrix_clear(void) {
  187. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  188. }
  189. bool matrix_is_on(uint8_t row, uint8_t col) {
  190. return (matrix_get_row(row) & (1 << col));
  191. }
  192. #if (MATRIX_COLS <= 8)
  193. # define print_matrix_header() print("\nr/c 01234567\n")
  194. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  195. #elif (MATRIX_COLS <= 16)
  196. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  197. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  198. #elif (MATRIX_COLS <= 32)
  199. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  200. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  201. #endif
  202. void matrix_print(void) {
  203. print_matrix_header();
  204. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  205. print_hex8(row);
  206. print(": ");
  207. print_matrix_row(row);
  208. print("\n");
  209. }
  210. }
  211. /*
  212. XT Scancodes
  213. ============
  214. - http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/translate.pdf
  215. - https://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
  216. 01-53: Normal codes used in original XT keyboard
  217. 54-7F: Not used in original XT keyboard
  218. 0 1 2 3 4 5 6 7 8 9 A B C D E F
  219. 50 - - - - * * x x x x * * * o o o
  220. 60 * * * * x x x x x x x x x x x *
  221. 70 x * * x * * x * * x * x * x x *
  222. -: codes existed in original XT keyboard
  223. *: E0-escaped codes converted into unused code area(internal use in TMK)
  224. x: Non-espcaped codes(not used in real keyboards probably, for CodeSet2-CodeSet1 translation purpose)
  225. o: reserved
  226. Usage in TMK:
  227. 00 (reserved) DO NOT USE
  228. 54 PrintScr*
  229. 55 Pause*
  230. 56 Euro2
  231. 57 F11
  232. 58 F12
  233. 59 Keypad =
  234. 5A LGUI*
  235. 5B RGUI*
  236. 5C APP*
  237. 5D (reserved)
  238. 5E (reserved)
  239. 5F (reserved)
  240. 60 cursor*
  241. 61 cursor*
  242. 62 cursor*
  243. 63 cursor*
  244. 64 F13
  245. 65 F14
  246. 66 F15
  247. 67 F16
  248. 68 F17
  249. 69 F18
  250. 6A F19
  251. 6B F20
  252. 6C F21
  253. 6D F22
  254. 6E F23
  255. 6F Keypad Enter*
  256. 70 KANA
  257. 71 nav*
  258. 72 nav*
  259. 73 RO
  260. 74 nav*
  261. 75 nav*
  262. 76 F24
  263. 77 nav*
  264. 78 nav*
  265. 79 HENKAN
  266. 7A RCTL*
  267. 7B MUHENKAN
  268. 7C RALT*
  269. 7D JPY
  270. 7E Keypad ,
  271. 7F Keypad / *
  272. */