matrix.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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_quantum(void) {
  69. matrix_init_kb();
  70. }
  71. __attribute__ ((weak))
  72. void matrix_scan_quantum(void) {
  73. matrix_scan_kb();
  74. }
  75. __attribute__ ((weak))
  76. void matrix_init_kb(void) {
  77. matrix_init_user();
  78. }
  79. __attribute__ ((weak))
  80. void matrix_scan_kb(void) {
  81. matrix_scan_user();
  82. }
  83. __attribute__ ((weak))
  84. void matrix_init_user(void) {
  85. }
  86. __attribute__ ((weak))
  87. void matrix_scan_user(void) {
  88. }
  89. inline
  90. uint8_t matrix_rows(void)
  91. {
  92. return MATRIX_ROWS;
  93. }
  94. inline
  95. uint8_t matrix_cols(void)
  96. {
  97. return MATRIX_COLS;
  98. }
  99. void matrix_init(void)
  100. {
  101. debug_enable = true;
  102. debug_matrix = true;
  103. debug_mouse = true;
  104. // initialize row and col
  105. unselect_cols();
  106. init_rows();
  107. // init_lcols();
  108. // TX_RX_LED_INIT;
  109. // initialize matrix state: all keys off
  110. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  111. matrix[i] = 0;
  112. matrix_debouncing[i] = 0;
  113. }
  114. matrix_init_quantum();
  115. }
  116. uint8_t _matrix_scan(void)
  117. {
  118. // Set col, read rows
  119. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  120. # if (DEBOUNCING_DELAY > 0)
  121. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  122. if (matrix_changed) {
  123. debouncing = true;
  124. debouncing_time = timer_read();
  125. }
  126. # else
  127. read_rows_on_col(matrix, current_col);
  128. # endif
  129. }
  130. # if (DEBOUNCING_DELAY > 0)
  131. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
  132. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  133. matrix[i] = matrix_debouncing[i];
  134. }
  135. debouncing = false;
  136. }
  137. # endif
  138. return 1;
  139. }
  140. uint8_t matrix_scan(void)
  141. {
  142. uint8_t ret = _matrix_scan();
  143. matrix_scan_quantum();
  144. return ret;
  145. }
  146. bool matrix_is_modified(void)
  147. {
  148. if (debouncing) return false;
  149. return true;
  150. }
  151. inline
  152. bool matrix_is_on(uint8_t row, uint8_t col)
  153. {
  154. return (matrix[row] & ((matrix_row_t)1<<col));
  155. }
  156. inline
  157. matrix_row_t matrix_get_row(uint8_t row)
  158. {
  159. return matrix[row];
  160. }
  161. void matrix_print(void)
  162. {
  163. print("\nr/c 0123456789ABCDEF\n");
  164. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  165. phex(row); print(": ");
  166. pbin_reverse16(matrix_get_row(row));
  167. print("\n");
  168. }
  169. }
  170. uint8_t matrix_key_count(void)
  171. {
  172. uint8_t count = 0;
  173. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  174. count += bitpop16(matrix[i]);
  175. }
  176. return count;
  177. }
  178. static void init_rows(void)
  179. {
  180. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  181. uint8_t pin = row_pins[x];
  182. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  183. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  184. }
  185. }
  186. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  187. {
  188. bool matrix_changed = false;
  189. // Select col and wait for col selection to stabilize
  190. select_col(current_col);
  191. wait_us(30);
  192. // For each row...
  193. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  194. {
  195. // Store last value of row prior to reading
  196. matrix_row_t last_row_value = current_matrix[row_index];
  197. // Check row pin state
  198. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  199. {
  200. // Pin LO, set col bit
  201. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  202. }
  203. else
  204. {
  205. // Pin HI, clear col bit
  206. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  207. }
  208. // Determine if the matrix changed state
  209. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  210. {
  211. matrix_changed = true;
  212. }
  213. }
  214. // Unselect col
  215. unselect_cols();
  216. return matrix_changed;
  217. }
  218. static void select_col(uint8_t col)
  219. {
  220. #ifdef FLIPPED_BOARD
  221. col = MATRIX_COLS - col - 1;
  222. #endif
  223. for(uint8_t x = 0; x < 4; x++) {
  224. uint8_t pin = col_pins[x];
  225. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  226. if (((col >> x) & 0x1) == 1){
  227. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HIGH
  228. } else {
  229. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  230. }
  231. }
  232. }
  233. static void unselect_cols(void)
  234. {
  235. // FIXME This really needs to use the global enable on the decoder, because currently this sets the value to col1
  236. for(uint8_t x = 0; x < 4; x++) {
  237. uint8_t pin = col_pins[x];
  238. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  239. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  240. }
  241. }