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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. for (uint8_t i=0; i < 1; i++) { // Loop for very short delay, wait_us is returning a 1ms delay
  70. LED_TGL(); // toggle the onboard LED attached to pin 13
  71. }
  72. matrix_row_t cols = read_cols();
  73. if (matrix_debouncing[i] != cols) {
  74. matrix_debouncing[i] = cols;
  75. if (debouncing) {
  76. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  77. }
  78. debouncing = DEBOUNCE;
  79. }
  80. unselect_rows();
  81. }
  82. if (debouncing) {
  83. if (--debouncing) {
  84. wait_ms(1);
  85. } else {
  86. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  87. matrix[i] = matrix_debouncing[i];
  88. }
  89. }
  90. }
  91. return 1;
  92. }
  93. inline
  94. bool matrix_is_on(uint8_t row, uint8_t col)
  95. {
  96. return (matrix[row] & ((matrix_row_t)1<<col));
  97. }
  98. inline
  99. matrix_row_t matrix_get_row(uint8_t row)
  100. {
  101. return matrix[row];
  102. }
  103. void matrix_print(void)
  104. {
  105. print("\nr/c 0123456789ABCDEF\n");
  106. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  107. phex(row); print(": ");
  108. pbin_reverse16(matrix_get_row(row));
  109. print("\n");
  110. }
  111. }
  112. /* Column pin configuration
  113. */
  114. static void init_cols(void)
  115. {
  116. // internal pull-up
  117. palSetPadMode(TEENSY_PIN10_IOPORT, TEENSY_PIN10, PAL_MODE_INPUT_PULLUP);
  118. palSetPadMode(TEENSY_PIN11_IOPORT, TEENSY_PIN11, PAL_MODE_INPUT_PULLUP);
  119. palSetPadMode(TEENSY_PIN12_IOPORT, TEENSY_PIN12, PAL_MODE_INPUT_PULLUP);
  120. palSetPadMode(TEENSY_PIN14_IOPORT, TEENSY_PIN14, PAL_MODE_INPUT_PULLUP);
  121. palSetPadMode(TEENSY_PIN15_IOPORT, TEENSY_PIN15, PAL_MODE_INPUT_PULLUP);
  122. palSetPadMode(TEENSY_PIN16_IOPORT, TEENSY_PIN16, PAL_MODE_INPUT_PULLUP);
  123. palSetPadMode(TEENSY_PIN18_IOPORT, TEENSY_PIN18, PAL_MODE_INPUT_PULLUP);
  124. palSetPadMode(TEENSY_PIN19_IOPORT, TEENSY_PIN19, PAL_MODE_INPUT_PULLUP);
  125. palSetPadMode(TEENSY_PIN20_IOPORT, TEENSY_PIN20, PAL_MODE_INPUT_PULLUP);
  126. palSetPadMode(TEENSY_PIN21_IOPORT, TEENSY_PIN21, PAL_MODE_INPUT_PULLUP);
  127. palSetPadMode(TEENSY_PIN22_IOPORT, TEENSY_PIN22, PAL_MODE_INPUT_PULLUP);
  128. palSetPadMode(TEENSY_PIN23_IOPORT, TEENSY_PIN23, PAL_MODE_INPUT_PULLUP);
  129. }
  130. /* Returns status of switches(1:on, 0:off) */
  131. static matrix_row_t read_cols(void)
  132. {
  133. return ((palReadPad(TEENSY_PIN10_IOPORT, TEENSY_PIN10)==PAL_HIGH) ? 0 : (1<<0))
  134. | ((palReadPad(TEENSY_PIN11_IOPORT, TEENSY_PIN11)==PAL_HIGH) ? 0 : (1<<1))
  135. | ((palReadPad(TEENSY_PIN12_IOPORT, TEENSY_PIN12)==PAL_HIGH) ? 0 : (1<<2))
  136. | ((palReadPad(TEENSY_PIN14_IOPORT, TEENSY_PIN14)==PAL_HIGH) ? 0 : (1<<3))
  137. | ((palReadPad(TEENSY_PIN15_IOPORT, TEENSY_PIN15)==PAL_HIGH) ? 0 : (1<<4))
  138. | ((palReadPad(TEENSY_PIN16_IOPORT, TEENSY_PIN16)==PAL_HIGH) ? 0 : (1<<5))
  139. | ((palReadPad(TEENSY_PIN18_IOPORT, TEENSY_PIN18)==PAL_HIGH) ? 0 : (1<<6))
  140. | ((palReadPad(TEENSY_PIN19_IOPORT, TEENSY_PIN19)==PAL_HIGH) ? 0 : (1<<7))
  141. | ((palReadPad(TEENSY_PIN20_IOPORT, TEENSY_PIN20)==PAL_HIGH) ? 0 : (1<<8))
  142. | ((palReadPad(TEENSY_PIN21_IOPORT, TEENSY_PIN21)==PAL_HIGH) ? 0 : (1<<9))
  143. | ((palReadPad(TEENSY_PIN22_IOPORT, TEENSY_PIN22)==PAL_HIGH) ? 0 : (1<<10))
  144. | ((palReadPad(TEENSY_PIN23_IOPORT, TEENSY_PIN23)==PAL_HIGH) ? 0 : (1<<11));
  145. }
  146. /* Row pin configuration
  147. */
  148. static void unselect_rows(void)
  149. {
  150. palSetPadMode(TEENSY_PIN6_IOPORT, TEENSY_PIN6, PAL_MODE_INPUT); // hi-Z
  151. palSetPadMode(TEENSY_PIN7_IOPORT, TEENSY_PIN7, PAL_MODE_INPUT); // hi-Z
  152. palSetPadMode(TEENSY_PIN8_IOPORT, TEENSY_PIN8, PAL_MODE_INPUT); // hi-Z
  153. palSetPadMode(TEENSY_PIN9_IOPORT, TEENSY_PIN9, PAL_MODE_INPUT); // hi-Z
  154. }
  155. static void select_row(uint8_t row)
  156. {
  157. (void)row;
  158. // Output low to select
  159. switch (row) {
  160. case 0:
  161. palSetPadMode(TEENSY_PIN6_IOPORT, TEENSY_PIN6, PAL_MODE_OUTPUT_PUSHPULL);
  162. palClearPad(TEENSY_PIN6_IOPORT, TEENSY_PIN6);
  163. break;
  164. case 1:
  165. palSetPadMode(TEENSY_PIN7_IOPORT, TEENSY_PIN7, PAL_MODE_OUTPUT_PUSHPULL);
  166. palClearPad(TEENSY_PIN7_IOPORT, TEENSY_PIN7);
  167. break;
  168. case 2:
  169. palSetPadMode(TEENSY_PIN8_IOPORT, TEENSY_PIN8, PAL_MODE_OUTPUT_PUSHPULL);
  170. palClearPad(TEENSY_PIN8_IOPORT, TEENSY_PIN8);
  171. break;
  172. case 3:
  173. palSetPadMode(TEENSY_PIN9_IOPORT, TEENSY_PIN9, PAL_MODE_OUTPUT_PUSHPULL);
  174. palClearPad(TEENSY_PIN9_IOPORT, TEENSY_PIN9);
  175. break;
  176. }
  177. }