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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * scan matrix
  3. */
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. #include <avr/io.h>
  7. #include <util/delay.h>
  8. #include "print.h"
  9. #include "util.h"
  10. #include "debug.h"
  11. #include "ps2.h"
  12. #include "usb_keyboard.h"
  13. #include "matrix_skel.h"
  14. #if (MATRIX_COLS > 16)
  15. # error "MATRIX_COLS must not exceed 16"
  16. #endif
  17. #if (MATRIX_ROWS > 255)
  18. # error "MATRIX_ROWS must not exceed 255"
  19. #endif
  20. /*
  21. * Matrix usage:
  22. * "PS/2 Scan Codes Set 2" is assigned to 256(32x8)cells matrix.
  23. * Hmm, It is very sparse and not efficient :(
  24. *
  25. * 8bit
  26. * ---------
  27. * 0| |
  28. * :| XX | 00-7F for normal codes(without E0-prefix)
  29. * f|_________|
  30. * 10| |
  31. * :| E0 XX | 80-FF for E0-prefix codes(use (XX|0x80) as code)
  32. * 1f| |
  33. * ---------
  34. * exceptions:
  35. * 83: F8[0x83](normal codes but > 0x7F)
  36. * FC: PrintScreen[E0 7C or 84]
  37. * FE: Puause
  38. */
  39. #define F8 (0x83)
  40. #define PRINT_SCREEN (0xFC)
  41. #define PAUSE (0xFE)
  42. #define ROW(code) (code>>3)
  43. #define COL(code) (code&0x07)
  44. static bool is_modified = false;
  45. // matrix state buffer(1:on, 0:off)
  46. #if (MATRIX_COLS <= 8)
  47. static uint8_t matrix[MATRIX_ROWS];
  48. #else
  49. static uint16_t matrix[MATRIX_ROWS];
  50. #endif
  51. #ifdef MATRIX_HAS_GHOST
  52. static bool matrix_has_ghost_in_row(uint8_t row);
  53. #endif
  54. static void matrix_make(uint8_t code);
  55. static void matrix_break(uint8_t code);
  56. inline
  57. uint8_t matrix_rows(void)
  58. {
  59. return MATRIX_ROWS;
  60. }
  61. inline
  62. uint8_t matrix_cols(void)
  63. {
  64. return MATRIX_COLS;
  65. }
  66. void matrix_init(void)
  67. {
  68. ps2_host_init();
  69. // initialize matrix state: all keys off
  70. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  71. return;
  72. }
  73. /*
  74. * PS/2 Scan Code Set 2: Exceptional Handling
  75. *
  76. * There are several keys to be handled exceptionally.
  77. * The scan code for these keys are varied or prefix/postfix'd
  78. * depending on modifier key state.
  79. *
  80. * References:
  81. * http://www.microsoft.com/whdc/archive/scancode.mspx
  82. * http://download.microsoft.com/download/1/6/1/161ba512-40e2-4cc9-843a-923143f3456c/scancode.doc
  83. *
  84. *
  85. * Insert, Delete, Home, End, PageUp, PageDown, Up, Down, Right, Left:
  86. * Num Lock: off
  87. * modifiers | make | break
  88. * ----------+---------------------------+----------------------
  89. * Ohter | <make> | <break>
  90. * LShift | E0 F0 12 <make> | <break> E0 12
  91. * RShift | E0 F0 59 <make> | <break> E0 59
  92. * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
  93. *
  94. * Num Lock: on
  95. * modifiers | make | break
  96. * ----------+---------------------------+----------------------
  97. * Other | E0 12 <make> | <break> E0 F0 12
  98. * Shift'd | <make> | <break>
  99. *
  100. * Handling: ignore these prefix/postfix codes
  101. *
  102. *
  103. * Keypad-/:
  104. * modifiers | make | break
  105. * ----------+---------------------------+----------------------
  106. * Ohter | <make> | <break>
  107. * LShift | E0 F0 12 <make> | <break> E0 12
  108. * RShift | E0 F0 59 <make> | <break> E0 59
  109. * L+RShift | E0 F0 12 E0 F0 59 <make> | <break> E0 59 E0 12
  110. *
  111. * Handling: ignore these prefix/postfix codes
  112. *
  113. *
  114. * PrintScreen:
  115. * With hoding down modifiers, the scan code is sent as following:
  116. *
  117. * modifiers | make | break
  118. * ----------+--------------+-----------------------------------
  119. * Other | E0 12 E0 7C | E0 F0 7C E0 F0 12
  120. * Shift'd | E0 7C | E0 F0 7C
  121. * Control'd | E0 7C | E0 F0 7C
  122. * Alt'd | 84 | F0 84
  123. *
  124. * Handling: ignore prefix/postfix codes and treat both scan code
  125. * E0 7C and 84 as PrintScreen.
  126. *
  127. * Pause:
  128. * With hoding down modifiers, the scan code is sent as following:
  129. *
  130. * modifiers | make(no break code)
  131. * ----------+--------------------------------------------------
  132. * no mods | E1 14 77 E1 F0 14 F0 77
  133. * Control'd | E0 7E E0 F0 7E
  134. *
  135. * Handling: treat these two code sequence as Pause
  136. *
  137. */
  138. uint8_t matrix_scan(void)
  139. {
  140. static enum {
  141. INIT,
  142. F0,
  143. E0,
  144. E0_F0,
  145. // states for Pause/Break
  146. E1,
  147. E1_14,
  148. E1_14_77,
  149. E1_14_77_E1,
  150. E1_14_77_E1_F0,
  151. E1_14_77_E1_F0_14,
  152. E1_14_77_E1_F0_14_F0,
  153. } state = INIT;
  154. is_modified = false;
  155. // Pause/Break off(PS/2 has no break for this key)
  156. if (matrix_is_on(ROW(PAUSE), COL(PAUSE))) {
  157. matrix_break(PAUSE);
  158. }
  159. uint8_t code;
  160. while ((code = ps2_host_recv())) {
  161. switch (state) {
  162. case INIT:
  163. switch (code) {
  164. case 0xE0: // 2byte make
  165. state = E0;
  166. break;
  167. case 0xF0: // break code
  168. state = F0;
  169. break;
  170. case 0xE1: // Pause/Break
  171. state = E1;
  172. break;
  173. case 0x83: // F8
  174. matrix_make(F8);
  175. state = INIT;
  176. break;
  177. case 0x84: // PrintScreen
  178. matrix_make(PRINT_SCREEN);
  179. state = INIT;
  180. break;
  181. default: // normal key make
  182. if (code < 0x80) {
  183. matrix_make(code);
  184. } else {
  185. debug("unexpected scan code at INIT: "); debug_hex(code); debug("\n");
  186. }
  187. state = INIT;
  188. }
  189. break;
  190. case E0:
  191. switch (code) {
  192. case 0x12: // postfix/postfix code for exceptional keys
  193. case 0x59: // postfix/postfix code for exceptional keys
  194. // ignore
  195. state = INIT;
  196. break;
  197. case 0x7E: // former part of Control-Pause[E0 7E E0 F0 7E]
  198. matrix_make(PAUSE);
  199. state = INIT;
  200. break;
  201. case 0xF0: // E0 break
  202. state = E0_F0;
  203. break;
  204. default: // E0 make
  205. if (code < 0x80) {
  206. matrix_make(code|0x80);
  207. } else {
  208. debug("unexpected scan code at E0: "); debug_hex(code); debug("\n");
  209. }
  210. state = INIT;
  211. }
  212. break;
  213. case F0:
  214. switch (code) {
  215. case 0x83:
  216. matrix_break(F8);
  217. state = INIT;
  218. break;
  219. case 0x84:
  220. matrix_break(PRINT_SCREEN);
  221. state = INIT;
  222. break;
  223. default:
  224. if (code < 0x80) {
  225. matrix_break(code);
  226. } else {
  227. debug("unexpected scan code at F0: "); debug_hex(code); debug("\n");
  228. }
  229. state = INIT;
  230. }
  231. break;
  232. case E0_F0: // E0 break
  233. switch (code) {
  234. case 0x12: // postfix/postfix code for exceptional keys
  235. case 0x59: // postfix/postfix code for exceptional keys
  236. case 0x7E: // latter part of Control-Pause[E0 7E E0 F0 7E]
  237. // ignore
  238. state = INIT;
  239. break;
  240. default:
  241. if (code < 0x80) {
  242. matrix_break(code|0x80);
  243. } else {
  244. debug("unexpected scan code at E0_F0: "); debug_hex(code); debug("\n");
  245. }
  246. state = INIT;
  247. }
  248. break;
  249. /* Pause */
  250. case E1:
  251. switch (code) {
  252. case 0x14:
  253. state = E1_14;
  254. break;
  255. default:
  256. state = INIT;
  257. }
  258. break;
  259. case E1_14:
  260. switch (code) {
  261. case 0x77:
  262. state = E1_14_77;
  263. break;
  264. default:
  265. state = INIT;
  266. }
  267. break;
  268. case E1_14_77:
  269. switch (code) {
  270. case 0xE1:
  271. state = E1_14_77_E1;
  272. break;
  273. default:
  274. state = INIT;
  275. }
  276. break;
  277. case E1_14_77_E1:
  278. switch (code) {
  279. case 0xF0:
  280. state = E1_14_77_E1_F0;
  281. break;
  282. default:
  283. state = INIT;
  284. }
  285. break;
  286. case E1_14_77_E1_F0:
  287. switch (code) {
  288. case 0x14:
  289. state = E1_14_77_E1_F0_14;
  290. break;
  291. default:
  292. state = INIT;
  293. }
  294. break;
  295. case E1_14_77_E1_F0_14:
  296. switch (code) {
  297. case 0xF0:
  298. state = E1_14_77_E1_F0_14_F0;
  299. break;
  300. default:
  301. state = INIT;
  302. }
  303. break;
  304. case E1_14_77_E1_F0_14_F0:
  305. switch (code) {
  306. case 0x77:
  307. matrix_make(PAUSE);
  308. state = INIT;
  309. break;
  310. default:
  311. state = INIT;
  312. }
  313. break;
  314. default:
  315. state = INIT;
  316. }
  317. }
  318. return 1;
  319. }
  320. bool matrix_is_modified(void)
  321. {
  322. return is_modified;
  323. }
  324. inline
  325. bool matrix_has_ghost(void)
  326. {
  327. #ifdef MATRIX_HAS_GHOST
  328. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  329. if (matrix_has_ghost_in_row(i))
  330. return true;
  331. }
  332. #endif
  333. return false;
  334. }
  335. inline
  336. bool matrix_is_on(uint8_t row, uint8_t col)
  337. {
  338. return (matrix[row] & (1<<col));
  339. }
  340. inline
  341. #if (MATRIX_COLS <= 8)
  342. uint8_t matrix_get_row(uint8_t row)
  343. #else
  344. uint16_t matrix_get_row(uint8_t row)
  345. #endif
  346. {
  347. return matrix[row];
  348. }
  349. void matrix_print(void)
  350. {
  351. #if (MATRIX_COLS <= 8)
  352. print("\nr/c 01234567\n");
  353. #else
  354. print("\nr/c 0123456789ABCDEF\n");
  355. #endif
  356. for (uint8_t row = 0; row < matrix_rows(); row++) {
  357. phex(row); print(": ");
  358. #if (MATRIX_COLS <= 8)
  359. pbin_reverse(matrix_get_row(row));
  360. #else
  361. pbin_reverse16(matrix_get_row(row));
  362. #endif
  363. #ifdef MATRIX_HAS_GHOST
  364. if (matrix_has_ghost_in_row(row)) {
  365. print(" <ghost");
  366. }
  367. #endif
  368. print("\n");
  369. }
  370. }
  371. uint8_t matrix_key_count(void)
  372. {
  373. uint8_t count = 0;
  374. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  375. #if (MATRIX_COLS <= 8)
  376. count += bitpop(matrix[i]);
  377. #else
  378. count += bitpop16(matrix[i]);
  379. #endif
  380. }
  381. return count;
  382. }
  383. #ifdef MATRIX_HAS_GHOST
  384. inline
  385. static bool matrix_has_ghost_in_row(uint8_t row)
  386. {
  387. // no ghost exists in case less than 2 keys on
  388. if (((matrix[row] - 1) & matrix[row]) == 0)
  389. return false;
  390. // ghost exists in case same state as other row
  391. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  392. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  393. return true;
  394. }
  395. return false;
  396. }
  397. #endif
  398. inline
  399. static void matrix_make(uint8_t code)
  400. {
  401. if (!matrix_is_on(ROW(code), COL(code))) {
  402. matrix[ROW(code)] |= 1<<COL(code);
  403. is_modified = true;
  404. //print("matrix_make: "); phex(code); print("\n");
  405. }
  406. }
  407. inline
  408. static void matrix_break(uint8_t code)
  409. {
  410. if (matrix_is_on(ROW(code), COL(code))) {
  411. matrix[ROW(code)] &= ~(1<<COL(code));
  412. is_modified = true;
  413. //print("matrix_break: "); phex(code); print("\n");
  414. }
  415. }