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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_UP (CAPS | 0x80)
  30. #define ROW(key) ((key)>>3&0x0F)
  31. #define COL(key) ((key)&0x07)
  32. static bool is_modified = false;
  33. // matrix state buffer(1:on, 0:off)
  34. static uint8_t *matrix;
  35. static uint8_t _matrix0[MATRIX_ROWS];
  36. #ifdef MATRIX_HAS_GHOST
  37. static bool matrix_has_ghost_in_row(uint8_t row);
  38. #endif
  39. static void register_key(uint8_t key);
  40. inline
  41. uint8_t matrix_rows(void)
  42. {
  43. return MATRIX_ROWS;
  44. }
  45. inline
  46. uint8_t matrix_cols(void)
  47. {
  48. return MATRIX_COLS;
  49. }
  50. void matrix_init(void)
  51. {
  52. print_enable = true;
  53. debug_enable = true;
  54. debug_matrix = false;
  55. debug_keyboard = false;
  56. debug_mouse = false;
  57. print("debug enabled.\n");
  58. m0110_init();
  59. // initialize matrix state: all keys off
  60. for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
  61. matrix = _matrix0;
  62. return;
  63. }
  64. uint8_t matrix_scan(void)
  65. {
  66. uint8_t key;
  67. is_modified = false;
  68. key = m0110_recv_key();
  69. #ifdef MATRIX_HAS_LOCKING_CAPS
  70. // Send Caps key up event
  71. if (matrix_is_on(ROW(CAPS), COL(CAPS))) {
  72. is_modified = true;
  73. register_key(CAPS_UP);
  74. }
  75. #endif
  76. if (key == M0110_NULL) {
  77. return 0;
  78. } else {
  79. #ifdef MATRIX_HAS_LOCKING_CAPS
  80. if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) {
  81. // CAPS LOCK on:
  82. // Ignore LockingCaps key down event
  83. if (key == CAPS) return 0;
  84. // Convert LockingCaps key up event into down event
  85. if (key == CAPS_UP) key = CAPS;
  86. } else {
  87. // CAPS LOCK off:
  88. // Ignore LockingCaps key up event
  89. if (key == CAPS_UP) return 0;
  90. }
  91. #endif
  92. is_modified = true;
  93. register_key(key);
  94. }
  95. if (debug_enable) {
  96. print("key: "); phex(key); print("\n");
  97. }
  98. return 1;
  99. }
  100. bool matrix_is_modified(void)
  101. {
  102. return is_modified;
  103. }
  104. inline
  105. bool matrix_has_ghost(void)
  106. {
  107. #ifdef MATRIX_HAS_GHOST
  108. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  109. if (matrix_has_ghost_in_row(i))
  110. return true;
  111. }
  112. #endif
  113. return false;
  114. }
  115. inline
  116. bool matrix_is_on(uint8_t row, uint8_t col)
  117. {
  118. return (matrix[row] & (1<<col));
  119. }
  120. inline
  121. uint8_t matrix_get_row(uint8_t row)
  122. {
  123. return matrix[row];
  124. }
  125. void matrix_print(void)
  126. {
  127. print("\nr/c 01234567\n");
  128. for (uint8_t row = 0; row < matrix_rows(); row++) {
  129. phex(row); print(": ");
  130. pbin_reverse(matrix_get_row(row));
  131. print("\n");
  132. }
  133. }
  134. uint8_t matrix_key_count(void)
  135. {
  136. uint8_t count = 0;
  137. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  138. count += bitpop(matrix[i]);
  139. }
  140. return count;
  141. }
  142. #ifdef MATRIX_HAS_GHOST
  143. inline
  144. static bool matrix_has_ghost_in_row(uint8_t row)
  145. {
  146. // no ghost exists in case less than 2 keys on
  147. if (((matrix[row] - 1) & matrix[row]) == 0)
  148. return false;
  149. // ghost exists in case same state as other row
  150. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  151. if (i != row && (matrix[i] & matrix[row]) == matrix[row])
  152. return true;
  153. }
  154. return false;
  155. }
  156. #endif
  157. inline
  158. static void register_key(uint8_t key)
  159. {
  160. if (key&0x80) {
  161. matrix[ROW(key)] &= ~(1<<COL(key));
  162. } else {
  163. matrix[ROW(key)] |= (1<<COL(key));
  164. }
  165. }