matrix.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. Copyright 2012 Jun Wako <wakojun@gmail.com>
  3. Copyright 2017 Cole Markham <cole@ccmcomputing.net>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. * scan matrix
  17. */
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #if defined(__AVR__)
  21. #include <avr/io.h>
  22. #endif
  23. #include "meira.h"
  24. #include "wait.h"
  25. #include "print.h"
  26. #include "debug.h"
  27. #include "util.h"
  28. #include "matrix.h"
  29. #include "config.h"
  30. #include "timer.h"
  31. #ifndef DEBOUNCING_DELAY
  32. # define DEBOUNCING_DELAY 5
  33. #endif
  34. #if (DEBOUNCING_DELAY > 0)
  35. static uint16_t debouncing_time;
  36. static bool debouncing = false;
  37. #endif
  38. #if (MATRIX_COLS <= 8)
  39. # define print_matrix_header() print("\nr/c 01234567\n")
  40. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  41. # define matrix_bitpop(i) bitpop(matrix[i])
  42. # define ROW_SHIFTER ((uint8_t)1)
  43. #elif (MATRIX_COLS <= 16)
  44. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  45. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  46. # define matrix_bitpop(i) bitpop16(matrix[i])
  47. # define ROW_SHIFTER ((uint16_t)1)
  48. #elif (MATRIX_COLS <= 32)
  49. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  50. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  51. # define matrix_bitpop(i) bitpop32(matrix[i])
  52. # define ROW_SHIFTER ((uint32_t)1)
  53. #endif
  54. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  55. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  56. static const uint8_t col_pins[4] = MATRIX_COL_PINS;
  57. //static const uint8_t lrow_pins[MATRIX_ROWS] = LED_ROW_PINS;
  58. //static const uint8_t lcol_pins[4] = LED_COL_PINS;
  59. /* matrix state(1:on, 0:off) */
  60. static matrix_row_t matrix[MATRIX_ROWS];
  61. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  62. static void init_rows(void);
  63. //static void init_lcols(void);
  64. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  65. static void unselect_cols(void);
  66. static void select_col(uint8_t col);
  67. __attribute__ ((weak))
  68. void matrix_init_kb(void) {
  69. matrix_init_user();
  70. }
  71. __attribute__ ((weak))
  72. void matrix_scan_kb(void) {
  73. matrix_scan_user();
  74. }
  75. __attribute__ ((weak))
  76. void matrix_init_user(void) {
  77. }
  78. __attribute__ ((weak))
  79. void matrix_scan_user(void) {
  80. }
  81. inline
  82. uint8_t matrix_rows(void)
  83. {
  84. return MATRIX_ROWS;
  85. }
  86. inline
  87. uint8_t matrix_cols(void)
  88. {
  89. return MATRIX_COLS;
  90. }
  91. void matrix_init(void)
  92. {
  93. debug_enable = true;
  94. debug_matrix = true;
  95. debug_mouse = true;
  96. // initialize row and col
  97. unselect_cols();
  98. init_rows();
  99. // init_lcols();
  100. // TX_RX_LED_INIT;
  101. // initialize matrix state: all keys off
  102. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  103. matrix[i] = 0;
  104. matrix_debouncing[i] = 0;
  105. }
  106. matrix_init_quantum();
  107. }
  108. uint8_t _matrix_scan(void)
  109. {
  110. // Set col, read rows
  111. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  112. # if (DEBOUNCING_DELAY > 0)
  113. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  114. if (matrix_changed) {
  115. debouncing = true;
  116. debouncing_time = timer_read();
  117. }
  118. # else
  119. read_rows_on_col(matrix, current_col);
  120. # endif
  121. }
  122. # if (DEBOUNCING_DELAY > 0)
  123. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
  124. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  125. matrix[i] = matrix_debouncing[i];
  126. }
  127. debouncing = false;
  128. }
  129. # endif
  130. return 1;
  131. }
  132. uint8_t matrix_scan(void)
  133. {
  134. uint8_t ret = _matrix_scan();
  135. matrix_scan_quantum();
  136. return ret;
  137. }
  138. bool matrix_is_modified(void)
  139. {
  140. if (debouncing) return false;
  141. return true;
  142. }
  143. inline
  144. bool matrix_is_on(uint8_t row, uint8_t col)
  145. {
  146. return (matrix[row] & ((matrix_row_t)1<<col));
  147. }
  148. inline
  149. matrix_row_t matrix_get_row(uint8_t row)
  150. {
  151. return matrix[row];
  152. }
  153. void matrix_print(void)
  154. {
  155. print("\nr/c 0123456789ABCDEF\n");
  156. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  157. phex(row); print(": ");
  158. pbin_reverse16(matrix_get_row(row));
  159. print("\n");
  160. }
  161. }
  162. uint8_t matrix_key_count(void)
  163. {
  164. uint8_t count = 0;
  165. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  166. count += bitpop16(matrix[i]);
  167. }
  168. return count;
  169. }
  170. static void init_rows(void)
  171. {
  172. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  173. uint8_t pin = row_pins[x];
  174. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  175. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  176. }
  177. }
  178. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  179. {
  180. bool matrix_changed = false;
  181. // Select col and wait for col selection to stabilize
  182. select_col(current_col);
  183. wait_us(30);
  184. // For each row...
  185. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  186. {
  187. // Store last value of row prior to reading
  188. matrix_row_t last_row_value = current_matrix[row_index];
  189. // Check row pin state
  190. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  191. {
  192. // Pin LO, set col bit
  193. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  194. }
  195. else
  196. {
  197. // Pin HI, clear col bit
  198. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  199. }
  200. // Determine if the matrix changed state
  201. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  202. {
  203. matrix_changed = true;
  204. }
  205. }
  206. // Unselect col
  207. unselect_cols();
  208. return matrix_changed;
  209. }
  210. static void select_col(uint8_t col)
  211. {
  212. #ifdef FLIPPED_BOARD
  213. col = MATRIX_COLS - col - 1;
  214. #endif
  215. for(uint8_t x = 0; x < 4; x++) {
  216. uint8_t pin = col_pins[x];
  217. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  218. if (((col >> x) & 0x1) == 1){
  219. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HIGH
  220. } else {
  221. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  222. }
  223. }
  224. }
  225. static void unselect_cols(void)
  226. {
  227. // FIXME This really needs to use the global enable on the decoder, because currently this sets the value to col1
  228. for(uint8_t x = 0; x < 4; x++) {
  229. uint8_t pin = col_pins[x];
  230. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  231. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  232. }
  233. }