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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 12 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 13 години
преди 12 години
преди 13 години
преди 12 години
преди 13 години
преди 13 години
преди 13 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. #if (MATRIX_COLS > 16)
  27. # error "MATRIX_COLS must not exceed 16"
  28. #endif
  29. #if (MATRIX_ROWS > 255)
  30. # error "MATRIX_ROWS must not exceed 255"
  31. #endif
  32. static bool is_modified = false;
  33. // matrix state buffer(1:on, 0:off)
  34. #if (MATRIX_COLS <= 8)
  35. static uint8_t matrix[MATRIX_ROWS];
  36. #else
  37. static uint16_t matrix[MATRIX_ROWS];
  38. #endif
  39. #ifdef MATRIX_HAS_GHOST
  40. static bool matrix_has_ghost_in_row(uint8_t row);
  41. #endif
  42. static void register_key(uint8_t key);
  43. inline
  44. uint8_t matrix_rows(void)
  45. {
  46. return MATRIX_ROWS;
  47. }
  48. inline
  49. uint8_t matrix_cols(void)
  50. {
  51. return MATRIX_COLS;
  52. }
  53. void matrix_init(void)
  54. {
  55. adb_host_init();
  56. // wait for keyboard to boot up and receive command
  57. _delay_ms(1000);
  58. // Enable keyboard left/right modifier distinction
  59. // Addr:Keyboard(0010), Cmd:Listen(10), Register3(11)
  60. // upper byte: reserved bits 0000, device address 0010
  61. // lower byte: device handler 00000011
  62. adb_host_listen(0x2B,0x02,0x03);
  63. // initialize matrix state: all keys off
  64. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  65. debug_enable = true;
  66. //debug_matrix = true;
  67. //debug_keyboard = true;
  68. //debug_mouse = true;
  69. print("debug enabled.\n");
  70. return;
  71. }
  72. uint8_t matrix_scan(void)
  73. {
  74. /* extra_key is volatile and more convoluted than necessary because gcc refused
  75. to generate valid code otherwise. Making extra_key uint8_t and constructing codes
  76. here via codes = extra_key<<8 | 0xFF; would consistently fail to even LOAD
  77. extra_key from memory, and leave garbage in the high byte of codes. I tried
  78. dozens of code variations and it kept generating broken assembly output. So
  79. beware if attempting to make extra_key code more logical and efficient. */
  80. static volatile uint16_t extra_key = 0xFFFF;
  81. uint16_t codes;
  82. uint8_t key0, key1;
  83. is_modified = false;
  84. codes = extra_key;
  85. extra_key = 0xFFFF;
  86. if ( codes == 0xFFFF )
  87. {
  88. _delay_ms(12); // delay for preventing overload of poor ADB keyboard controller
  89. codes = adb_host_kbd_recv();
  90. }
  91. key0 = codes>>8;
  92. key1 = codes&0xFF;
  93. if (debug_matrix && codes) {
  94. print("adb_host_kbd_recv: "); phex16(codes); print("\n");
  95. }
  96. if (codes == 0) { // no keys
  97. return 0;
  98. } else if (codes == 0x7F7F) { // power key press
  99. register_key(0x7F);
  100. } else if (codes == 0xFFFF) { // power key release
  101. register_key(0xFF);
  102. } else if (key0 == 0xFF) { // error
  103. xprintf("adb_host_kbd_recv: ERROR(%d)\n", codes);
  104. return key1;
  105. } else {
  106. register_key(key0);
  107. if (key1 != 0xFF) // key1 is 0xFF when no second key.
  108. extra_key = key1<<8 | 0xFF; // process in a separate call
  109. }
  110. return 1;
  111. }
  112. bool matrix_is_modified(void)
  113. {
  114. return is_modified;
  115. }
  116. inline
  117. bool matrix_has_ghost(void)
  118. {
  119. #ifdef MATRIX_HAS_GHOST
  120. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  121. if (matrix_has_ghost_in_row(i))
  122. return true;
  123. }
  124. #endif
  125. return false;
  126. }
  127. inline
  128. bool matrix_is_on(uint8_t row, uint8_t col)
  129. {
  130. return (matrix[row] & (1<<col));
  131. }
  132. inline
  133. #if (MATRIX_COLS <= 8)
  134. uint8_t matrix_get_row(uint8_t row)
  135. #else
  136. uint16_t matrix_get_row(uint8_t row)
  137. #endif
  138. {
  139. return matrix[row];
  140. }
  141. void matrix_print(void)
  142. {
  143. if (!debug_matrix) return;
  144. #if (MATRIX_COLS <= 8)
  145. print("r/c 01234567\n");
  146. #else
  147. print("r/c 0123456789ABCDEF\n");
  148. #endif
  149. for (uint8_t row = 0; row < matrix_rows(); row++) {
  150. phex(row); print(": ");
  151. #if (MATRIX_COLS <= 8)
  152. pbin_reverse(matrix_get_row(row));
  153. #else
  154. pbin_reverse16(matrix_get_row(row));
  155. #endif
  156. #ifdef MATRIX_HAS_GHOST
  157. if (matrix_has_ghost_in_row(row)) {
  158. print(" <ghost");
  159. }
  160. #endif
  161. print("\n");
  162. }
  163. }
  164. uint8_t matrix_key_count(void)
  165. {
  166. uint8_t count = 0;
  167. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  168. #if (MATRIX_COLS <= 8)
  169. count += bitpop(matrix[i]);
  170. #else
  171. count += bitpop16(matrix[i]);
  172. #endif
  173. }
  174. return count;
  175. }
  176. #ifdef MATRIX_HAS_GHOST
  177. inline
  178. static bool matrix_has_ghost_in_row(uint8_t row)
  179. {
  180. // no ghost exists in case less than 2 keys on
  181. if (((matrix[row] - 1) & matrix[row]) == 0)
  182. return false;
  183. // ghost exists in case same state as other row
  184. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  185. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  186. return true;
  187. }
  188. return false;
  189. }
  190. #endif
  191. inline
  192. static void register_key(uint8_t key)
  193. {
  194. uint8_t col, row;
  195. col = key&0x07;
  196. row = (key>>3)&0x0F;
  197. if (key&0x80) {
  198. matrix[row] &= ~(1<<col);
  199. } else {
  200. matrix[row] |= (1<<col);
  201. }
  202. is_modified = true;
  203. }