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

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