matrix.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. Copyright 2012 Jun Wako <wakojun@gmail.com>
  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 "action_layer.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "util.h"
  25. #include "matrix.h"
  26. #ifndef DEBOUNCE
  27. # define DEBOUNCE 10
  28. #endif
  29. static uint8_t debouncing = DEBOUNCE;
  30. /* matrix state(1:on, 0:off) */
  31. static matrix_row_t matrix[MATRIX_ROWS];
  32. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  33. static matrix_row_t read_cols(void);
  34. static void init_cols(void);
  35. static void unselect_rows(void);
  36. static void select_row(uint8_t row);
  37. inline
  38. uint8_t matrix_rows(void)
  39. {
  40. return MATRIX_ROWS;
  41. }
  42. inline
  43. uint8_t matrix_cols(void)
  44. {
  45. return MATRIX_COLS;
  46. }
  47. static
  48. void setup_leds(void) {
  49. DDRF |= 0x00;
  50. PORTF |= 0x00;
  51. }
  52. void matrix_init(void)
  53. {
  54. // initialize row and col
  55. unselect_rows();
  56. init_cols();
  57. setup_leds();
  58. // initialize matrix state: all keys off
  59. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  60. matrix[i] = 0;
  61. matrix_debouncing[i] = 0;
  62. }
  63. }
  64. uint8_t matrix_scan(void)
  65. {
  66. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  67. select_row(i);
  68. _delay_us(30); // without this wait read unstable value.
  69. matrix_row_t cols = read_cols();
  70. if (matrix_debouncing[i] != cols) {
  71. matrix_debouncing[i] = cols;
  72. if (debouncing) {
  73. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  74. }
  75. debouncing = DEBOUNCE;
  76. }
  77. unselect_rows();
  78. }
  79. if (debouncing) {
  80. if (--debouncing) {
  81. _delay_ms(1);
  82. } else {
  83. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  84. matrix[i] = matrix_debouncing[i];
  85. }
  86. }
  87. }
  88. // uint8_t layer = biton32(default_layer_state);
  89. switch (default_layer_state) {
  90. case 1:
  91. DDRF &= ~(1<<0);
  92. PORTF &= ~(1<<0);
  93. break;
  94. case 2:
  95. DDRF |= (1<<0);
  96. PORTF |= (1<<0);
  97. break;
  98. }
  99. return 1;
  100. }
  101. bool matrix_is_modified(void)
  102. {
  103. if (debouncing) return false;
  104. return true;
  105. }
  106. inline
  107. bool matrix_is_on(uint8_t row, uint8_t col)
  108. {
  109. return (matrix[row] & ((matrix_row_t)1<<col));
  110. }
  111. inline
  112. matrix_row_t matrix_get_row(uint8_t row)
  113. {
  114. return matrix[row];
  115. }
  116. void matrix_print(void)
  117. {
  118. print("\nr/c 0123456789ABCDEF\n");
  119. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  120. phex(row); print(": ");
  121. pbin_reverse16(matrix_get_row(row));
  122. print("\n");
  123. }
  124. }
  125. uint8_t matrix_key_count(void)
  126. {
  127. uint8_t count = 0;
  128. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  129. count += bitpop16(matrix[i]);
  130. }
  131. return count;
  132. }
  133. /* Column pin configuration
  134. * col: 0 1 2 3 4 5 6 7 8 9 10 11
  135. * pin: F0 F1 F4 F5 F6 F7 B6 B5 B4 D7 D5 D4
  136. */
  137. static void init_cols(void)
  138. {
  139. DDRC &= ~(1<<6 | 1<<7);
  140. PORTC |= (1<<6 | 1<<7);
  141. DDRD &= ~(1<<4 | 1<<5 | 1<<6 | 1<<7);
  142. PORTD |= (1<<4 | 1<<5 | 1<<6 | 1<<7);
  143. DDRB &= ~(1<<4 | 1<<5 | 1<<6);
  144. PORTB |= (1<<4 | 1<<5 | 1<<6);
  145. DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
  146. PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
  147. }
  148. static matrix_row_t read_cols(void)
  149. {
  150. return (PINC&(1<<6) ? 0 : (1<< 0)) |
  151. (PINC&(1<<7) ? 0 : (1<< 1)) |
  152. (PIND&(1<<5) ? 0 : (1<< 2)) |
  153. (PIND&(1<<4) ? 0 : (1<< 3)) |
  154. (PIND&(1<<6) ? 0 : (1<< 4)) |
  155. (PIND&(1<<7) ? 0 : (1<< 5)) |
  156. (PINB&(1<<4) ? 0 : (1<< 6)) |
  157. (PINB&(1<<5) ? 0 : (1<< 7)) |
  158. (PINB&(1<<6) ? 0 : (1<< 8)) |
  159. (PINF&(1<<7) ? 0 : (1<< 9)) |
  160. (PINF&(1<<6) ? 0 : (1<<10)) |
  161. (PINF&(1<<5) ? 0 : (1<<11)) |
  162. (PINF&(1<<4) ? 0 : (1<<12)) |
  163. (PINF&(1<<1) ? 0 : (1<<13)) |
  164. (PINF&(1<<0) ? 0 : (1<<14));
  165. }
  166. /* Row pin configuration
  167. * row: 0 1 2 3
  168. * pin: B0 B1 B2 B3
  169. */
  170. static void unselect_rows(void)
  171. {
  172. // Hi-Z(DDR:0, PORT:0) to unselect
  173. DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
  174. PORTB |= (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
  175. }
  176. static void select_row(uint8_t row)
  177. {
  178. switch (row) {
  179. case 0:
  180. DDRB |= (1<<0);
  181. PORTB &= ~(1<<0);
  182. break;
  183. case 1:
  184. DDRB |= (1<<1);
  185. PORTB &= ~(1<<1);
  186. break;
  187. case 2:
  188. DDRB |= (1<<2);
  189. PORTB &= ~(1<<2);
  190. break;
  191. case 3:
  192. DDRB |= (1<<3);
  193. PORTB &= ~(1<<3);
  194. break;
  195. case 4:
  196. DDRB |= (1<<7);
  197. PORTB &= ~(1<<7);
  198. break;
  199. }
  200. }