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