Keyboard firmwares for Atmel AVR and Cortex-M
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.

matrix.c 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 _matrix_is_modified = false;
  33. // matrix state buffer(1:on, 0:off)
  34. #if (MATRIX_COLS <= 8)
  35. static uint8_t *matrix;
  36. static uint8_t _matrix0[MATRIX_ROWS];
  37. #else
  38. static uint16_t *matrix;
  39. static uint16_t _matrix0[MATRIX_ROWS];
  40. #endif
  41. #ifdef MATRIX_HAS_GHOST
  42. static bool matrix_has_ghost_in_row(uint8_t row);
  43. #endif
  44. static void _register_key(uint8_t key);
  45. inline
  46. uint8_t matrix_rows(void)
  47. {
  48. return MATRIX_ROWS;
  49. }
  50. inline
  51. uint8_t matrix_cols(void)
  52. {
  53. return MATRIX_COLS;
  54. }
  55. void matrix_init(void)
  56. {
  57. adb_host_init();
  58. // initialize matrix state: all keys off
  59. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  60. matrix = _matrix0;
  61. print_enable = true;
  62. debug_enable = true;
  63. debug_matrix = true;
  64. debug_keyboard = true;
  65. debug_mouse = true;
  66. print("debug enabled.\n");
  67. return;
  68. }
  69. uint8_t matrix_scan(void)
  70. {
  71. uint16_t codes;
  72. uint8_t key0, key1;
  73. _matrix_is_modified = false;
  74. codes = adb_host_kbd_recv();
  75. key0 = codes>>8;
  76. key1 = codes&0xFF;
  77. if (codes == 0) { // no keys
  78. return 0;
  79. } else if (key0 == 0xFF && key1 != 0xFF) { // error
  80. return codes&0xFF;
  81. } else {
  82. _matrix_is_modified = true;
  83. _register_key(key0);
  84. if (key1 != 0xFF) // key1 is 0xFF when no second key.
  85. _register_key(key1);
  86. }
  87. if (debug_enable) {
  88. print("adb_host_kbd_recv: "); phex16(codes); print("\n");
  89. }
  90. return 1;
  91. }
  92. bool matrix_is_modified(void)
  93. {
  94. return _matrix_is_modified;
  95. }
  96. inline
  97. bool matrix_has_ghost(void)
  98. {
  99. #ifdef MATRIX_HAS_GHOST
  100. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  101. if (matrix_has_ghost_in_row(i))
  102. return true;
  103. }
  104. #endif
  105. return false;
  106. }
  107. inline
  108. bool matrix_is_on(uint8_t row, uint8_t col)
  109. {
  110. return (matrix[row] & (1<<col));
  111. }
  112. inline
  113. #if (MATRIX_COLS <= 8)
  114. uint8_t matrix_get_row(uint8_t row)
  115. #else
  116. uint16_t matrix_get_row(uint8_t row)
  117. #endif
  118. {
  119. return matrix[row];
  120. }
  121. void matrix_print(void)
  122. {
  123. #if (MATRIX_COLS <= 8)
  124. print("\nr/c 01234567\n");
  125. #else
  126. print("\nr/c 0123456789ABCDEF\n");
  127. #endif
  128. for (uint8_t row = 0; row < matrix_rows(); row++) {
  129. phex(row); print(": ");
  130. #if (MATRIX_COLS <= 8)
  131. pbin_reverse(matrix_get_row(row));
  132. #else
  133. pbin_reverse16(matrix_get_row(row));
  134. #endif
  135. #ifdef MATRIX_HAS_GHOST
  136. if (matrix_has_ghost_in_row(row)) {
  137. print(" <ghost");
  138. }
  139. #endif
  140. print("\n");
  141. }
  142. }
  143. uint8_t matrix_key_count(void)
  144. {
  145. uint8_t count = 0;
  146. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  147. #if (MATRIX_COLS <= 8)
  148. count += bitpop(matrix[i]);
  149. #else
  150. count += bitpop16(matrix[i]);
  151. #endif
  152. }
  153. return count;
  154. }
  155. #ifdef MATRIX_HAS_GHOST
  156. inline
  157. static bool matrix_has_ghost_in_row(uint8_t row)
  158. {
  159. // no ghost exists in case less than 2 keys on
  160. if (((matrix[row] - 1) & matrix[row]) == 0)
  161. return false;
  162. // ghost exists in case same state as other row
  163. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  164. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  165. return true;
  166. }
  167. return false;
  168. }
  169. #endif
  170. inline
  171. static void _register_key(uint8_t key)
  172. {
  173. uint8_t col, row;
  174. col = key&0x07;
  175. row = (key>>3)&0x0F;
  176. if (key&0x80) {
  177. matrix[row] &= ~(1<<col);
  178. } else {
  179. matrix[row] |= (1<<col);
  180. }
  181. }