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 7.0KB

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