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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #include "ch.h"
  15. #include "hal.h"
  16. /*
  17. * scan matrix
  18. */
  19. #include "print.h"
  20. #include "debug.h"
  21. #include "util.h"
  22. #include "matrix.h"
  23. #include "wait.h"
  24. #ifndef DEBOUNCE
  25. # define DEBOUNCE 5
  26. #endif
  27. static uint8_t debouncing = DEBOUNCE;
  28. /* matrix state(1:on, 0:off) */
  29. static matrix_row_t matrix[MATRIX_ROWS];
  30. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  31. static matrix_row_t read_cols(void);
  32. static void init_cols(void);
  33. static void unselect_rows(void);
  34. static void select_row(uint8_t row);
  35. inline
  36. uint8_t matrix_rows(void)
  37. {
  38. return MATRIX_ROWS;
  39. }
  40. inline
  41. uint8_t matrix_cols(void)
  42. {
  43. return MATRIX_COLS;
  44. }
  45. #define LED_ON() do { palSetPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13) ;} while (0)
  46. #define LED_OFF() do { palClearPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13); } while (0)
  47. #define LED_TGL() do { palTogglePad(TEENSY_PIN13_IOPORT, TEENSY_PIN13); } while (0)
  48. void matrix_init(void)
  49. {
  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. //debug
  59. debug_matrix = true;
  60. LED_ON();
  61. wait_ms(500);
  62. LED_OFF();
  63. }
  64. uint8_t matrix_scan(void)
  65. {
  66. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  67. select_row(i);
  68. wait_us(30); // without this wait read unstable value.
  69. matrix_row_t cols = read_cols();
  70. if (matrix_debouncing[i] != cols) {
  71. matrix_debouncing[i] = cols;
  72. if (debouncing) {
  73. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  74. }
  75. debouncing = DEBOUNCE;
  76. }
  77. unselect_rows();
  78. }
  79. if (debouncing) {
  80. if (--debouncing) {
  81. wait_ms(1);
  82. } else {
  83. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  84. matrix[i] = matrix_debouncing[i];
  85. }
  86. }
  87. }
  88. return 1;
  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. /* Column pin configuration
  110. */
  111. static void init_cols(void)
  112. {
  113. // internal pull-up
  114. palSetPadMode(TEENSY_PIN2_IOPORT, TEENSY_PIN2, PAL_MODE_INPUT_PULLUP);
  115. }
  116. /* Returns status of switches(1:on, 0:off) */
  117. static matrix_row_t read_cols(void)
  118. {
  119. return ((palReadPad(TEENSY_PIN2_IOPORT, TEENSY_PIN2)==PAL_HIGH) ? 0 : (1<<0));
  120. // | ((palReadPad(...)==PAL_HIGH) ? 0 : (1<<1))
  121. }
  122. /* Row pin configuration
  123. */
  124. static void unselect_rows(void)
  125. {
  126. palSetPadMode(TEENSY_PIN5_IOPORT, TEENSY_PIN5, PAL_MODE_INPUT); // hi-Z
  127. }
  128. static void select_row(uint8_t row)
  129. {
  130. (void)row;
  131. // Output low to select
  132. switch (row) {
  133. case 0:
  134. palSetPadMode(TEENSY_PIN5_IOPORT, TEENSY_PIN5, PAL_MODE_OUTPUT_PUSHPULL);
  135. palClearPad(TEENSY_PIN5_IOPORT, TEENSY_PIN5);
  136. break;
  137. }
  138. }