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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 handle id
  63. // http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L815
  64. uint16_t handle_id = adb_host_talk(ADB_ADDR_KEYBOARD, 3);
  65. switch (handle_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 handle_id2 = adb_host_talk(ADB_ADDR_KEYBOARD, 3);
  92. xprintf("handle_id: %02X -> %02X\n", handle_id&0xff, handle_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. *
  188. * ANSI
  189. * ,----------- ----------.
  190. * | *a| 1| 2 =|Backspa|
  191. * |----------- ----------|
  192. * |Tab | Q| | ]| *c|
  193. * |----------- ----------|
  194. * |CapsLo| A| '|Return |
  195. * |----------- ----------|
  196. * |Shift | Shift |
  197. * `----------- ----------'
  198. *
  199. * ISO
  200. * ,----------- ----------.
  201. * | *a| 1| 2 =|Backspa|
  202. * |----------- ----------|
  203. * |Tab | Q| | ]|Retur|
  204. * |----------- -----` |
  205. * |CapsLo| A| '| *c| |
  206. * |----------- ----------|
  207. * |Shif| *b| Shift |
  208. * `----------- ----------'
  209. *
  210. * ADB scan code USB usage
  211. * ------------- ---------
  212. * Key ANSI ISO ANSI ISO
  213. * ---------------------------------------------
  214. * *a 0x32 0x0A 0x35 0x35
  215. * *b ---- 0x32 ---- 0x64
  216. * *c 0x2A 0x2A 0x31 0x31(or 0x32)
  217. */
  218. if (is_iso_layout) {
  219. if (key0 == 0x32) {
  220. key0 = 0x0A;
  221. } else if (key0 == 0x0A) {
  222. key0 = 0x32;
  223. }
  224. }
  225. register_key(key0);
  226. if (key1 != 0xFF) // key1 is 0xFF when no second key.
  227. extra_key = key1<<8 | 0xFF; // process in a separate call
  228. }
  229. return 1;
  230. }
  231. bool matrix_is_modified(void)
  232. {
  233. return is_modified;
  234. }
  235. inline
  236. bool matrix_has_ghost(void)
  237. {
  238. #ifdef MATRIX_HAS_GHOST
  239. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  240. if (matrix_has_ghost_in_row(i))
  241. return true;
  242. }
  243. #endif
  244. return false;
  245. }
  246. inline
  247. bool matrix_is_on(uint8_t row, uint8_t col)
  248. {
  249. return (matrix[row] & (1<<col));
  250. }
  251. inline
  252. #if (MATRIX_COLS <= 8)
  253. uint8_t matrix_get_row(uint8_t row)
  254. #else
  255. uint16_t matrix_get_row(uint8_t row)
  256. #endif
  257. {
  258. return matrix[row];
  259. }
  260. void matrix_print(void)
  261. {
  262. if (!debug_matrix) return;
  263. #if (MATRIX_COLS <= 8)
  264. print("r/c 01234567\n");
  265. #else
  266. print("r/c 0123456789ABCDEF\n");
  267. #endif
  268. for (uint8_t row = 0; row < matrix_rows(); row++) {
  269. phex(row); print(": ");
  270. #if (MATRIX_COLS <= 8)
  271. pbin_reverse(matrix_get_row(row));
  272. #else
  273. pbin_reverse16(matrix_get_row(row));
  274. #endif
  275. #ifdef MATRIX_HAS_GHOST
  276. if (matrix_has_ghost_in_row(row)) {
  277. print(" <ghost");
  278. }
  279. #endif
  280. print("\n");
  281. }
  282. }
  283. uint8_t matrix_key_count(void)
  284. {
  285. uint8_t count = 0;
  286. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  287. #if (MATRIX_COLS <= 8)
  288. count += bitpop(matrix[i]);
  289. #else
  290. count += bitpop16(matrix[i]);
  291. #endif
  292. }
  293. return count;
  294. }
  295. #ifdef MATRIX_HAS_GHOST
  296. inline
  297. static bool matrix_has_ghost_in_row(uint8_t row)
  298. {
  299. // no ghost exists in case less than 2 keys on
  300. if (((matrix[row] - 1) & matrix[row]) == 0)
  301. return false;
  302. // ghost exists in case same state as other row
  303. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  304. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  305. return true;
  306. }
  307. return false;
  308. }
  309. #endif
  310. inline
  311. static void register_key(uint8_t key)
  312. {
  313. uint8_t col, row;
  314. col = key&0x07;
  315. row = (key>>3)&0x0F;
  316. if (key&0x80) {
  317. matrix[row] &= ~(1<<col);
  318. } else {
  319. matrix[row] |= (1<<col);
  320. }
  321. is_modified = true;
  322. }