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.

213 lines
4.7 KiB

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