matrix.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. Copyright 2012 Jun Wako <wakojun@gmail.com>
  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. return MATRIX_ROWS;
  38. }
  39. inline
  40. uint8_t matrix_cols(void) {
  41. return MATRIX_COLS;
  42. }
  43. void matrix_init(void) {
  44. // initialize row and col
  45. palSetPadMode(TEENSY_PIN13_IOPORT, TEENSY_PIN13, PAL_MODE_OUTPUT_PUSHPULL);
  46. palSetPadMode(TEENSY_PIN12_IOPORT, TEENSY_PIN12, PAL_MODE_OUTPUT_PUSHPULL);
  47. palSetPadMode(TEENSY_PIN18_IOPORT, TEENSY_PIN18, PAL_MODE_OUTPUT_PUSHPULL);
  48. init_cols();
  49. // initialize LED
  50. //palSetPadMode(TEENSY_PIN20_IOPORT, TEENSY_PIN20, PAL_MODE_OUTPUT_PUSHPULL);
  51. // Turn on C's LED
  52. //palClearPad(TEENSY_PIN20_IOPORT, TEENSY_PIN20);
  53. //palSetPad(TEENSY_PIN10_IOPORT, TEENSY_PIN10);
  54. // initialize matrix state: all keys off
  55. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  56. matrix[i] = 0;
  57. matrix_debouncing[i] = 0;
  58. }
  59. //debug
  60. debug_matrix = true;
  61. }
  62. uint8_t matrix_scan(void) {
  63. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  64. select_row(i);
  65. wait_us(30); // without this wait read unstable value.
  66. matrix_row_t cols = read_cols();
  67. printf("row:%d cols:%d\n", i, cols);
  68. if (matrix_debouncing[i] != cols) {
  69. matrix_debouncing[i] = cols;
  70. if (debouncing) {
  71. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  72. }
  73. debouncing = DEBOUNCE;
  74. }
  75. unselect_rows();
  76. }
  77. if (debouncing) {
  78. if (--debouncing) {
  79. wait_ms(1);
  80. } else {
  81. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  82. matrix[i] = matrix_debouncing[i];
  83. }
  84. }
  85. }
  86. print("\n"); // FIXME: only here for debugging
  87. wait_ms(1000); // FIXME: only here for debugging
  88. return 1;
  89. }
  90. inline
  91. bool matrix_is_on(uint8_t row, uint8_t col) {
  92. return (matrix[row] & ((matrix_row_t)1<<col));
  93. }
  94. inline
  95. matrix_row_t matrix_get_row(uint8_t row) {
  96. return matrix[row];
  97. }
  98. void matrix_print(void) {
  99. print("\nr/c 0123456789ABCDEF\n");
  100. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  101. phex(row); print(": ");
  102. pbin_reverse16(matrix_get_row(row));
  103. print("\n");
  104. }
  105. }
  106. /* Column pin configuration
  107. */
  108. static void init_cols(void) {
  109. // Set columns to input mode so they can be read.
  110. palSetPadMode(TEENSY_PIN14_IOPORT, TEENSY_PIN14, PAL_MODE_INPUT);
  111. palSetPadMode(TEENSY_PIN15_IOPORT, TEENSY_PIN15, PAL_MODE_INPUT);
  112. palSetPadMode(TEENSY_PIN16_IOPORT, TEENSY_PIN16, PAL_MODE_INPUT);
  113. palSetPadMode(TEENSY_PIN11_IOPORT, TEENSY_PIN11, PAL_MODE_INPUT);
  114. }
  115. /* Returns status of switches(1:on, 0:off) */
  116. static matrix_row_t read_cols(void) {
  117. printf("pin14:%d, ", (palReadPad(TEENSY_PIN14_IOPORT, TEENSY_PIN14)==PAL_LOW)<<0);
  118. printf("pin15:%d, ", (palReadPad(TEENSY_PIN15_IOPORT, TEENSY_PIN15)==PAL_LOW)<<1);
  119. printf("pin16:%d, ", (palReadPad(TEENSY_PIN16_IOPORT, TEENSY_PIN16)==PAL_LOW)<<2);
  120. printf("pin11:%d\n", (palReadPad(TEENSY_PIN11_IOPORT, TEENSY_PIN11)==PAL_LOW)<<3);
  121. return (palReadPad(TEENSY_PIN14_IOPORT, TEENSY_PIN14)==PAL_LOW) ? 0 : (1<<0) ||
  122. (palReadPad(TEENSY_PIN15_IOPORT, TEENSY_PIN15)==PAL_LOW) ? 0 : (1<<1) ||
  123. (palReadPad(TEENSY_PIN16_IOPORT, TEENSY_PIN16)==PAL_LOW) ? 0 : (1<<2) ||
  124. (palReadPad(TEENSY_PIN11_IOPORT, TEENSY_PIN11)==PAL_LOW) ? 0 : (1<<3);
  125. }
  126. /* Row pin configuration
  127. */
  128. static void unselect_rows(void) {
  129. // internal pull-down
  130. palClearPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
  131. palClearPad(TEENSY_PIN12_IOPORT, TEENSY_PIN12);
  132. palClearPad(TEENSY_PIN18_IOPORT, TEENSY_PIN18);
  133. }
  134. static void select_row(uint8_t row) {
  135. (void)row;
  136. // Output high to select
  137. switch (row) {
  138. case 0:
  139. print("Row 1: ");
  140. palSetPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
  141. break;
  142. case 1:
  143. print("Row 2: ");
  144. palSetPad(TEENSY_PIN12_IOPORT, TEENSY_PIN12);
  145. break;
  146. case 2:
  147. print("Row 3: ");
  148. palSetPad(TEENSY_PIN18_IOPORT, TEENSY_PIN18);
  149. break;
  150. }
  151. }