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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. debug_enable = true;
  39. debug_matrix = true;
  40. debug_mouse = true;
  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. }
  50. uint8_t matrix_scan(void)
  51. {
  52. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  53. select_row(i);
  54. _delay_us(30); // without this wait read unstable value.
  55. matrix_row_t cols = read_cols();
  56. if (matrix_debouncing[i] != cols) {
  57. matrix_debouncing[i] = cols;
  58. if (debouncing) {
  59. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  60. }
  61. debouncing = DEBOUNCE;
  62. }
  63. unselect_rows();
  64. }
  65. if (debouncing) {
  66. if (--debouncing) {
  67. _delay_ms(1);
  68. } else {
  69. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  70. matrix[i] = matrix_debouncing[i];
  71. }
  72. }
  73. }
  74. return 1;
  75. }
  76. inline
  77. matrix_row_t matrix_get_row(uint8_t row)
  78. {
  79. return matrix[row];
  80. }
  81. /* Column pin configuration
  82. * col: 0
  83. * pin: B0
  84. */
  85. static void init_cols(void)
  86. {
  87. // Input with pull-up(DDR:0, PORT:1)
  88. DDRB &= ~(1<<0);
  89. PORTB |= (1<<0);
  90. }
  91. static matrix_row_t read_cols(void)
  92. {
  93. return (PINB&(1<<0) ? 0 : (1<<0));
  94. }
  95. /* Row pin configuration
  96. * row: 0
  97. * pin: B1
  98. */
  99. static void unselect_rows(void)
  100. {
  101. // Hi-Z(DDR:0, PORT:0) to unselect
  102. DDRB &= ~0b00000010;
  103. PORTB &= ~0b00000010;
  104. }
  105. static void select_row(uint8_t row)
  106. {
  107. // Output low(DDR:1, PORT:0) to select
  108. switch (row) {
  109. case 0:
  110. DDRB |= (1<<1);
  111. PORTB &= ~(1<<1);
  112. break;
  113. }
  114. }