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 роки тому
13 роки тому
13 роки тому
13 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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_modified = false;
  35. static report_mouse_t mouse_report = {};
  36. // matrix state buffer(1:on, 0:off)
  37. #if (MATRIX_COLS <= 8)
  38. static uint8_t matrix[MATRIX_ROWS];
  39. #else
  40. static uint16_t matrix[MATRIX_ROWS];
  41. #endif
  42. #ifdef MATRIX_HAS_GHOST
  43. static bool matrix_has_ghost_in_row(uint8_t row);
  44. #endif
  45. static void register_key(uint8_t key);
  46. inline
  47. uint8_t matrix_rows(void)
  48. {
  49. return MATRIX_ROWS;
  50. }
  51. inline
  52. uint8_t matrix_cols(void)
  53. {
  54. return MATRIX_COLS;
  55. }
  56. void matrix_init(void)
  57. {
  58. adb_host_init();
  59. // wait for keyboard to boot up and receive command
  60. _delay_ms(1000);
  61. // Enable keyboard left/right modifier distinction
  62. // Addr:Keyboard(0010), Cmd:Listen(10), Register3(11)
  63. // upper byte: reserved bits 0000, device address 0010
  64. // lower byte: device handler 00000011
  65. adb_host_listen(0x2B,0x02,0x03);
  66. // initialize matrix state: all keys off
  67. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  68. debug_enable = true;
  69. //debug_matrix = true;
  70. //debug_keyboard = true;
  71. //debug_mouse = true;
  72. print("debug enabled.\n");
  73. return;
  74. }
  75. #ifdef ADB_MOUSE_ENABLE
  76. #ifdef MAX
  77. #undef MAX
  78. #endif
  79. #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
  80. void adb_mouse_task(void)
  81. {
  82. uint16_t codes;
  83. int16_t x, y;
  84. static int8_t mouseacc;
  85. _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
  86. codes = adb_host_mouse_recv();
  87. // If nothing received reset mouse acceleration, and quit.
  88. if (!codes) {
  89. mouseacc = 1;
  90. return;
  91. };
  92. // Bit sixteen is button.
  93. if (~codes & (1 << 15))
  94. mouse_report.buttons |= MOUSE_BTN1;
  95. if (codes & (1 << 15))
  96. mouse_report.buttons &= ~MOUSE_BTN1;
  97. // lower seven bits are movement, as signed int_7.
  98. // low byte is X-axis, high byte is Y.
  99. y = (codes>>8 & 0x3F);
  100. x = (codes>>0 & 0x3F);
  101. // bit seven and fifteen is negative
  102. // usb does not use int_8, but int_7 (measuring distance) with sign-bit.
  103. if (codes & (1 << 6))
  104. x = (x-0x40);
  105. if (codes & (1 << 14))
  106. y = (y-0x40);
  107. // Accelerate mouse. (They weren't meant to be used on screens larger than 320x200).
  108. x *= mouseacc;
  109. y *= mouseacc;
  110. // Cap our two bytes per axis to one byte.
  111. // Easier with a MIN-function, but since -MAX(-a,-b) = MIN(a,b)...
  112. // I.E. MIN(MAX(x,-127),127) = -MAX(-MAX(x, -127), -127) = MIN(-MIN(-x,127),127)
  113. mouse_report.x = -MAX(-MAX(x, -127), -127);
  114. mouse_report.y = -MAX(-MAX(y, -127), -127);
  115. if (debug_mouse) {
  116. print("adb_host_mouse_recv: "); print_bin16(codes); print("\n");
  117. print("adb_mouse raw: [");
  118. phex(mouseacc); print(" ");
  119. phex(mouse_report.buttons); print("|");
  120. print_decs(mouse_report.x); print(" ");
  121. print_decs(mouse_report.y); print("]\n");
  122. }
  123. // Send result by usb.
  124. host_mouse_send(&mouse_report);
  125. // increase acceleration of mouse
  126. mouseacc += ( mouseacc < ADB_MOUSE_MAXACC ? 1 : 0 );
  127. return;
  128. }
  129. #endif
  130. uint8_t matrix_scan(void)
  131. {
  132. /* extra_key is volatile and more convoluted than necessary because gcc refused
  133. to generate valid code otherwise. Making extra_key uint8_t and constructing codes
  134. here via codes = extra_key<<8 | 0xFF; would consistently fail to even LOAD
  135. extra_key from memory, and leave garbage in the high byte of codes. I tried
  136. dozens of code variations and it kept generating broken assembly output. So
  137. beware if attempting to make extra_key code more logical and efficient. */
  138. static volatile uint16_t extra_key = 0xFFFF;
  139. uint16_t codes;
  140. uint8_t key0, key1;
  141. is_modified = false;
  142. codes = extra_key;
  143. extra_key = 0xFFFF;
  144. if ( codes == 0xFFFF )
  145. {
  146. _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
  147. codes = adb_host_kbd_recv();
  148. }
  149. key0 = codes>>8;
  150. key1 = codes&0xFF;
  151. if (debug_matrix && codes) {
  152. print("adb_host_kbd_recv: "); phex16(codes); print("\n");
  153. }
  154. if (codes == 0) { // no keys
  155. return 0;
  156. } else if (codes == 0x7F7F) { // power key press
  157. register_key(0x7F);
  158. } else if (codes == 0xFFFF) { // power key release
  159. register_key(0xFF);
  160. } else if (key0 == 0xFF) { // error
  161. xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
  162. return key1;
  163. } else {
  164. register_key(key0);
  165. if (key1 != 0xFF) // key1 is 0xFF when no second key.
  166. extra_key = key1<<8 | 0xFF; // process in a separate call
  167. }
  168. return 1;
  169. }
  170. bool matrix_is_modified(void)
  171. {
  172. return is_modified;
  173. }
  174. inline
  175. bool matrix_has_ghost(void)
  176. {
  177. #ifdef MATRIX_HAS_GHOST
  178. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  179. if (matrix_has_ghost_in_row(i))
  180. return true;
  181. }
  182. #endif
  183. return false;
  184. }
  185. inline
  186. bool matrix_is_on(uint8_t row, uint8_t col)
  187. {
  188. return (matrix[row] & (1<<col));
  189. }
  190. inline
  191. #if (MATRIX_COLS <= 8)
  192. uint8_t matrix_get_row(uint8_t row)
  193. #else
  194. uint16_t matrix_get_row(uint8_t row)
  195. #endif
  196. {
  197. return matrix[row];
  198. }
  199. void matrix_print(void)
  200. {
  201. if (!debug_matrix) return;
  202. #if (MATRIX_COLS <= 8)
  203. print("r/c 01234567\n");
  204. #else
  205. print("r/c 0123456789ABCDEF\n");
  206. #endif
  207. for (uint8_t row = 0; row < matrix_rows(); row++) {
  208. phex(row); print(": ");
  209. #if (MATRIX_COLS <= 8)
  210. pbin_reverse(matrix_get_row(row));
  211. #else
  212. pbin_reverse16(matrix_get_row(row));
  213. #endif
  214. #ifdef MATRIX_HAS_GHOST
  215. if (matrix_has_ghost_in_row(row)) {
  216. print(" <ghost");
  217. }
  218. #endif
  219. print("\n");
  220. }
  221. }
  222. uint8_t matrix_key_count(void)
  223. {
  224. uint8_t count = 0;
  225. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  226. #if (MATRIX_COLS <= 8)
  227. count += bitpop(matrix[i]);
  228. #else
  229. count += bitpop16(matrix[i]);
  230. #endif
  231. }
  232. return count;
  233. }
  234. #ifdef MATRIX_HAS_GHOST
  235. inline
  236. static bool matrix_has_ghost_in_row(uint8_t row)
  237. {
  238. // no ghost exists in case less than 2 keys on
  239. if (((matrix[row] - 1) & matrix[row]) == 0)
  240. return false;
  241. // ghost exists in case same state as other row
  242. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  243. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  244. return true;
  245. }
  246. return false;
  247. }
  248. #endif
  249. inline
  250. static void register_key(uint8_t key)
  251. {
  252. uint8_t col, row;
  253. col = key&0x07;
  254. row = (key>>3)&0x0F;
  255. if (key&0x80) {
  256. matrix[row] &= ~(1<<col);
  257. } else {
  258. matrix[row] |= (1<<col);
  259. }
  260. is_modified = true;
  261. }