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.

matrix.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <avr/io.h>
  17. #include <util/delay.h>
  18. #include "print.h"
  19. #include "util.h"
  20. #include "debug.h"
  21. #include "ps2.h"
  22. #include "matrix.h"
  23. static void matrix_make(uint8_t code);
  24. static void matrix_break(uint8_t code);
  25. #ifdef MATRIX_HAS_GHOST
  26. static bool matrix_has_ghost_in_row(uint8_t row);
  27. #endif
  28. /*
  29. * Matrix Array usage:
  30. * 'Scan Code Set 3' is assigned into 17x8 cell matrix.
  31. *
  32. * 8bit wide
  33. * +---------+
  34. * 0| |
  35. * :| | 0x00-0x87
  36. * ;| |
  37. * 17| |
  38. * +---------+
  39. */
  40. static uint8_t matrix[MATRIX_ROWS];
  41. #define ROW(code) (code>>3)
  42. #define COL(code) (code&0x07)
  43. static bool is_modified = false;
  44. inline
  45. uint8_t matrix_rows(void)
  46. {
  47. return MATRIX_ROWS;
  48. }
  49. inline
  50. uint8_t matrix_cols(void)
  51. {
  52. return MATRIX_COLS;
  53. }
  54. void matrix_init(void)
  55. {
  56. print_enable = true;
  57. debug_enable = true;
  58. //debug_matrix = true;
  59. //debug_keyboard = true;
  60. //debug_mouse = false;
  61. ps2_host_init();
  62. // Make and Break code without Typematic
  63. while (ps2_host_send(0xF8) != 0xFA) {
  64. debug("send F8: failed\n");
  65. _delay_ms(500);
  66. }
  67. debug("send F8: OK\n");
  68. // initialize matrix state: all keys off
  69. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  70. return;
  71. }
  72. uint8_t matrix_scan(void)
  73. {
  74. // scan code reading states
  75. static enum {
  76. INIT,
  77. F0,
  78. } state = INIT;
  79. is_modified = false;
  80. uint8_t code;
  81. while ((code = ps2_host_recv())) {
  82. debug_hex(code);
  83. switch (state) {
  84. case INIT:
  85. switch (code) {
  86. case 0xF0:
  87. state = F0;
  88. debug(" ");
  89. break;
  90. default: // normal key make
  91. if (code < 0x88) {
  92. matrix_make(code);
  93. } else {
  94. debug("unexpected scan code at INIT: "); debug_hex(code); debug("\n");
  95. }
  96. state = INIT;
  97. debug("\n");
  98. }
  99. break;
  100. case F0: // Break code
  101. switch (code) {
  102. default:
  103. if (code < 0x88) {
  104. matrix_break(code);
  105. } else {
  106. debug("unexpected scan code at F0: "); debug_hex(code); debug("\n");
  107. }
  108. state = INIT;
  109. debug("\n");
  110. }
  111. break;
  112. }
  113. }
  114. return 1;
  115. }
  116. bool matrix_is_modified(void)
  117. {
  118. return is_modified;
  119. }
  120. inline
  121. bool matrix_has_ghost(void)
  122. {
  123. #ifdef MATRIX_HAS_GHOST
  124. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  125. if (matrix_has_ghost_in_row(i))
  126. return true;
  127. }
  128. #endif
  129. return false;
  130. }
  131. inline
  132. bool matrix_is_on(uint8_t row, uint8_t col)
  133. {
  134. return (matrix[row] & (1<<col));
  135. }
  136. inline
  137. uint8_t matrix_get_row(uint8_t row)
  138. {
  139. return matrix[row];
  140. }
  141. void matrix_print(void)
  142. {
  143. print("\nr/c 01234567\n");
  144. for (uint8_t row = 0; row < matrix_rows(); row++) {
  145. phex(row); print(": ");
  146. pbin_reverse(matrix_get_row(row));
  147. #ifdef MATRIX_HAS_GHOST
  148. if (matrix_has_ghost_in_row(row)) {
  149. print(" <ghost");
  150. }
  151. #endif
  152. print("\n");
  153. }
  154. }
  155. uint8_t matrix_key_count(void)
  156. {
  157. uint8_t count = 0;
  158. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  159. count += bitpop(matrix[i]);
  160. }
  161. return count;
  162. }
  163. #ifdef MATRIX_HAS_GHOST
  164. inline
  165. static bool matrix_has_ghost_in_row(uint8_t row)
  166. {
  167. // no ghost exists in case less than 2 keys on
  168. if (((matrix[row] - 1) & matrix[row]) == 0)
  169. return false;
  170. // ghost exists in case same state as other row
  171. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  172. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  173. return true;
  174. }
  175. return false;
  176. }
  177. #endif
  178. inline
  179. static void matrix_make(uint8_t code)
  180. {
  181. if (!matrix_is_on(ROW(code), COL(code))) {
  182. matrix[ROW(code)] |= 1<<COL(code);
  183. is_modified = true;
  184. }
  185. }
  186. inline
  187. static void matrix_break(uint8_t code)
  188. {
  189. if (matrix_is_on(ROW(code), COL(code))) {
  190. matrix[ROW(code)] &= ~(1<<COL(code));
  191. is_modified = true;
  192. }
  193. }