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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

matrix.c 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. Copyright 2014 Kai Ryu <[email protected]>
  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. #ifndef DEBOUNCE
  26. # define DEBOUNCE 5
  27. #endif
  28. static uint8_t debouncing = DEBOUNCE;
  29. /* matrix state(1:on, 0:off) */
  30. static matrix_row_t matrix[MATRIX_ROWS];
  31. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  32. static matrix_row_t read_cols(void);
  33. static void init_cols(void);
  34. static void unselect_rows(void);
  35. static void select_row(uint8_t row);
  36. inline
  37. uint8_t matrix_rows(void)
  38. {
  39. return MATRIX_ROWS;
  40. }
  41. inline
  42. uint8_t matrix_cols(void)
  43. {
  44. return MATRIX_COLS;
  45. }
  46. void matrix_init(void)
  47. {
  48. // initialize row and col
  49. unselect_rows();
  50. init_cols();
  51. // initialize matrix state: all keys off
  52. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  53. matrix[i] = 0;
  54. matrix_debouncing[i] = 0;
  55. }
  56. }
  57. uint8_t matrix_scan(void)
  58. {
  59. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  60. select_row(i);
  61. _delay_us(30); // without this wait read unstable value.
  62. matrix_row_t cols = read_cols();
  63. if (matrix_debouncing[i] != cols) {
  64. matrix_debouncing[i] = cols;
  65. if (debouncing) {
  66. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  67. }
  68. debouncing = DEBOUNCE;
  69. }
  70. unselect_rows();
  71. }
  72. if (debouncing) {
  73. if (--debouncing) {
  74. _delay_ms(1);
  75. } else {
  76. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  77. matrix[i] = matrix_debouncing[i];
  78. }
  79. }
  80. }
  81. return 1;
  82. }
  83. bool matrix_is_modified(void)
  84. {
  85. if (debouncing) return false;
  86. return true;
  87. }
  88. inline
  89. bool matrix_is_on(uint8_t row, uint8_t col)
  90. {
  91. return (matrix[row] & ((matrix_row_t)1<<col));
  92. }
  93. inline
  94. matrix_row_t matrix_get_row(uint8_t row)
  95. {
  96. return matrix[row];
  97. }
  98. void matrix_print(void)
  99. {
  100. print("\nr/c 0123456789ABCDEF\n");
  101. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  102. phex(row); print(": ");
  103. pbin_reverse16(matrix_get_row(row));
  104. print("\n");
  105. }
  106. }
  107. uint8_t matrix_key_count(void)
  108. {
  109. uint8_t count = 0;
  110. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  111. count += bitpop16(matrix[i]);
  112. }
  113. return count;
  114. }
  115. /* Column pin configuration
  116. * pin: D0 D1 D2 D3 D4 D5 D6
  117. */
  118. static void init_cols(void)
  119. {
  120. // Input with pull-up(DDR:0, PORT:1)
  121. DDRD &= ~(1<<PD6 | 1<<PD5 | 1<<PD4 | 1<<PD3 | 1<<PD2 | 1<<PD1 | 1<<PD0);
  122. PORTD |= (1<<PD6 | 1<<PD5 | 1<<PD4 | 1<<PD3 | 1<<PD2 | 1<<PD1 | 1<<PD0);
  123. }
  124. /* Column pin configuration
  125. * col: 0 1 2 3 4 5 6
  126. * pin: D0 D1 D2 D3 D4 D5 D6
  127. */
  128. static matrix_row_t read_cols(void)
  129. {
  130. return ~PIND & ~(1<<PD7);
  131. }
  132. /* Row pin configuration
  133. * pin: B0 F0 B4 E6 F1
  134. * row: off 0 x x x x
  135. * 0 1 0 0 0 0
  136. * 1 1 0 0 0 1
  137. * 2 1 0 0 1 0
  138. * 3 1 0 0 1 1
  139. * 4 1 0 1 0 0
  140. * 5 1 0 1 0 1
  141. * 6 1 0 1 1 0
  142. * 7 1 0 1 1 1
  143. * 8 1 1 0 0 0
  144. * 9 1 1 0 0 1
  145. * 10 1 1 0 1 0
  146. * 11 1 1 0 1 1
  147. * 12 1 1 1 0 0
  148. * 13 1 1 1 0 1
  149. * 14 1 1 1 1 0
  150. * 15 1 1 1 1 1
  151. */
  152. static void unselect_rows(void)
  153. {
  154. // Output high(DDR:1, PORT:1) to unselect
  155. DDRB |= (1<<PB0);
  156. PORTB |= (1<<PB0);
  157. }
  158. static void select_row(uint8_t row)
  159. {
  160. DDRB |= (1<<PB4 | 1<<PB0);
  161. DDRE |= (1<<PE6);
  162. DDRF |= (1<<PF1 | 1<<PF0);
  163. PORTB &= ~(1<<PB0);
  164. if (row & (1<<0)) {
  165. PORTF |= (1<<PF1);
  166. }
  167. else {
  168. PORTF &= ~(1<<PF1);
  169. }
  170. if (row & (1<<1)) {
  171. PORTE |= (1<<PE6);
  172. }
  173. else {
  174. PORTE &= ~(1<<PE6);
  175. }
  176. if (row & (1<<2)) {
  177. PORTB |= (1<<PB4);
  178. }
  179. else {
  180. PORTB &= ~(1<<PB4);
  181. }
  182. if (row & (1<<3)) {
  183. PORTF |= (1<<PF0);
  184. }
  185. else {
  186. PORTF &= ~(1<<PF0);
  187. }
  188. }