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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #include "led.h"
  29. static bool has_media_keys = false;
  30. static bool is_iso_layout = false;
  31. static report_mouse_t mouse_report = {};
  32. // matrix state buffer(1:on, 0:off)
  33. static matrix_row_t matrix[MATRIX_ROWS];
  34. static void register_key(uint8_t key);
  35. void matrix_init(void)
  36. {
  37. // LED on
  38. DDRD |= (1<<6); PORTD |= (1<<6);
  39. adb_host_init();
  40. // wait for keyboard to boot up and receive command
  41. _delay_ms(2000);
  42. // device scan
  43. xprintf("Before init:\n");
  44. for (uint8_t addr = 1; addr < 16; addr++) {
  45. uint16_t reg3 = adb_host_talk(addr, ADB_REG_3);
  46. if (reg3) {
  47. xprintf("Scan: addr:%d, reg3:%04X\n", addr, reg3);
  48. }
  49. _delay_ms(20);
  50. }
  51. // Determine ISO keyboard by handler id
  52. // http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L815
  53. uint16_t handler_id = adb_host_talk(ADB_ADDR_KEYBOARD, ADB_REG_3);
  54. switch (handler_id) {
  55. case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D:
  56. case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1:
  57. case 0xC4: case 0xC7:
  58. is_iso_layout = true;
  59. break;
  60. default:
  61. is_iso_layout = false;
  62. break;
  63. }
  64. // Adjustable keyboard media keys: address=0x07 and handlerID=0x02
  65. has_media_keys = (0x02 == (adb_host_talk(ADB_ADDR_APPLIANCE, ADB_REG_3) & 0xff));
  66. if (has_media_keys) {
  67. xprintf("Found: media keys\n");
  68. }
  69. // Enable keyboard left/right modifier distinction
  70. // Listen Register3
  71. // upper byte: reserved bits 0000, keyboard address 0010
  72. // lower byte: device handler 00000011
  73. adb_host_listen(ADB_ADDR_KEYBOARD, ADB_REG_3, ADB_ADDR_KEYBOARD, ADB_HANDLER_EXTENDED_PROTOCOL);
  74. // device scan
  75. xprintf("After init:\n");
  76. for (uint8_t addr = 1; addr < 16; addr++) {
  77. uint16_t reg3 = adb_host_talk(addr, ADB_REG_3);
  78. if (reg3) {
  79. xprintf("Scan: addr:%d, reg3:%04X\n", addr, reg3);
  80. }
  81. _delay_ms(20);
  82. }
  83. // initialize matrix state: all keys off
  84. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  85. led_set(host_keyboard_leds());
  86. debug_enable = true;
  87. //debug_matrix = true;
  88. //debug_keyboard = true;
  89. //debug_mouse = true;
  90. print("debug enabled.\n");
  91. // LED off
  92. DDRD |= (1<<6); PORTD &= ~(1<<6);
  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. codes = extra_key;
  162. extra_key = 0xFFFF;
  163. if ( codes == 0xFFFF )
  164. {
  165. _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
  166. codes = adb_host_kbd_recv(ADB_ADDR_KEYBOARD);
  167. // Adjustable keybaord media keys
  168. if (codes == 0 && has_media_keys &&
  169. (codes = adb_host_kbd_recv(ADB_ADDR_APPLIANCE))) {
  170. // key1
  171. switch (codes & 0x7f ) {
  172. case 0x00: // Mic
  173. codes = (codes & ~0x007f) | 0x42;
  174. break;
  175. case 0x01: // Mute
  176. codes = (codes & ~0x007f) | 0x4a;
  177. break;
  178. case 0x02: // Volume down
  179. codes = (codes & ~0x007f) | 0x49;
  180. break;
  181. case 0x03: // Volume Up
  182. codes = (codes & ~0x007f) | 0x48;
  183. break;
  184. case 0x7F: // no code
  185. break;
  186. default:
  187. xprintf("ERROR: media key1\n");
  188. return 0x11;
  189. }
  190. // key0
  191. switch ((codes >> 8) & 0x7f ) {
  192. case 0x00: // Mic
  193. codes = (codes & ~0x7f00) | (0x42 << 8);
  194. break;
  195. case 0x01: // Mute
  196. codes = (codes & ~0x7f00) | (0x4a << 8);
  197. break;
  198. case 0x02: // Volume down
  199. codes = (codes & ~0x7f00) | (0x49 << 8);
  200. break;
  201. case 0x03: // Volume Up
  202. codes = (codes & ~0x7f00) | (0x48 << 8);
  203. break;
  204. default:
  205. xprintf("ERROR: media key0\n");
  206. return 0x10;
  207. }
  208. }
  209. }
  210. key0 = codes>>8;
  211. key1 = codes&0xFF;
  212. if (debug_matrix && codes) {
  213. print("adb_host_kbd_recv: "); phex16(codes); print("\n");
  214. }
  215. if (codes == 0) { // no keys
  216. return 0;
  217. } else if (codes == 0x7F7F) { // power key press
  218. register_key(0x7F);
  219. } else if (codes == 0xFFFF) { // power key release
  220. register_key(0xFF);
  221. } else if (key0 == 0xFF) { // error
  222. xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
  223. // something wrong or plug-in
  224. matrix_init();
  225. return key1;
  226. } else {
  227. /* Swap codes for ISO keyboard
  228. * https://github.com/tmk/tmk_keyboard/issues/35
  229. *
  230. * ANSI
  231. * ,----------- ----------.
  232. * | *a| 1| 2 =|Backspa|
  233. * |----------- ----------|
  234. * |Tab | Q| | ]| *c|
  235. * |----------- ----------|
  236. * |CapsLo| A| '|Return |
  237. * |----------- ----------|
  238. * |Shift | Shift |
  239. * `----------- ----------'
  240. *
  241. * ISO
  242. * ,----------- ----------.
  243. * | *a| 1| 2 =|Backspa|
  244. * |----------- ----------|
  245. * |Tab | Q| | ]|Retur|
  246. * |----------- -----` |
  247. * |CapsLo| A| '| *c| |
  248. * |----------- ----------|
  249. * |Shif| *b| Shift |
  250. * `----------- ----------'
  251. *
  252. * ADB scan code USB usage
  253. * ------------- ---------
  254. * Key ANSI ISO ANSI ISO
  255. * ---------------------------------------------
  256. * *a 0x32 0x0A 0x35 0x35
  257. * *b ---- 0x32 ---- 0x64
  258. * *c 0x2A 0x2A 0x31 0x31(or 0x32)
  259. */
  260. if (is_iso_layout) {
  261. if ((key0 & 0x7F) == 0x32) {
  262. key0 = (key0 & 0x80) | 0x0A;
  263. } else if ((key0 & 0x7F) == 0x0A) {
  264. key0 = (key0 & 0x80) | 0x32;
  265. }
  266. }
  267. register_key(key0);
  268. if (key1 != 0xFF) // key1 is 0xFF when no second key.
  269. extra_key = key1<<8 | 0xFF; // process in a separate call
  270. }
  271. return 1;
  272. }
  273. inline
  274. matrix_row_t matrix_get_row(uint8_t row)
  275. {
  276. return matrix[row];
  277. }
  278. inline
  279. static void register_key(uint8_t key)
  280. {
  281. uint8_t col, row;
  282. col = key&0x07;
  283. row = (key>>3)&0x0F;
  284. if (key&0x80) {
  285. matrix[row] &= ~(1<<col);
  286. } else {
  287. matrix[row] |= (1<<col);
  288. }
  289. }