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.

276 lines
6.7 KiB

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