Keyboard firmwares for Atmel AVR and Cortex-M
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.

matrix.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. Copyright 2012 Jun Wako <[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. UCSR0B = 0; //Turn off serial
  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(100); // 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. return 1;
  83. }
  84. bool matrix_is_modified(void)
  85. {
  86. if (debouncing) return false;
  87. return true;
  88. }
  89. inline
  90. bool matrix_is_on(uint8_t row, uint8_t col)
  91. {
  92. return (matrix[row] & ((matrix_row_t)1<<col));
  93. }
  94. inline
  95. matrix_row_t matrix_get_row(uint8_t row)
  96. {
  97. return matrix[row];
  98. }
  99. void matrix_print(void)
  100. {
  101. print("\nr/c 0123456789ABCDEF\n");
  102. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  103. phex(row); print(": ");
  104. pbin_reverse16(matrix_get_row(row));
  105. print("\n");
  106. }
  107. }
  108. uint8_t matrix_key_count(void)
  109. {
  110. uint8_t count = 0;
  111. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  112. count += bitpop16(matrix[i]);
  113. }
  114. return count;
  115. }
  116. /* Column pin configuration
  117. * col: 0 1 2 3 4 5 6 7 8 9 10 11
  118. * pin: B0 B1 B2 C0 C1 C2 C3 C4 C5 D0 D1 B4
  119. */
  120. static void init_cols(void)
  121. {
  122. // Input with pull-up(DDR:0, PORT:1)
  123. DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<4);
  124. PORTB |= (1<<0 | 1<<1 | 1<<2 | 1<<4);
  125. DDRC &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5);
  126. PORTC |= (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5);
  127. DDRD &= ~(1<<0 | 1<<1);
  128. PORTD |= (1<<0 | 1<<1);
  129. }
  130. static matrix_row_t read_cols(void)
  131. {
  132. return (PINB&(1<<0) ? 0 : (1<<0)) |
  133. (PINB&(1<<1) ? 0 : (1<<1)) |
  134. (PINB&(1<<2) ? 0 : (1<<2)) |
  135. (PINC&(1<<0) ? 0 : (1<<3)) |
  136. (PINC&(1<<1) ? 0 : (1<<4)) |
  137. (PINC&(1<<2) ? 0 : (1<<5)) |
  138. (PINC&(1<<3) ? 0 : (1<<6)) |
  139. (PINC&(1<<4) ? 0 : (1<<7)) |
  140. (PINC&(1<<5) ? 0 : (1<<8)) |
  141. (PIND&(1<<0) ? 0 : (1<<9)) |
  142. (PIND&(1<<1) ? 0 : (1<<10)) |
  143. (PINB&(1<<4) ? 0 : (1<<11));
  144. }
  145. /* Row pin configuration
  146. * row: 0 1 2 3 4
  147. * pin: D6 D5 D3 B3 B5
  148. */
  149. static void unselect_rows(void)
  150. {
  151. // Hi-Z(DDR:0, PORT:0) to unselect
  152. DDRB &= ~0b00101000;
  153. PORTB &= ~0b00101000;
  154. DDRD &= ~0b01101000;
  155. PORTD &= ~0b01101000;
  156. }
  157. static void select_row(uint8_t row)
  158. {
  159. // Output low(DDR:1, PORT:0) to select
  160. switch (row) {
  161. case 0:
  162. DDRD |= (1<<6);
  163. PORTD &= ~(1<<6);
  164. break;
  165. case 1:
  166. DDRD |= (1<<5);
  167. PORTD &= ~(1<<5);
  168. break;
  169. case 2:
  170. DDRD |= (1<<3);
  171. PORTD &= ~(1<<3);
  172. break;
  173. case 3:
  174. DDRB |= (1<<3);
  175. PORTB &= ~(1<<3);
  176. break;
  177. case 4:
  178. DDRB |= (1<<5);
  179. PORTB &= ~(1<<5);
  180. break;
  181. }
  182. }