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 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 "m0110.h"
  27. #include "matrix.h"
  28. #define CAPS 0x39
  29. #define CAPS_BREAK (CAPS | 0x80)
  30. #define ROW(key) ((key)>>3&0x0F)
  31. #define COL(key) ((key)&0x07)
  32. #define ARROW_UP_BREAK (0x4D | 0x80)
  33. #define ARROW_DOWN_BREAK (0x48 | 0x80)
  34. #define ARROW_LEFT_BREAK (0x46 | 0x80)
  35. #define ARROW_RIGHT_BREAK (0x42 | 0x80)
  36. static bool is_modified = false;
  37. // matrix state buffer(1:on, 0:off)
  38. static uint8_t *matrix;
  39. static uint8_t _matrix0[MATRIX_ROWS];
  40. #ifdef MATRIX_HAS_GHOST
  41. static bool matrix_has_ghost_in_row(uint8_t row);
  42. #endif
  43. static void register_key(uint8_t key);
  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 = false;
  59. debug_keyboard = false;
  60. debug_mouse = false;
  61. print("debug enabled.\n");
  62. m0110_init();
  63. // initialize matrix state: all keys off
  64. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  65. matrix = _matrix0;
  66. return;
  67. }
  68. uint8_t matrix_scan(void)
  69. {
  70. uint8_t key;
  71. is_modified = false;
  72. key = m0110_recv_key();
  73. #ifdef MATRIX_HAS_LOCKING_CAPS
  74. // Send Caps key up event
  75. if (matrix_is_on(ROW(CAPS), COL(CAPS))) {
  76. is_modified = true;
  77. register_key(CAPS_BREAK);
  78. }
  79. #endif
  80. if (key == M0110_NULL) {
  81. return 0;
  82. } else if (key == M0110_ERROR) {
  83. return 0;
  84. } else if (key == ARROW_UP_BREAK ||
  85. key == ARROW_DOWN_BREAK ||
  86. key == ARROW_LEFT_BREAK ||
  87. key == ARROW_RIGHT_BREAK) {
  88. // WORK AROUND: exceptional handling for M0110A
  89. // Unregister both Arrow key and coressponding Calc key when receive Arrow key break.
  90. //
  91. // Shift + Calc keys(=/*+):
  92. // Send no Shift break(0xF1) when release Calc keys. Can't be desinguished from Arrow keys.
  93. // (press: 0x71, 0x79, 0xXX release: 0x79, 0xXX)
  94. // See m0110.c for key events and scan codes.
  95. is_modified = true;
  96. register_key(key);
  97. register_key(key|M0110_CALC_OFFSET);
  98. } else {
  99. #ifdef MATRIX_HAS_LOCKING_CAPS
  100. if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) {
  101. // CAPS LOCK on:
  102. // Ignore LockingCaps key down event
  103. if (key == CAPS) return 0;
  104. // Convert LockingCaps key up event into down event
  105. if (key == CAPS_BREAK) key = CAPS;
  106. } else {
  107. // CAPS LOCK off:
  108. // Ignore LockingCaps key up event
  109. if (key == CAPS_BREAK) return 0;
  110. }
  111. #endif
  112. is_modified = true;
  113. register_key(key);
  114. }
  115. if (debug_enable) {
  116. print("key: "); phex(key); print("\n");
  117. }
  118. return 1;
  119. }
  120. bool matrix_is_modified(void)
  121. {
  122. return is_modified;
  123. }
  124. inline
  125. bool matrix_has_ghost(void)
  126. {
  127. #ifdef MATRIX_HAS_GHOST
  128. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  129. if (matrix_has_ghost_in_row(i))
  130. return true;
  131. }
  132. #endif
  133. return false;
  134. }
  135. inline
  136. bool matrix_is_on(uint8_t row, uint8_t col)
  137. {
  138. return (matrix[row] & (1<<col));
  139. }
  140. inline
  141. uint8_t matrix_get_row(uint8_t row)
  142. {
  143. return matrix[row];
  144. }
  145. void matrix_print(void)
  146. {
  147. print("\nr/c 01234567\n");
  148. for (uint8_t row = 0; row < matrix_rows(); row++) {
  149. phex(row); print(": ");
  150. pbin_reverse(matrix_get_row(row));
  151. print("\n");
  152. }
  153. }
  154. uint8_t matrix_key_count(void)
  155. {
  156. uint8_t count = 0;
  157. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  158. count += bitpop(matrix[i]);
  159. }
  160. return count;
  161. }
  162. #ifdef MATRIX_HAS_GHOST
  163. inline
  164. static bool matrix_has_ghost_in_row(uint8_t row)
  165. {
  166. // no ghost exists in case less than 2 keys on
  167. if (((matrix[row] - 1) & matrix[row]) == 0)
  168. return false;
  169. // ghost exists in case same state as other row
  170. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  171. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  172. return true;
  173. }
  174. return false;
  175. }
  176. #endif
  177. inline
  178. static void register_key(uint8_t key)
  179. {
  180. if (key&0x80) {
  181. matrix[ROW(key)] &= ~(1<<COL(key));
  182. } else {
  183. matrix[ROW(key)] |= (1<<COL(key));
  184. }
  185. }