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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

matrix.c 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. Copyright 2011 Jun Wako <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*
  15. * scan matrix
  16. */
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. #include <avr/io.h>
  20. #include <util/delay.h>
  21. #include "print.h"
  22. #include "util.h"
  23. #include "debug.h"
  24. #include "adb.h"
  25. #include "matrix.h"
  26. #include "report.h"
  27. #include "host.h"
  28. #if (MATRIX_COLS > 16)
  29. # error "MATRIX_COLS must not exceed 16"
  30. #endif
  31. #if (MATRIX_ROWS > 255)
  32. # error "MATRIX_ROWS must not exceed 255"
  33. #endif
  34. static bool is_iso_layout = false;
  35. static bool is_modified = false;
  36. static report_mouse_t mouse_report = {};
  37. // matrix state buffer(1:on, 0:off)
  38. #if (MATRIX_COLS <= 8)
  39. static uint8_t matrix[MATRIX_ROWS];
  40. #else
  41. static uint16_t matrix[MATRIX_ROWS];
  42. #endif
  43. #ifdef MATRIX_HAS_GHOST
  44. static bool matrix_has_ghost_in_row(uint8_t row);
  45. #endif
  46. static void register_key(uint8_t key);
  47. inline
  48. uint8_t matrix_rows(void)
  49. {
  50. return MATRIX_ROWS;
  51. }
  52. inline
  53. uint8_t matrix_cols(void)
  54. {
  55. return MATRIX_COLS;
  56. }
  57. void matrix_init(void)
  58. {
  59. adb_host_init();
  60. // wait for keyboard to boot up and receive command
  61. _delay_ms(1000);
  62. // Determine ISO keyboard by handler id
  63. // http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L815
  64. uint16_t handler_id = adb_host_talk(ADB_ADDR_KEYBOARD, 3);
  65. switch (handler_id) {
  66. case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D:
  67. case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1:
  68. case 0xC4: case 0xC7:
  69. is_iso_layout = true;
  70. break;
  71. default:
  72. is_iso_layout = false;
  73. break;
  74. }
  75. // Enable keyboard left/right modifier distinction
  76. // Addr:Keyboard(0010), Cmd:Listen(10), Register3(11)
  77. // upper byte: reserved bits 0000, device address 0010
  78. // lower byte: device handler 00000011
  79. adb_host_listen(0x2B,0x02,0x03);
  80. // initialize matrix state: all keys off
  81. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  82. debug_enable = true;
  83. //debug_matrix = true;
  84. //debug_keyboard = true;
  85. //debug_mouse = true;
  86. print("debug enabled.\n");
  87. // LED flash
  88. DDRD |= (1<<6); PORTD |= (1<<6);
  89. _delay_ms(500);
  90. DDRD |= (1<<6); PORTD &= ~(1<<6);
  91. uint16_t handler_id2 = adb_host_talk(ADB_ADDR_KEYBOARD, 3);
  92. xprintf("handler_id: %02X -> %02X\n", (handler_id & 0xff), (handler_id2 & 0xff));
  93. return;
  94. }
  95. #ifdef ADB_MOUSE_ENABLE
  96. #ifdef MAX
  97. #undef MAX
  98. #endif
  99. #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
  100. void adb_mouse_task(void)
  101. {
  102. uint16_t codes;
  103. int16_t x, y;
  104. static int8_t mouseacc;
  105. _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
  106. codes = adb_host_mouse_recv();
  107. // If nothing received reset mouse acceleration, and quit.
  108. if (!codes) {
  109. mouseacc = 1;
  110. return;
  111. };
  112. // Bit sixteen is button.
  113. if (~codes & (1 << 15))
  114. mouse_report.buttons |= MOUSE_BTN1;
  115. if (codes & (1 << 15))
  116. mouse_report.buttons &= ~MOUSE_BTN1;
  117. // lower seven bits are movement, as signed int_7.
  118. // low byte is X-axis, high byte is Y.
  119. y = (codes>>8 & 0x3F);
  120. x = (codes>>0 & 0x3F);
  121. // bit seven and fifteen is negative
  122. // usb does not use int_8, but int_7 (measuring distance) with sign-bit.
  123. if (codes & (1 << 6))
  124. x = (x-0x40);
  125. if (codes & (1 << 14))
  126. y = (y-0x40);
  127. // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
  128. x *= mouseacc;
  129. y *= mouseacc;
  130. // Cap our two bytes per axis to one byte.
  131. // Easier with a MIN-function, but since -MAX(-a,-b) = MIN(a,b)...
  132. // I.E. MIN(MAX(x,-127),127) = -MAX(-MAX(x, -127), -127) = MIN(-MIN(-x,127),127)
  133. mouse_report.x = -MAX(-MAX(x, -127), -127);
  134. mouse_report.y = -MAX(-MAX(y, -127), -127);
  135. if (debug_mouse) {
  136. print("adb_host_mouse_recv: "); print_bin16(codes); print("\n");
  137. print("adb_mouse raw: [");
  138. phex(mouseacc); print(" ");
  139. phex(mouse_report.buttons); print("|");
  140. print_decs(mouse_report.x); print(" ");
  141. print_decs(mouse_report.y); print("]\n");
  142. }
  143. // Send result by usb.
  144. host_mouse_send(&mouse_report);
  145. // increase acceleration of mouse
  146. mouseacc += ( mouseacc < ADB_MOUSE_MAXACC ? 1 : 0 );
  147. return;
  148. }
  149. #endif
  150. uint8_t matrix_scan(void)
  151. {
  152. /* extra_key is volatile and more convoluted than necessary because gcc refused
  153. to generate valid code otherwise. Making extra_key uint8_t and constructing codes
  154. here via codes = extra_key<<8 | 0xFF; would consistently fail to even LOAD
  155. extra_key from memory, and leave garbage in the high byte of codes. I tried
  156. dozens of code variations and it kept generating broken assembly output. So
  157. beware if attempting to make extra_key code more logical and efficient. */
  158. static volatile uint16_t extra_key = 0xFFFF;
  159. uint16_t codes;
  160. uint8_t key0, key1;
  161. is_modified = false;
  162. codes = extra_key;
  163. extra_key = 0xFFFF;
  164. if ( codes == 0xFFFF )
  165. {
  166. _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
  167. codes = adb_host_kbd_recv();
  168. }
  169. key0 = codes>>8;
  170. key1 = codes&0xFF;
  171. if (debug_matrix && codes) {
  172. print("adb_host_kbd_recv: "); phex16(codes); print("\n");
  173. }
  174. if (codes == 0) { // no keys
  175. return 0;
  176. } else if (codes == 0x7F7F) { // power key press
  177. register_key(0x7F);
  178. } else if (codes == 0xFFFF) { // power key release
  179. register_key(0xFF);
  180. } else if (key0 == 0xFF) { // error
  181. xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
  182. // something wrong or plug-in
  183. matrix_init();
  184. return key1;
  185. } else {
  186. /* Swap codes for ISO keyboard
  187. * https://github.com/tmk/tmk_keyboard/issues/35
  188. *
  189. * ANSI
  190. * ,----------- ----------.
  191. * | *a| 1| 2 =|Backspa|
  192. * |----------- ----------|
  193. * |Tab | Q| | ]| *c|
  194. * |----------- ----------|
  195. * |CapsLo| A| '|Return |
  196. * |----------- ----------|
  197. * |Shift | Shift |
  198. * `----------- ----------'
  199. *
  200. * ISO
  201. * ,----------- ----------.
  202. * | *a| 1| 2 =|Backspa|
  203. * |----------- ----------|
  204. * |Tab | Q| | ]|Retur|
  205. * |----------- -----` |
  206. * |CapsLo| A| '| *c| |
  207. * |----------- ----------|
  208. * |Shif| *b| Shift |
  209. * `----------- ----------'
  210. *
  211. * ADB scan code USB usage
  212. * ------------- ---------
  213. * Key ANSI ISO ANSI ISO
  214. * ---------------------------------------------
  215. * *a 0x32 0x0A 0x35 0x35
  216. * *b ---- 0x32 ---- 0x64
  217. * *c 0x2A 0x2A 0x31 0x31(or 0x32)
  218. */
  219. if (is_iso_layout) {
  220. if ((key0 & 0x7F) == 0x32) {
  221. key0 = (key0 & 0x80) | 0x0A;
  222. } else if ((key0 & 0x7F) == 0x0A) {
  223. key0 = (key0 & 0x80) | 0x32;
  224. }
  225. }
  226. register_key(key0);
  227. if (key1 != 0xFF) // key1 is 0xFF when no second key.
  228. extra_key = key1<<8 | 0xFF; // process in a separate call
  229. }
  230. return 1;
  231. }
  232. bool matrix_is_modified(void)
  233. {
  234. return is_modified;
  235. }
  236. inline
  237. bool matrix_has_ghost(void)
  238. {
  239. #ifdef MATRIX_HAS_GHOST
  240. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  241. if (matrix_has_ghost_in_row(i))
  242. return true;
  243. }
  244. #endif
  245. return false;
  246. }
  247. inline
  248. bool matrix_is_on(uint8_t row, uint8_t col)
  249. {
  250. return (matrix[row] & (1<<col));
  251. }
  252. inline
  253. #if (MATRIX_COLS <= 8)
  254. uint8_t matrix_get_row(uint8_t row)
  255. #else
  256. uint16_t matrix_get_row(uint8_t row)
  257. #endif
  258. {
  259. return matrix[row];
  260. }
  261. void matrix_print(void)
  262. {
  263. if (!debug_matrix) return;
  264. #if (MATRIX_COLS <= 8)
  265. print("r/c 01234567\n");
  266. #else
  267. print("r/c 0123456789ABCDEF\n");
  268. #endif
  269. for (uint8_t row = 0; row < matrix_rows(); row++) {
  270. phex(row); print(": ");
  271. #if (MATRIX_COLS <= 8)
  272. pbin_reverse(matrix_get_row(row));
  273. #else
  274. pbin_reverse16(matrix_get_row(row));
  275. #endif
  276. #ifdef MATRIX_HAS_GHOST
  277. if (matrix_has_ghost_in_row(row)) {
  278. print(" <ghost");
  279. }
  280. #endif
  281. print("\n");
  282. }
  283. }
  284. uint8_t matrix_key_count(void)
  285. {
  286. uint8_t count = 0;
  287. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  288. #if (MATRIX_COLS <= 8)
  289. count += bitpop(matrix[i]);
  290. #else
  291. count += bitpop16(matrix[i]);
  292. #endif
  293. }
  294. return count;
  295. }
  296. #ifdef MATRIX_HAS_GHOST
  297. inline
  298. static bool matrix_has_ghost_in_row(uint8_t row)
  299. {
  300. // no ghost exists in case less than 2 keys on
  301. if (((matrix[row] - 1) & matrix[row]) == 0)
  302. return false;
  303. // ghost exists in case same state as other row
  304. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  305. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  306. return true;
  307. }
  308. return false;
  309. }
  310. #endif
  311. inline
  312. static void register_key(uint8_t key)
  313. {
  314. uint8_t col, row;
  315. col = key&0x07;
  316. row = (key>>3)&0x0F;
  317. if (key&0x80) {
  318. matrix[row] &= ~(1<<col);
  319. } else {
  320. matrix[row] |= (1<<col);
  321. }
  322. is_modified = true;
  323. }