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.

219 lines
5.0 KiB

  1. /*
  2. Copyright 2014 Kai Ryu <kai1103@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 "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #ifdef UART_RGB_ENABLE
  26. #include "uart_rgb.h"
  27. #endif
  28. #ifndef DEBOUNCE
  29. # define DEBOUNCE 5
  30. #endif
  31. static uint8_t debouncing = DEBOUNCE;
  32. /* matrix state(1:on, 0:off) */
  33. static matrix_row_t matrix[MATRIX_ROWS];
  34. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  35. static matrix_row_t read_cols(void);
  36. static void init_cols(void);
  37. static void init_rows(void);
  38. static void unselect_rows(void);
  39. static void select_row(uint8_t row);
  40. inline
  41. uint8_t matrix_rows(void)
  42. {
  43. return MATRIX_ROWS;
  44. }
  45. inline
  46. uint8_t matrix_cols(void)
  47. {
  48. return MATRIX_COLS;
  49. }
  50. void matrix_init(void)
  51. {
  52. #ifdef UART_RGB_ENABLE
  53. uart_rgb_init();
  54. #endif
  55. // 85 REST
  56. DDRD |= _BV(PD7);
  57. PORTD |= _BV(PD7);
  58. // initialize row and col
  59. init_rows();
  60. init_cols();
  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. uint8_t matrix_scan(void)
  68. {
  69. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  70. select_row(i);
  71. _delay_us(30); // without this wait read unstable value.
  72. matrix_row_t cols = read_cols();
  73. if (matrix_debouncing[i] != cols) {
  74. matrix_debouncing[i] = cols;
  75. if (debouncing) {
  76. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  77. }
  78. debouncing = DEBOUNCE;
  79. }
  80. unselect_rows();
  81. }
  82. if (debouncing) {
  83. if (--debouncing) {
  84. _delay_ms(1);
  85. } else {
  86. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  87. matrix[i] = matrix_debouncing[i];
  88. }
  89. }
  90. }
  91. return 1;
  92. }
  93. bool matrix_is_modified(void)
  94. {
  95. if (debouncing) return false;
  96. return true;
  97. }
  98. inline
  99. bool matrix_is_on(uint8_t row, uint8_t col)
  100. {
  101. return (matrix[row] & ((matrix_row_t)1<<col));
  102. }
  103. inline
  104. matrix_row_t matrix_get_row(uint8_t row)
  105. {
  106. return matrix[row];
  107. }
  108. void matrix_print(void)
  109. {
  110. print("\nr/c 0123456789ABCDEF\n");
  111. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  112. print_hex8(row); print(": ");
  113. print_bin_reverse16(matrix_get_row(row));
  114. print("\n");
  115. }
  116. }
  117. uint8_t matrix_key_count(void)
  118. {
  119. uint8_t count = 0;
  120. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  121. count += bitpop16(matrix[i]);
  122. }
  123. return count;
  124. }
  125. /* Column pin configuration
  126. * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
  127. * pin: F7 F6 F5 F4 F1 F0 E6 D7 D6 D5 D1 D0 B7 B6 B0 C7
  128. * */
  129. static void init_cols(void)
  130. {
  131. // Input with pull-up(DDR:0, PORT:1)
  132. DDRF &= 0b00001100;
  133. PORTF |= 0b11110011;
  134. DDRD &= 0b00011100;
  135. PORTD |= 0b11100011;
  136. DDRB &= ~(_BV(PB6) | _BV(PB7)| _BV(PB0));
  137. PORTB |= (_BV(PB6) | _BV(PB7)| _BV(PB0));
  138. DDRE &= ~_BV(PE6);
  139. PORTE |= _BV(PE6);
  140. DDRC &= ~_BV(PC7);
  141. PORTC |= _BV(PC7);
  142. }
  143. static matrix_row_t read_cols(void)
  144. {
  145. return (PINF&_BV(PF7) ? 0 : (1<<0)) |
  146. (PINF&_BV(PF6) ? 0 : (1<<1)) |
  147. (PINF&_BV(PF5) ? 0 : (1<<2)) |
  148. (PINF&_BV(PF4) ? 0 : (1<<3)) |
  149. (PINF&_BV(PF1) ? 0 : (1<<4)) |
  150. (PINF&_BV(PF0) ? 0 : (1<<5)) |
  151. (PINE&_BV(PE6) ? 0 : (1<<6)) |
  152. (PIND&_BV(PD7) ? 0 : (1<<7)) |
  153. (PIND&_BV(PD6) ? 0 : (1<<8)) |
  154. (PIND&_BV(PD5) ? 0 : (1<<9)) |
  155. (PIND&_BV(PD1) ? 0 : (1<<10)) |
  156. (PIND&_BV(PD0) ? 0 : (1<<11)) |
  157. (PINB&_BV(PB7) ? 0 : (1<<12)) |
  158. (PINB&_BV(PB6) ? 0 : (1<<13)) |
  159. (PINB&_BV(PB0) ? 0 : (1<<14)) |
  160. (PINC&_BV(PC7) ? 0 : (1<<15));
  161. }
  162. /* Row pin configuration
  163. * row: 0 1 2 3 4 5 x
  164. * pin: B3 0 1 0 1 0 1 1
  165. * B2 0 0 1 1 0 0 1
  166. * B1 0 0 0 0 1 1 1
  167. */
  168. static void init_rows(void)
  169. {
  170. DDRB |= (1<<PB1 | 1<<PB2 | 1<<PB3);
  171. }
  172. static void unselect_rows(void)
  173. {
  174. // Hi-Z(DDR:0, PORT:0) to unselect
  175. PORTB |= (1<<PB1);
  176. PORTB |= (1<<PB2);
  177. PORTB |= (1<<PB3);
  178. }
  179. static void select_row(uint8_t row)
  180. {
  181. // Output low(DDR:1, PORT:0) to select
  182. (row & (1<<0)) ? (PORTB |= (1<<PB3)) : (PORTB &= ~(1<<PB3));
  183. (row & (1<<1)) ? (PORTB |= (1<<PB2)) : (PORTB &= ~(1<<PB2));
  184. (row & (1<<2)) ? (PORTB |= (1<<PB1)) : (PORTB &= ~(1<<PB1));
  185. }