Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
13 лет назад
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 "host.h"
  25. #include "led.h"
  26. #include "adb.h"
  27. #include "matrix.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. #define CAPS 0x39
  35. #define CAPS_UP (CAPS | 0x80)
  36. #define ROW(key) ((key)>>3&0x0F)
  37. #define COL(key) ((key)&0x07)
  38. static bool _matrix_is_modified = false;
  39. // matrix state buffer(1:on, 0:off)
  40. #if (MATRIX_COLS <= 8)
  41. static uint8_t *matrix;
  42. static uint8_t _matrix0[MATRIX_ROWS];
  43. #else
  44. static uint16_t *matrix;
  45. static uint16_t _matrix0[MATRIX_ROWS];
  46. #endif
  47. #ifdef MATRIX_HAS_GHOST
  48. static bool matrix_has_ghost_in_row(uint8_t row);
  49. #endif
  50. static void _register_key(uint8_t key);
  51. inline
  52. uint8_t matrix_rows(void)
  53. {
  54. return MATRIX_ROWS;
  55. }
  56. inline
  57. uint8_t matrix_cols(void)
  58. {
  59. return MATRIX_COLS;
  60. }
  61. void matrix_init(void)
  62. {
  63. adb_host_init();
  64. // initialize matrix state: all keys off
  65. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  66. matrix = _matrix0;
  67. print_enable = true;
  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. uint8_t matrix_scan(void)
  76. {
  77. uint16_t codes;
  78. uint8_t key0, key1;
  79. _matrix_is_modified = false;
  80. codes = adb_host_kbd_recv();
  81. key0 = codes>>8;
  82. key1 = codes&0xFF;
  83. #ifdef MATRIX_HAS_LOCKING_CAPS
  84. // Send Caps key up event
  85. if (matrix_is_on(ROW(CAPS), COL(CAPS))) {
  86. _matrix_is_modified = true;
  87. _register_key(CAPS_UP);
  88. }
  89. #endif
  90. if (codes == 0) { // no keys
  91. return 0;
  92. } else if (key0 == 0xFF && key1 != 0xFF) { // error
  93. return codes&0xFF;
  94. } else {
  95. #ifdef MATRIX_HAS_LOCKING_CAPS
  96. if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) {
  97. // Ignore LockingCaps key down event when CAPS LOCK is on
  98. if (key0 == CAPS && (key1 == CAPS || key1 == 0xFF)) return 0;
  99. if (key0 == CAPS) key0 = key1;
  100. if (key1 == CAPS) key1 = 0xFF;
  101. // Convert LockingCaps key up event into down event
  102. if (key0 == CAPS_UP) key0 = CAPS;
  103. if (key1 == CAPS_UP) key1 = CAPS;
  104. } else {
  105. // CAPS LOCK off:
  106. // Ignore LockingCaps key up event when CAPS LOCK is off
  107. if (key0 == CAPS_UP && (key1 == CAPS_UP || key1 == 0xFF)) return 0;
  108. if (key0 == CAPS_UP) key0 = key1;
  109. if (key1 == CAPS_UP) key1 = 0xFF;
  110. }
  111. #endif
  112. _matrix_is_modified = true;
  113. _register_key(key0);
  114. if (key1 != 0xFF) // key1 is 0xFF when no second key.
  115. _register_key(key1);
  116. }
  117. if (debug_enable) {
  118. print("adb_host_kbd_recv: "); phex16(codes); print("\n");
  119. }
  120. return 1;
  121. }
  122. bool matrix_is_modified(void)
  123. {
  124. return _matrix_is_modified;
  125. }
  126. inline
  127. bool matrix_has_ghost(void)
  128. {
  129. #ifdef MATRIX_HAS_GHOST
  130. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  131. if (matrix_has_ghost_in_row(i))
  132. return true;
  133. }
  134. #endif
  135. return false;
  136. }
  137. inline
  138. bool matrix_is_on(uint8_t row, uint8_t col)
  139. {
  140. return (matrix[row] & (1<<col));
  141. }
  142. inline
  143. #if (MATRIX_COLS <= 8)
  144. uint8_t matrix_get_row(uint8_t row)
  145. #else
  146. uint16_t matrix_get_row(uint8_t row)
  147. #endif
  148. {
  149. return matrix[row];
  150. }
  151. void matrix_print(void)
  152. {
  153. #if (MATRIX_COLS <= 8)
  154. print("\nr/c 01234567\n");
  155. #else
  156. print("\nr/c 0123456789ABCDEF\n");
  157. #endif
  158. for (uint8_t row = 0; row < matrix_rows(); row++) {
  159. phex(row); print(": ");
  160. #if (MATRIX_COLS <= 8)
  161. pbin_reverse(matrix_get_row(row));
  162. #else
  163. pbin_reverse16(matrix_get_row(row));
  164. #endif
  165. #ifdef MATRIX_HAS_GHOST
  166. if (matrix_has_ghost_in_row(row)) {
  167. print(" <ghost");
  168. }
  169. #endif
  170. print("\n");
  171. }
  172. }
  173. uint8_t matrix_key_count(void)
  174. {
  175. uint8_t count = 0;
  176. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  177. #if (MATRIX_COLS <= 8)
  178. count += bitpop(matrix[i]);
  179. #else
  180. count += bitpop16(matrix[i]);
  181. #endif
  182. }
  183. return count;
  184. }
  185. #ifdef MATRIX_HAS_GHOST
  186. inline
  187. static bool matrix_has_ghost_in_row(uint8_t row)
  188. {
  189. // no ghost exists in case less than 2 keys on
  190. if (((matrix[row] - 1) & matrix[row]) == 0)
  191. return false;
  192. // ghost exists in case same state as other row
  193. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  194. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  195. return true;
  196. }
  197. return false;
  198. }
  199. #endif
  200. inline
  201. static void _register_key(uint8_t key)
  202. {
  203. uint8_t col, row;
  204. col = key&0x07;
  205. row = (key>>3)&0x0F;
  206. if (key&0x80) {
  207. matrix[row] &= ~(1<<col);
  208. } else {
  209. matrix[row] |= (1<<col);
  210. }
  211. }