matrix.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. // // HACK backlighting
  145. // for (uint8_t t = 0; t < meira_get_backlight_level(); t++) {
  146. // for (uint8_t x = 0; x < 13; x++) {
  147. // for (uint8_t y = 0; y < 4; y++) {
  148. // uint8_t pin = lcol_pins[y];
  149. // if ((x >> y) & 1) {
  150. // _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  151. // } else {
  152. // _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LO
  153. // }
  154. // }
  155. // }
  156. // }
  157. return ret;
  158. }
  159. bool matrix_is_modified(void)
  160. {
  161. if (debouncing) return false;
  162. return true;
  163. }
  164. inline
  165. bool matrix_is_on(uint8_t row, uint8_t col)
  166. {
  167. return (matrix[row] & ((matrix_row_t)1<<col));
  168. }
  169. inline
  170. matrix_row_t matrix_get_row(uint8_t row)
  171. {
  172. return matrix[row];
  173. }
  174. void matrix_print(void)
  175. {
  176. print("\nr/c 0123456789ABCDEF\n");
  177. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  178. phex(row); print(": ");
  179. pbin_reverse16(matrix_get_row(row));
  180. print("\n");
  181. }
  182. }
  183. uint8_t matrix_key_count(void)
  184. {
  185. uint8_t count = 0;
  186. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  187. count += bitpop16(matrix[i]);
  188. }
  189. return count;
  190. }
  191. static void init_rows(void)
  192. {
  193. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  194. uint8_t pin = row_pins[x];
  195. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  196. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  197. // // HACK backlighting
  198. // uint8_t lpin = lrow_pins[x];
  199. // _SFR_IO8((lpin >> 4) + 1) |= _BV(lpin & 0xF); // OUT
  200. // _SFR_IO8((lpin >> 4) + 2) |= _BV(lpin & 0xF); // HI
  201. }
  202. }
  203. //static void init_lcols(void)
  204. //{
  205. // for (uint8_t x = 0; x < 4; x++) {
  206. // uint8_t pin = lcol_pins[x];
  207. // _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  208. // _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HIGH
  209. // }
  210. //}
  211. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  212. {
  213. bool matrix_changed = false;
  214. // Select col and wait for col selection to stabilize
  215. select_col(current_col);
  216. wait_us(30);
  217. // For each row...
  218. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  219. {
  220. // Store last value of row prior to reading
  221. matrix_row_t last_row_value = current_matrix[row_index];
  222. // Check row pin state
  223. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  224. {
  225. // Pin LO, set col bit
  226. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  227. }
  228. else
  229. {
  230. // Pin HI, clear col bit
  231. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  232. }
  233. // Determine if the matrix changed state
  234. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  235. {
  236. matrix_changed = true;
  237. }
  238. }
  239. // Unselect col
  240. unselect_cols();
  241. return matrix_changed;
  242. }
  243. static void select_col(uint8_t col)
  244. {
  245. #ifdef FLIPPED_BOARD
  246. col = MATRIX_COLS - col - 1;
  247. #endif
  248. for(uint8_t x = 0; x < 4; x++) {
  249. uint8_t pin = col_pins[x];
  250. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  251. if (((col >> x) & 0x1) == 1){
  252. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HIGH
  253. } else {
  254. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  255. }
  256. }
  257. }
  258. static void unselect_cols(void)
  259. {
  260. // FIXME This really needs to use the global enable on the decoder, because currently this sets the value to col1
  261. for(uint8_t x = 0; x < 4; x++) {
  262. uint8_t pin = col_pins[x];
  263. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  264. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  265. }
  266. }