Keyboard firmwares for Atmel AVR and Cortex-M
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. static bool has_media_keys = false;
  29. static bool is_iso_layout = false;
  30. static report_mouse_t mouse_report = {};
  31. // matrix state buffer(1:on, 0:off)
  32. static matrix_row_t matrix[MATRIX_ROWS];
  33. static void register_key(uint8_t key);
  34. void matrix_init(void)
  35. {
  36. // LED on
  37. DDRD |= (1<<6); PORTD |= (1<<6);
  38. adb_host_init();
  39. // wait for keyboard to boot up and receive command
  40. _delay_ms(2000);
  41. // device scan
  42. xprintf("Before init:\n");
  43. for (uint8_t addr = 1; addr < 16; addr++) {
  44. uint16_t reg3 = adb_host_talk(addr, ADB_REG_3);
  45. if (reg3) {
  46. xprintf("Scan: addr:%d, reg3:%04X\n", addr, reg3);
  47. }
  48. _delay_ms(20);
  49. }
  50. // Determine ISO keyboard by handler id
  51. // http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L815
  52. uint16_t handler_id = adb_host_talk(ADB_ADDR_KEYBOARD, ADB_REG_3);
  53. switch (handler_id) {
  54. case 0x04: case 0x05: case 0x07: case 0x09: case 0x0D:
  55. case 0x11: case 0x14: case 0x19: case 0x1D: case 0xC1:
  56. case 0xC4: case 0xC7:
  57. is_iso_layout = true;
  58. break;
  59. default:
  60. is_iso_layout = false;
  61. break;
  62. }
  63. // Adjustable keyboard media keys: address=0x07 and handlerID=0x02
  64. has_media_keys = (0x02 == (adb_host_talk(ADB_ADDR_APPLIANCE, ADB_REG_3) & 0xff));
  65. if (has_media_keys) {
  66. xprintf("Found: media keys\n");
  67. }
  68. // Enable keyboard left/right modifier distinction
  69. // Listen Register3
  70. // upper byte: reserved bits 0000, keyboard address 0010
  71. // lower byte: device handler 00000011
  72. adb_host_listen(ADB_ADDR_KEYBOARD, ADB_REG_3, ADB_ADDR_KEYBOARD, ADB_HANDLER_EXTENDED_PROTOCOL);
  73. // device scan
  74. xprintf("After init:\n");
  75. for (uint8_t addr = 1; addr < 16; addr++) {
  76. uint16_t reg3 = adb_host_talk(addr, ADB_REG_3);
  77. if (reg3) {
  78. xprintf("Scan: addr:%d, reg3:%04X\n", addr, reg3);
  79. }
  80. _delay_ms(20);
  81. }
  82. // initialize matrix state: all keys off
  83. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  84. debug_enable = true;
  85. //debug_matrix = true;
  86. //debug_keyboard = true;
  87. //debug_mouse = true;
  88. print("debug enabled.\n");
  89. // LED off
  90. DDRD |= (1<<6); PORTD &= ~(1<<6);
  91. return;
  92. }
  93. #ifdef ADB_MOUSE_ENABLE
  94. #ifdef MAX
  95. #undef MAX
  96. #endif
  97. #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
  98. void adb_mouse_task(void)
  99. {
  100. uint16_t codes;
  101. int16_t x, y;
  102. static int8_t mouseacc;
  103. _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
  104. codes = adb_host_mouse_recv();
  105. // If nothing received reset mouse acceleration, and quit.
  106. if (!codes) {
  107. mouseacc = 1;
  108. return;
  109. };
  110. // Bit sixteen is button.
  111. if (~codes & (1 << 15))
  112. mouse_report.buttons |= MOUSE_BTN1;
  113. if (codes & (1 << 15))
  114. mouse_report.buttons &= ~MOUSE_BTN1;
  115. // lower seven bits are movement, as signed int_7.
  116. // low byte is X-axis, high byte is Y.
  117. y = (codes>>8 & 0x3F);
  118. x = (codes>>0 & 0x3F);
  119. // bit seven and fifteen is negative
  120. // usb does not use int_8, but int_7 (measuring distance) with sign-bit.
  121. if (codes & (1 << 6))
  122. x = (x-0x40);
  123. if (codes & (1 << 14))
  124. y = (y-0x40);
  125. // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
  126. x *= mouseacc;
  127. y *= mouseacc;
  128. // Cap our two bytes per axis to one byte.
  129. // Easier with a MIN-function, but since -MAX(-a,-b) = MIN(a,b)...
  130. // I.E. MIN(MAX(x,-127),127) = -MAX(-MAX(x, -127), -127) = MIN(-MIN(-x,127),127)
  131. mouse_report.x = -MAX(-MAX(x, -127), -127);
  132. mouse_report.y = -MAX(-MAX(y, -127), -127);
  133. if (debug_mouse) {
  134. print("adb_host_mouse_recv: "); print_bin16(codes); print("\n");
  135. print("adb_mouse raw: [");
  136. phex(mouseacc); print(" ");
  137. phex(mouse_report.buttons); print("|");
  138. print_decs(mouse_report.x); print(" ");
  139. print_decs(mouse_report.y); print("]\n");
  140. }
  141. // Send result by usb.
  142. host_mouse_send(&mouse_report);
  143. // increase acceleration of mouse
  144. mouseacc += ( mouseacc < ADB_MOUSE_MAXACC ? 1 : 0 );
  145. return;
  146. }
  147. #endif
  148. uint8_t matrix_scan(void)
  149. {
  150. /* extra_key is volatile and more convoluted than necessary because gcc refused
  151. to generate valid code otherwise. Making extra_key uint8_t and constructing codes
  152. here via codes = extra_key<<8 | 0xFF; would consistently fail to even LOAD
  153. extra_key from memory, and leave garbage in the high byte of codes. I tried
  154. dozens of code variations and it kept generating broken assembly output. So
  155. beware if attempting to make extra_key code more logical and efficient. */
  156. static volatile uint16_t extra_key = 0xFFFF;
  157. uint16_t codes;
  158. uint8_t key0, key1;
  159. codes = extra_key;
  160. extra_key = 0xFFFF;
  161. if ( codes == 0xFFFF )
  162. {
  163. _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
  164. codes = adb_host_kbd_recv(ADB_ADDR_KEYBOARD);
  165. // Adjustable keybaord media keys
  166. if (codes == 0 && has_media_keys &&
  167. (codes = adb_host_kbd_recv(ADB_ADDR_APPLIANCE))) {
  168. // key1
  169. switch (codes & 0x7f ) {
  170. case 0x00: // Mic
  171. codes = (codes & ~0x007f) | 0x42;
  172. break;
  173. case 0x01: // Mute
  174. codes = (codes & ~0x007f) | 0x4a;
  175. break;
  176. case 0x02: // Volume down
  177. codes = (codes & ~0x007f) | 0x49;
  178. break;
  179. case 0x03: // Volume Up
  180. codes = (codes & ~0x007f) | 0x48;
  181. break;
  182. case 0x7F: // no code
  183. break;
  184. default:
  185. xprintf("ERROR: media key1\n");
  186. return 0x11;
  187. }
  188. // key0
  189. switch ((codes >> 8) & 0x7f ) {
  190. case 0x00: // Mic
  191. codes = (codes & ~0x7f00) | (0x42 << 8);
  192. break;
  193. case 0x01: // Mute
  194. codes = (codes & ~0x7f00) | (0x4a << 8);
  195. break;
  196. case 0x02: // Volume down
  197. codes = (codes & ~0x7f00) | (0x49 << 8);
  198. break;
  199. case 0x03: // Volume Up
  200. codes = (codes & ~0x7f00) | (0x48 << 8);
  201. break;
  202. default:
  203. xprintf("ERROR: media key0\n");
  204. return 0x10;
  205. }
  206. }
  207. }
  208. key0 = codes>>8;
  209. key1 = codes&0xFF;
  210. if (debug_matrix && codes) {
  211. print("adb_host_kbd_recv: "); phex16(codes); print("\n");
  212. }
  213. if (codes == 0) { // no keys
  214. return 0;
  215. } else if (codes == 0x7F7F) { // power key press
  216. register_key(0x7F);
  217. } else if (codes == 0xFFFF) { // power key release
  218. register_key(0xFF);
  219. } else if (key0 == 0xFF) { // error
  220. xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
  221. // something wrong or plug-in
  222. matrix_init();
  223. return key1;
  224. } else {
  225. /* Swap codes for ISO keyboard
  226. * https://github.com/tmk/tmk_keyboard/issues/35
  227. *
  228. * ANSI
  229. * ,----------- ----------.
  230. * | *a| 1| 2 =|Backspa|
  231. * |----------- ----------|
  232. * |Tab | Q| | ]| *c|
  233. * |----------- ----------|
  234. * |CapsLo| A| '|Return |
  235. * |----------- ----------|
  236. * |Shift | Shift |
  237. * `----------- ----------'
  238. *
  239. * ISO
  240. * ,----------- ----------.
  241. * | *a| 1| 2 =|Backspa|
  242. * |----------- ----------|
  243. * |Tab | Q| | ]|Retur|
  244. * |----------- -----` |
  245. * |CapsLo| A| '| *c| |
  246. * |----------- ----------|
  247. * |Shif| *b| Shift |
  248. * `----------- ----------'
  249. *
  250. * ADB scan code USB usage
  251. * ------------- ---------
  252. * Key ANSI ISO ANSI ISO
  253. * ---------------------------------------------
  254. * *a 0x32 0x0A 0x35 0x35
  255. * *b ---- 0x32 ---- 0x64
  256. * *c 0x2A 0x2A 0x31 0x31(or 0x32)
  257. */
  258. if (is_iso_layout) {
  259. if ((key0 & 0x7F) == 0x32) {
  260. key0 = (key0 & 0x80) | 0x0A;
  261. } else if ((key0 & 0x7F) == 0x0A) {
  262. key0 = (key0 & 0x80) | 0x32;
  263. }
  264. }
  265. register_key(key0);
  266. if (key1 != 0xFF) // key1 is 0xFF when no second key.
  267. extra_key = key1<<8 | 0xFF; // process in a separate call
  268. }
  269. return 1;
  270. }
  271. inline
  272. matrix_row_t matrix_get_row(uint8_t row)
  273. {
  274. return matrix[row];
  275. }
  276. inline
  277. static void register_key(uint8_t key)
  278. {
  279. uint8_t col, row;
  280. col = key&0x07;
  281. row = (key>>3)&0x0F;
  282. if (key&0x80) {
  283. matrix[row] &= ~(1<<col);
  284. } else {
  285. matrix[row] |= (1<<col);
  286. }
  287. }