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.

215 lines
4.8 KiB

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