matrix_center.c 5.9 KB

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