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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. debug_enable = true;
  49. debug_matrix = true;
  50. // initialize row and col
  51. unselect_rows();
  52. init_cols();
  53. // initialize matrix state: all keys off
  54. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  55. matrix[i] = 0;
  56. matrix_debouncing[i] = 0;
  57. }
  58. }
  59. uint8_t matrix_scan(void)
  60. {
  61. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  62. select_row(i);
  63. _delay_us(30); // without this wait read unstable value.
  64. matrix_row_t cols = read_cols();
  65. if (matrix_debouncing[i] != cols) {
  66. matrix_debouncing[i] = cols;
  67. if (debouncing) {
  68. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  69. }
  70. debouncing = DEBOUNCE;
  71. }
  72. unselect_rows();
  73. }
  74. if (debouncing) {
  75. if (--debouncing) {
  76. _delay_ms(1);
  77. } else {
  78. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  79. matrix[i] = matrix_debouncing[i];
  80. }
  81. }
  82. }
  83. return 1;
  84. }
  85. bool matrix_is_modified(void)
  86. {
  87. if (debouncing) return false;
  88. return true;
  89. }
  90. inline
  91. bool matrix_is_on(uint8_t row, uint8_t col)
  92. {
  93. return (matrix[row] & ((matrix_row_t)1<<col));
  94. }
  95. inline
  96. matrix_row_t matrix_get_row(uint8_t row)
  97. {
  98. return matrix[row];
  99. }
  100. void matrix_print(void)
  101. {
  102. print("\nr/c 0123456789ABCDEF\n");
  103. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  104. phex(row); print(": ");
  105. pbin_reverse16(matrix_get_row(row));
  106. print("\n");
  107. }
  108. }
  109. uint8_t matrix_key_count(void)
  110. {
  111. uint8_t count = 0;
  112. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  113. count += bitpop16(matrix[i]);
  114. }
  115. return count;
  116. }
  117. /* Column pin configuration
  118. * col: 0
  119. * pin: D0
  120. */
  121. static void init_cols(void)
  122. {
  123. // Input with pull-up(DDR:0, PORT:1)
  124. DDRD &= ~(1<<0);
  125. PORTD |= (1<<0);
  126. }
  127. static matrix_row_t read_cols(void)
  128. {
  129. return (PIND&(1<<0) ? 0 : (1<<0));
  130. }
  131. /* Row pin configuration
  132. * row: 0
  133. * pin: D1
  134. */
  135. static void unselect_rows(void)
  136. {
  137. // Hi-Z(DDR:0, PORT:0) to unselect
  138. DDRD &= ~0b00000010;
  139. PORTD &= ~0b00000010;
  140. }
  141. static void select_row(uint8_t row)
  142. {
  143. // Output low(DDR:1, PORT:0) to select
  144. switch (row) {
  145. case 0:
  146. DDRD |= (1<<1);
  147. PORTD &= ~(1<<1);
  148. break;
  149. }
  150. }