logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (5848B)


  1. /*
  2. Copyright 2011 Jun Wako <wakojun@gmail.com>
  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. #define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  24. #define print_matrix_header() print("\nr/c 01234567\n")
  25. #define ROW_SHIFTER ((uint8_t)1)
  26. static void matrix_make(uint8_t code);
  27. static void matrix_break(uint8_t code);
  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. __attribute__ ((weak))
  44. void matrix_init_user(void) {
  45. }
  46. void matrix_init(void)
  47. {
  48. debug_enable = true;
  49. //debug_matrix = true;
  50. //debug_keyboard = true;
  51. //debug_mouse = false;
  52. ps2_host_init();
  53. // initialize matrix state: all keys off
  54. for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
  55. matrix_init_user();
  56. return;
  57. }
  58. uint8_t matrix_scan(void)
  59. {
  60. // scan code reading states
  61. static enum {
  62. RESET,
  63. RESET_RESPONSE,
  64. KBD_ID0,
  65. KBD_ID1,
  66. CONFIG,
  67. READY,
  68. F0_BREAK,
  69. } state = RESET;
  70. uint8_t code;
  71. if ((code = ps2_host_recv())) {
  72. dprintf("r%02X ", code);
  73. }
  74. switch (state) {
  75. case RESET:
  76. dprint("wFF ");
  77. if (ps2_host_send(0xFF) == 0xFA) {
  78. dprint("[ack]\nRESET_RESPONSE: ");
  79. state = RESET_RESPONSE;
  80. }
  81. break;
  82. case RESET_RESPONSE:
  83. if (code == 0xAA) {
  84. dprint("[ok]\nKBD_ID: ");
  85. state = KBD_ID0;
  86. } else if (code) {
  87. dprint("err\nRESET: ");
  88. state = RESET;
  89. }
  90. break;
  91. // after reset receive keyboard ID(2 bytes)
  92. case KBD_ID0:
  93. if (code) {
  94. state = KBD_ID1;
  95. }
  96. break;
  97. case KBD_ID1:
  98. if (code) {
  99. dprint("\nCONFIG: ");
  100. state = CONFIG;
  101. }
  102. break;
  103. case CONFIG:
  104. dprint("wF8 ");
  105. if (ps2_host_send(0xF8) == 0xFA) {
  106. dprint("[ack]\nREADY\n");
  107. state = READY;
  108. }
  109. break;
  110. case READY:
  111. switch (code) {
  112. case 0x00:
  113. break;
  114. case 0xF0:
  115. state = F0_BREAK;
  116. dprint(" ");
  117. break;
  118. default: // normal key make
  119. if (code < 0x88) {
  120. matrix_make(code);
  121. } else {
  122. dprintf("unexpected scan code at READY: %02X\n", code);
  123. }
  124. state = READY;
  125. dprint("\n");
  126. }
  127. break;
  128. case F0_BREAK: // Break code
  129. switch (code) {
  130. case 0x00:
  131. break;
  132. default:
  133. if (code < 0x88) {
  134. matrix_break(code);
  135. } else {
  136. dprintf("unexpected scan code at F0: %02X\n", code);
  137. }
  138. state = READY;
  139. dprint("\n");
  140. }
  141. break;
  142. }
  143. return 1;
  144. }
  145. inline
  146. uint8_t matrix_get_row(uint8_t row)
  147. {
  148. return matrix[row];
  149. }
  150. inline
  151. static void matrix_make(uint8_t code)
  152. {
  153. if (!matrix_is_on(ROW(code), COL(code))) {
  154. matrix[ROW(code)] |= 1<<COL(code);
  155. }
  156. }
  157. inline
  158. static void matrix_break(uint8_t code)
  159. {
  160. if (matrix_is_on(ROW(code), COL(code))) {
  161. matrix[ROW(code)] &= ~(1<<COL(code));
  162. }
  163. }
  164. bool matrix_is_on(uint8_t row, uint8_t col)
  165. {
  166. return (matrix_get_row(row) & (1<<col));
  167. }
  168. void matrix_print(void)
  169. {
  170. #if (MATRIX_COLS <= 8)
  171. print("r/c 01234567\n");
  172. #elif (MATRIX_COLS <= 16)
  173. print("r/c 0123456789ABCDEF\n");
  174. #elif (MATRIX_COLS <= 32)
  175. print("r/c 0123456789ABCDEF0123456789ABCDEF\n");
  176. #endif
  177. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  178. #if (MATRIX_COLS <= 8)
  179. xprintf("%02X: %08b%s\n", row, bitrev(matrix_get_row(row)),
  180. #elif (MATRIX_COLS <= 16)
  181. xprintf("%02X: %016b%s\n", row, bitrev16(matrix_get_row(row)),
  182. #elif (MATRIX_COLS <= 32)
  183. xprintf("%02X: %032b%s\n", row, bitrev32(matrix_get_row(row)),
  184. #endif
  185. #ifdef MATRIX_HAS_GHOST
  186. matrix_has_ghost_in_row(row) ? " <ghost" : ""
  187. #else
  188. ""
  189. #endif
  190. );
  191. }
  192. }
  193. #ifdef MATRIX_HAS_GHOST
  194. __attribute__ ((weak))
  195. bool matrix_has_ghost_in_row(uint8_t row)
  196. {
  197. matrix_row_t matrix_row = matrix_get_row(row);
  198. // No ghost exists when less than 2 keys are down on the row
  199. if (((matrix_row - 1) & matrix_row) == 0)
  200. return false;
  201. // Ghost occurs when the row shares column line with other row
  202. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  203. if (i != row && (matrix_get_row(i) & matrix_row))
  204. return true;
  205. }
  206. return false;
  207. }
  208. #endif