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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

matrix.c 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. Copyright 2014 Kai Ryu <[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 "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #include "kimera.h"
  26. #include "keymap_in_eeprom.h"
  27. #include "timer.h"
  28. #ifndef DEBOUNCE
  29. # define DEBOUNCE 5
  30. #endif
  31. /* matrix state(1:on, 0:off) */
  32. static matrix_row_t matrix[MATRIX_ROWS];
  33. #define IMPROVED_DEBOUNCE 1
  34. #if IMPROVED_DEBOUNCE
  35. #define DEBOUNCE_MASK ((1 << DEBOUNCE) - 1)
  36. static uint8_t matrix_current_row;
  37. static uint16_t matrix_row_timestamp[MATRIX_ROWS];
  38. static uint8_t matrix_debouncing[MATRIX_ROWS][MATRIX_COLS];
  39. #else
  40. static uint8_t debouncing = DEBOUNCE;
  41. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  42. #endif
  43. static uint16_t kimera_scan_timestamp;
  44. inline
  45. uint8_t matrix_rows(void)
  46. {
  47. return kimera_matrix_rows();
  48. }
  49. inline
  50. uint8_t matrix_cols(void)
  51. {
  52. return kimera_matrix_cols();
  53. }
  54. void matrix_init(void)
  55. {
  56. // disable JTAG
  57. MCUCR = (1<<JTD);
  58. MCUCR = (1<<JTD);
  59. kimera_init();
  60. kimera_scan_timestamp = timer_read();
  61. // initialize row and col
  62. kimera_unselect_rows();
  63. // initialize matrix state: all keys off
  64. #if IMPROVED_DEBOUNCE
  65. for (uint8_t i = 0; i < matrix_rows(); i++) {
  66. matrix[i] = 0;
  67. matrix_current_row = 0;
  68. matrix_row_timestamp[i] = timer_read();
  69. for (uint8_t j = 0; j < matrix_cols(); j++) {
  70. matrix_debouncing[i][j] = 0;
  71. }
  72. }
  73. #else
  74. for (uint8_t i=0; i < matrix_rows(); i++) {
  75. matrix[i] = 0;
  76. matrix_debouncing[i] = 0;
  77. }
  78. #endif
  79. PORTD &= ~(1<<PD4);
  80. }
  81. uint8_t matrix_scan(void)
  82. {
  83. /* xprintf("Row: %d, %u\n", matrix_current_row, timer_read()); */
  84. if (timer_elapsed(kimera_scan_timestamp) >= 1000) {
  85. xprintf("Scan, %u\n", kimera_scan_timestamp);
  86. kimera_scan_timestamp = timer_read();
  87. kimera_scan();
  88. }
  89. #if IMPROVED_DEBOUNCE
  90. uint16_t elapsed = timer_elapsed(matrix_row_timestamp[matrix_current_row]);
  91. if (elapsed >= 1) {
  92. matrix_row_timestamp[matrix_current_row] = timer_read();
  93. kimera_select_row(matrix_current_row);
  94. _delay_us(30);
  95. kimera_read_cols();
  96. for (uint8_t i = 0; i < matrix_cols(); i++) {
  97. uint8_t *debounce = &matrix_debouncing[matrix_current_row][i];
  98. uint8_t col = kimera_get_col(matrix_current_row, i);
  99. uint8_t count = elapsed;
  100. do {
  101. *debounce <<= 1;
  102. *debounce |= col;
  103. } while (--count);
  104. matrix_row_t *row = &matrix[matrix_current_row];
  105. matrix_row_t mask = ((matrix_row_t)1 << i);
  106. switch (*debounce & DEBOUNCE_MASK) {
  107. case DEBOUNCE_MASK:
  108. #if DEBOUNCE > 1
  109. case (DEBOUNCE_MASK >> 1):
  110. #if DEBOUNCE > 2
  111. case (DEBOUNCE_MASK >> 2):
  112. #if DEBOUNCE > 3
  113. case (DEBOUNCE_MASK >> 3):
  114. #if DEBOUNCE > 4
  115. case (DEBOUNCE_MASK >> 4):
  116. #if DEBOUNCE > 5
  117. case (DEBOUNCE_MASK >> 5):
  118. #if DEBOUNCE > 6
  119. case (DEBOUNCE_MASK >> 6):
  120. #if DEBOUNCE > 7
  121. case (DEBOUNCE_MASK >> 7):
  122. #if DEBOUNCE > 8
  123. case (DEBOUNCE_MASK >> 8):
  124. #if DEBOUNCE > 9
  125. case (DEBOUNCE_MASK >> 9):
  126. #if DEBOUNCE > 10
  127. case (DEBOUNCE_MASK >> 10):
  128. #endif
  129. #endif
  130. #endif
  131. #endif
  132. #endif
  133. #endif
  134. #endif
  135. #endif
  136. #endif
  137. #endif
  138. if ((*row & mask) == 0) {
  139. *row |= mask;
  140. }
  141. break;
  142. #if DEBOUNCE > 1
  143. case ((DEBOUNCE_MASK << 1) & DEBOUNCE_MASK):
  144. #if DEBOUNCE > 2
  145. case ((DEBOUNCE_MASK << 2) & DEBOUNCE_MASK):
  146. #if DEBOUNCE > 3
  147. case ((DEBOUNCE_MASK << 3) & DEBOUNCE_MASK):
  148. #if DEBOUNCE > 4
  149. case ((DEBOUNCE_MASK << 4) & DEBOUNCE_MASK):
  150. #if DEBOUNCE > 5
  151. case ((DEBOUNCE_MASK << 5) & DEBOUNCE_MASK):
  152. #if DEBOUNCE > 6
  153. case ((DEBOUNCE_MASK << 6) & DEBOUNCE_MASK):
  154. #if DEBOUNCE > 7
  155. case ((DEBOUNCE_MASK << 7) & DEBOUNCE_MASK):
  156. #if DEBOUNCE > 8
  157. case ((DEBOUNCE_MASK << 8) & DEBOUNCE_MASK):
  158. #if DEBOUNCE > 9
  159. case ((DEBOUNCE_MASK << 9) & DEBOUNCE_MASK):
  160. #if DEBOUNCE > 10
  161. case ((DEBOUNCE_MASK << 10) & DEBOUNCE_MASK):
  162. #endif
  163. #endif
  164. #endif
  165. #endif
  166. #endif
  167. #endif
  168. #endif
  169. #endif
  170. #endif
  171. #endif
  172. break;
  173. case 0:
  174. if ((*row & mask) != 0) {
  175. *row &= ~mask;
  176. }
  177. break;
  178. default:
  179. debug("bounce!: ");
  180. debug_bin8(*debounce & DEBOUNCE_MASK);
  181. debug("\n");
  182. break;
  183. }
  184. }
  185. kimera_unselect_rows();
  186. }
  187. matrix_current_row++;
  188. if (matrix_current_row >= matrix_rows()) {
  189. matrix_current_row = 0;
  190. }
  191. #else
  192. for (uint8_t i = 0; i < matrix_rows(); i++) {
  193. kimera_select_row(i);
  194. _delay_us(30); // without this wait read unstable value.
  195. matrix_row_t cols = kimera_read_row(i);
  196. if (matrix_debouncing[i] != cols) {
  197. matrix_debouncing[i] = cols;
  198. if (debouncing) {
  199. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  200. }
  201. debouncing = DEBOUNCE;
  202. }
  203. kimera_unselect_rows();
  204. }
  205. if (debouncing) {
  206. if (--debouncing) {
  207. _delay_ms(1);
  208. } else {
  209. for (uint8_t i = 0; i < matrix_rows(); i++) {
  210. matrix[i] = matrix_debouncing[i];
  211. }
  212. }
  213. }
  214. #endif
  215. return 1;
  216. }
  217. #if IMPROVED_DEBOUNCE
  218. #else
  219. bool matrix_is_modified(void)
  220. {
  221. if (debouncing) return false;
  222. return true;
  223. }
  224. #endif
  225. inline
  226. bool matrix_is_on(uint8_t row, uint8_t col)
  227. {
  228. return (matrix[row] & ((matrix_row_t)1<<col));
  229. }
  230. inline
  231. matrix_row_t matrix_get_row(uint8_t row)
  232. {
  233. return matrix[row];
  234. }
  235. void matrix_print(void)
  236. {
  237. print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n");
  238. for (uint8_t row = 0; row < matrix_rows(); row++) {
  239. phex(row); print(": ");
  240. print_bin_reverse32(matrix_get_row(row));
  241. print("\n");
  242. }
  243. }
  244. uint8_t matrix_key_count(void)
  245. {
  246. uint8_t count = 0;
  247. for (uint8_t i = 0; i < matrix_rows(); i++) {
  248. count += bitpop32(matrix[i]);
  249. }
  250. return count;
  251. }