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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. void matrix_init(void)
  37. {
  38. // initialize row and col
  39. unselect_rows();
  40. init_cols();
  41. // initialize matrix state: all keys off
  42. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  43. matrix[i] = 0;
  44. matrix_debouncing[i] = 0;
  45. }
  46. }
  47. uint8_t matrix_scan(void)
  48. {
  49. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  50. select_row(i);
  51. _delay_us(30); // without this wait read unstable value.
  52. matrix_row_t cols = read_cols();
  53. if (matrix_debouncing[i] != cols) {
  54. matrix_debouncing[i] = cols;
  55. if (debouncing) {
  56. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  57. }
  58. debouncing = DEBOUNCE;
  59. }
  60. unselect_rows();
  61. }
  62. if (debouncing) {
  63. if (--debouncing) {
  64. _delay_ms(1);
  65. } else {
  66. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  67. matrix[i] = matrix_debouncing[i];
  68. }
  69. }
  70. }
  71. return 1;
  72. }
  73. inline
  74. matrix_row_t matrix_get_row(uint8_t row)
  75. {
  76. return matrix[row];
  77. }
  78. /* Column pin configuration
  79. * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
  80. * pin: F0 F1 E6 C7 C6 B6 D4 B1 B0 B5 B4 D7 D6 B3 (Rev.A)
  81. * pin: B7 (Rev.B)
  82. */
  83. static void init_cols(void)
  84. {
  85. // Input with pull-up(DDR:0, PORT:1)
  86. DDRF &= ~(1<<0 | 1<<1);
  87. PORTF |= (1<<0 | 1<<1);
  88. DDRE &= ~(1<<6);
  89. PORTE |= (1<<6);
  90. DDRD &= ~(1<<7 | 1<<6 | 1<<4);
  91. PORTD |= (1<<7 | 1<<6 | 1<<4);
  92. DDRC &= ~(1<<7 | 1<<6);
  93. PORTC |= (1<<7 | 1<<6);
  94. DDRB &= ~(1<<7 | 1<<6 | 1<< 5 | 1<<4 | 1<<3 | 1<<1 | 1<<0);
  95. PORTB |= (1<<7 | 1<<6 | 1<< 5 | 1<<4 | 1<<3 | 1<<1 | 1<<0);
  96. }
  97. static matrix_row_t read_cols(void)
  98. {
  99. return (PINF&(1<<0) ? 0 : (1<<0)) |
  100. (PINF&(1<<1) ? 0 : (1<<1)) |
  101. (PINE&(1<<6) ? 0 : (1<<2)) |
  102. (PINC&(1<<7) ? 0 : (1<<3)) |
  103. (PINC&(1<<6) ? 0 : (1<<4)) |
  104. (PINB&(1<<6) ? 0 : (1<<5)) |
  105. (PIND&(1<<4) ? 0 : (1<<6)) |
  106. (PINB&(1<<1) ? 0 : (1<<7)) |
  107. ((PINB&(1<<0) && PINB&(1<<7)) ? 0 : (1<<8)) | // Rev.A and B
  108. (PINB&(1<<5) ? 0 : (1<<9)) |
  109. (PINB&(1<<4) ? 0 : (1<<10)) |
  110. (PIND&(1<<7) ? 0 : (1<<11)) |
  111. (PIND&(1<<6) ? 0 : (1<<12)) |
  112. (PINB&(1<<3) ? 0 : (1<<13));
  113. }
  114. /* Row pin configuration
  115. * row: 0 1 2 3 4
  116. * pin: D0 D1 D2 D3 D5
  117. */
  118. static void unselect_rows(void)
  119. {
  120. // Hi-Z(DDR:0, PORT:0) to unselect
  121. DDRD &= ~0b00101111;
  122. PORTD &= ~0b00101111;
  123. }
  124. static void select_row(uint8_t row)
  125. {
  126. // Output low(DDR:1, PORT:0) to select
  127. switch (row) {
  128. case 0:
  129. DDRD |= (1<<0);
  130. PORTD &= ~(1<<0);
  131. break;
  132. case 1:
  133. DDRD |= (1<<1);
  134. PORTD &= ~(1<<1);
  135. break;
  136. case 2:
  137. DDRD |= (1<<2);
  138. PORTD &= ~(1<<2);
  139. break;
  140. case 3:
  141. DDRD |= (1<<3);
  142. PORTD &= ~(1<<3);
  143. break;
  144. case 4:
  145. DDRD |= (1<<5);
  146. PORTD &= ~(1<<5);
  147. break;
  148. }
  149. }