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.

261 lines
7.2 KiB

  1. /*
  2. Copyright 2011 Jun Wako <wakojun@gmail.com>
  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. Ported to QMK by Peter Roe <pete@13bit.me>
  14. */
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include <avr/io.h>
  18. #include <util/delay.h>
  19. #include "print.h"
  20. #include "util.h"
  21. #include "debug.h"
  22. #include "adb.h"
  23. #include "matrix.h"
  24. #include "report.h"
  25. #include "host.h"
  26. #include "led.h"
  27. #include "timer.h"
  28. static bool is_iso_layout = false;
  29. // matrix state buffer(1:on, 0:off)
  30. static matrix_row_t matrix[MATRIX_ROWS];
  31. static void register_key(uint8_t key);
  32. __attribute__ ((weak))
  33. void matrix_init_kb(void) {
  34. matrix_init_user();
  35. }
  36. __attribute__ ((weak))
  37. void matrix_scan_kb(void) {
  38. matrix_scan_user();
  39. }
  40. __attribute__ ((weak))
  41. void matrix_init_user(void) {
  42. }
  43. __attribute__ ((weak))
  44. void matrix_scan_user(void) {
  45. }
  46. void matrix_init(void)
  47. {
  48. adb_host_init();
  49. // wait for keyboard to boot up and receive command
  50. _delay_ms(2000);
  51. // initialize matrix state: all keys off
  52. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  53. // debug_enable = true;
  54. // debug_matrix = true;
  55. // debug_keyboard = true;
  56. // debug_mouse = true;
  57. // print("debug enabled.\n");
  58. matrix_init_quantum();
  59. }
  60. #ifdef ADB_MOUSE_ENABLE
  61. #ifdef MAX
  62. #undef MAX
  63. #endif
  64. #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
  65. static report_mouse_t mouse_report = {};
  66. void adb_mouse_task(void)
  67. {
  68. uint16_t codes;
  69. int16_t x, y;
  70. static int8_t mouseacc;
  71. /* tick of last polling */
  72. static uint16_t tick_ms;
  73. // polling with 12ms interval
  74. if (timer_elapsed(tick_ms) < 12) return;
  75. tick_ms = timer_read();
  76. codes = adb_host_mouse_recv();
  77. // If nothing received reset mouse acceleration, and quit.
  78. if (!codes) {
  79. mouseacc = 1;
  80. return;
  81. };
  82. // Bit sixteen is button.
  83. if (~codes & (1 << 15))
  84. mouse_report.buttons |= MOUSE_BTN1;
  85. if (codes & (1 << 15))
  86. mouse_report.buttons &= ~MOUSE_BTN1;
  87. // lower seven bits are movement, as signed int_7.
  88. // low byte is X-axis, high byte is Y.
  89. y = (codes>>8 & 0x3F);
  90. x = (codes>>0 & 0x3F);
  91. // bit seven and fifteen is negative
  92. // usb does not use int_8, but int_7 (measuring distance) with sign-bit.
  93. if (codes & (1 << 6))
  94. x = (x-0x40);
  95. if (codes & (1 << 14))
  96. y = (y-0x40);
  97. // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
  98. x *= mouseacc;
  99. y *= mouseacc;
  100. // Cap our two bytes per axis to one byte.
  101. // Easier with a MIN-function, but since -MAX(-a,-b) = MIN(a,b)...
  102. // I.E. MIN(MAX(x,-127),127) = -MAX(-MAX(x, -127), -127) = MIN(-MIN(-x,127),127)
  103. mouse_report.x = -MAX(-MAX(x, -127), -127);
  104. mouse_report.y = -MAX(-MAX(y, -127), -127);
  105. if (debug_mouse) {
  106. print("adb_host_mouse_recv: "); print_bin16(codes); print("\n");
  107. print("adb_mouse raw: [");
  108. print_hex8(mouseacc); print(" ");
  109. print_hex8(mouse_report.buttons); print("|");
  110. print_decs(mouse_report.x); print(" ");
  111. print_decs(mouse_report.y); print("]\n");
  112. }
  113. // Send result by usb.
  114. host_mouse_send(&mouse_report);
  115. // increase acceleration of mouse
  116. mouseacc += ( mouseacc < ADB_MOUSE_MAXACC ? 1 : 0 );
  117. return;
  118. }
  119. #endif
  120. uint8_t matrix_scan(void)
  121. {
  122. /* extra_key is volatile and more convoluted than necessary because gcc refused
  123. to generate valid code otherwise. Making extra_key uint8_t and constructing codes
  124. here via codes = extra_key<<8 | 0xFF; would consistently fail to even LOAD
  125. extra_key from memory, and leave garbage in the high byte of codes. I tried
  126. dozens of code variations and it kept generating broken assembly output. So
  127. beware if attempting to make extra_key code more logical and efficient. */
  128. static volatile uint16_t extra_key = 0xFFFF;
  129. uint16_t codes;
  130. uint8_t key0, key1;
  131. /* tick of last polling */
  132. static uint16_t tick_ms;
  133. codes = extra_key;
  134. extra_key = 0xFFFF;
  135. if ( codes == 0xFFFF )
  136. {
  137. // polling with 12ms interval
  138. if (timer_elapsed(tick_ms) < 12) return 0;
  139. tick_ms = timer_read();
  140. codes = adb_host_kbd_recv();
  141. }
  142. key0 = codes>>8;
  143. key1 = codes&0xFF;
  144. if (debug_matrix && codes) {
  145. print("adb_host_kbd_recv: "); print_hex16(codes); print("\n");
  146. }
  147. if (codes == 0) { // no keys
  148. return 0;
  149. } else if (codes == 0x7F7F) { // power key press
  150. register_key(0x7F);
  151. } else if (codes == 0xFFFF) { // power key release
  152. register_key(0xFF);
  153. } else if (key0 == 0xFF) { // error
  154. xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
  155. // something wrong or plug-in
  156. matrix_init();
  157. return key1;
  158. } else {
  159. /* Swap codes for ISO keyboard
  160. * https://github.com/tmk/tmk_keyboard/issues/35
  161. *
  162. * ANSI
  163. * ,----------- ----------.
  164. * | *a| 1| 2 =|Backspa|
  165. * |----------- ----------|
  166. * |Tab | Q| | ]| *c|
  167. * |----------- ----------|
  168. * |CapsLo| A| '|Return |
  169. * |----------- ----------|
  170. * |Shift | Shift |
  171. * `----------- ----------'
  172. *
  173. * ISO
  174. * ,----------- ----------.
  175. * | *a| 1| 2 =|Backspa|
  176. * |----------- ----------|
  177. * |Tab | Q| | ]|Retur|
  178. * |----------- -----` |
  179. * |CapsLo| A| '| *c| |
  180. * |----------- ----------|
  181. * |Shif| *b| Shift |
  182. * `----------- ----------'
  183. *
  184. * ADB scan code USB usage
  185. * ------------- ---------
  186. * Key ANSI ISO ANSI ISO
  187. * ---------------------------------------------
  188. * *a 0x32 0x0A 0x35 0x35
  189. * *b ---- 0x32 ---- 0x64
  190. * *c 0x2A 0x2A 0x31 0x31(or 0x32)
  191. */
  192. if (is_iso_layout) {
  193. if ((key0 & 0x7F) == 0x32) {
  194. key0 = (key0 & 0x80) | 0x0A;
  195. } else if ((key0 & 0x7F) == 0x0A) {
  196. key0 = (key0 & 0x80) | 0x32;
  197. }
  198. }
  199. register_key(key0);
  200. if (key1 != 0xFF) // key1 is 0xFF when no second key.
  201. extra_key = key1<<8 | 0xFF; // process in a separate call
  202. }
  203. matrix_scan_quantum();
  204. return 1;
  205. }
  206. void matrix_print(void){
  207. }
  208. inline
  209. matrix_row_t matrix_get_row(uint8_t row)
  210. {
  211. return matrix[row];
  212. }
  213. inline
  214. static void register_key(uint8_t key)
  215. {
  216. uint8_t col, row;
  217. col = key&0x07;
  218. row = (key>>3)&0x0F;
  219. if (key&0x80) {
  220. matrix[row] &= ~(1<<col);
  221. } else {
  222. matrix[row] |= (1<<col);
  223. }
  224. }