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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 uint8_t matrix[MATRIX_ROWS];
  26. #define ROW(code) (code>>3)
  27. #define COL(code) (code&0x07)
  28. // matrix positions for exceptional keys
  29. #define PRINT_SCREEN (0x7C)
  30. #define PAUSE (0x7D)
  31. void matrix_init(void)
  32. {
  33. debug_enable = true;
  34. xt_host_init();
  35. // initialize matrix state: all keys off
  36. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  37. return;
  38. }
  39. static uint8_t move_codes(uint8_t code) {
  40. switch(code) {
  41. case 0x10:
  42. code += 0x5E;
  43. break;
  44. case 0x19:
  45. code += 0x41;
  46. break;
  47. case 0x1C:
  48. case 0x1D:
  49. code += 0x38;
  50. break;
  51. case 0x20:
  52. case 0x21:
  53. case 0x22:
  54. case 0x24:
  55. code += 0x40;
  56. break;
  57. case 0x2E:
  58. case 0x30:
  59. case 0x32:
  60. code += 0x44;
  61. break;
  62. case 0x35:
  63. case 0x38:
  64. code += 0x21;
  65. break;
  66. case 0x47:
  67. case 0x48:
  68. case 0x49:
  69. case 0x4B:
  70. case 0x4D:
  71. case 0x4F:
  72. case 0x50:
  73. case 0x51:
  74. case 0x52:
  75. case 0x53:
  76. code += 0x28;
  77. break;
  78. }
  79. return code;
  80. }
  81. uint8_t matrix_scan(void)
  82. {
  83. // scan code reading states
  84. static enum {
  85. INIT,
  86. E0,
  87. E0_2A,
  88. E0_2A_E0,
  89. E0_B7,
  90. E0_B7_E0,
  91. // print screen
  92. E1,
  93. E1_1D,
  94. E1_1D_45,
  95. E1_1D_45_E1,
  96. E1_1D_45_E1_9D,
  97. // pause
  98. } state = INIT;
  99. // 'pseudo break code' hack
  100. if (matrix_is_on(ROW(PAUSE), COL(PAUSE))) {
  101. matrix_break(PAUSE);
  102. }
  103. uint8_t code = xt_host_recv();
  104. switch (state) {
  105. case INIT:
  106. switch (code) {
  107. case 0xE0:
  108. state = E0;
  109. break;
  110. case 0xE1:
  111. state = E1;
  112. break;
  113. default: // normal key make
  114. if (code < 0x80 && code != 0x00) {
  115. xprintf("make: %X\r\n", code);
  116. matrix_make(code);
  117. } else if (code > 0x80 && code < 0xFF && code != 0x00) {
  118. xprintf("break %X\r\n", code);
  119. matrix_break(code - 0x80);
  120. }
  121. state = INIT;
  122. }
  123. break;
  124. case E0: // E0-Prefixed
  125. switch (code) { //move these codes to unused places on the matrix
  126. case 0x2A:
  127. state = E0_2A;
  128. break;
  129. case 0xB7:
  130. state = E0_B7;
  131. break;
  132. default:
  133. if (code < 0x80 && code != 0x00) {
  134. matrix_make(move_codes(code));
  135. } else if (code > 0x80 && code < 0xFF && code != 0x00) {
  136. matrix_break(move_codes(code - 0x80));
  137. }
  138. state = INIT;
  139. }
  140. break;
  141. case E0_2A:
  142. if(code == 0xE0)
  143. state = E0_2A_E0;
  144. else
  145. state = INIT;
  146. break;
  147. case E0_2A_E0:
  148. if(code == 0x37)
  149. matrix_make(PRINT_SCREEN);
  150. else
  151. state = INIT;
  152. break;
  153. case E0_B7:
  154. if(code == 0xE0)
  155. state = E0_B7;
  156. else
  157. state = INIT;
  158. break;
  159. case E0_B7_E0:
  160. if(code == 0xAA)
  161. matrix_break(PRINT_SCREEN);
  162. else
  163. state = INIT;
  164. break;
  165. case E1:
  166. if (code == 0x1D)
  167. state = E1_1D;
  168. else
  169. state = INIT;
  170. break;
  171. case E1_1D:
  172. if(code == 0x45)
  173. state = E1_1D_45;
  174. else
  175. state = INIT;
  176. break;
  177. case E1_1D_45:
  178. if(code == 0xE1)
  179. state = E1_1D_45_E1;
  180. else
  181. state = INIT;
  182. break;
  183. case E1_1D_45_E1:
  184. if(code == 0x9D)
  185. state = E1_1D_45_E1_9D;
  186. else
  187. state = INIT;
  188. break;
  189. case E1_1D_45_E1_9D:
  190. if(code == 0xC5)
  191. matrix_make(PAUSE);
  192. else
  193. state = INIT;
  194. break;
  195. default:
  196. state = INIT;
  197. }
  198. return 1;
  199. }
  200. inline
  201. uint8_t matrix_get_row(uint8_t row)
  202. {
  203. return matrix[row];
  204. }
  205. inline
  206. static void matrix_make(uint8_t code)
  207. {
  208. if (!matrix_is_on(ROW(code), COL(code))) {
  209. matrix[ROW(code)] |= 1<<COL(code);
  210. }
  211. }
  212. inline
  213. static void matrix_break(uint8_t code)
  214. {
  215. if (matrix_is_on(ROW(code), COL(code))) {
  216. matrix[ROW(code)] &= ~(1<<COL(code));
  217. }
  218. }
  219. void matrix_clear(void)
  220. {
  221. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  222. }