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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /* generic STM32F103C8T6 board */
  46. #ifdef BOARD_GENERIC_STM32_F103
  47. #define LED_ON() do { palClearPad(GPIOC, GPIOC_LED) ;} while (0)
  48. #define LED_OFF() do { palSetPad(GPIOC, GPIOC_LED); } while (0)
  49. #define LED_TGL() do { palTogglePad(GPIOC, GPIOC_LED); } while (0)
  50. #endif
  51. /* Maple Mini */
  52. #ifdef BOARD_MAPLEMINI_STM32_F103
  53. #define LED_ON() do { palSetPad(GPIOB, 1) ;} while (0)
  54. #define LED_OFF() do { palClearPad(GPIOB, 1); } while (0)
  55. #define LED_TGL() do { palTogglePad(GPIOB, 1); } while (0)
  56. #endif
  57. void matrix_init(void)
  58. {
  59. // initialize row and col
  60. unselect_rows();
  61. init_cols();
  62. // initialize matrix state: all keys off
  63. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  64. matrix[i] = 0;
  65. matrix_debouncing[i] = 0;
  66. }
  67. //debug
  68. debug_matrix = true;
  69. LED_ON();
  70. wait_ms(500);
  71. LED_OFF();
  72. }
  73. uint8_t matrix_scan(void)
  74. {
  75. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  76. select_row(i);
  77. wait_us(30); // without this wait read unstable value.
  78. matrix_row_t cols = read_cols();
  79. if (matrix_debouncing[i] != cols) {
  80. matrix_debouncing[i] = cols;
  81. if (debouncing) {
  82. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  83. }
  84. debouncing = DEBOUNCE;
  85. }
  86. unselect_rows();
  87. }
  88. if (debouncing) {
  89. if (--debouncing) {
  90. wait_ms(1);
  91. } else {
  92. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  93. matrix[i] = matrix_debouncing[i];
  94. }
  95. }
  96. }
  97. return 1;
  98. }
  99. inline
  100. bool matrix_is_on(uint8_t row, uint8_t col)
  101. {
  102. return (matrix[row] & ((matrix_row_t)1<<col));
  103. }
  104. inline
  105. matrix_row_t matrix_get_row(uint8_t row)
  106. {
  107. return matrix[row];
  108. }
  109. void matrix_print(void)
  110. {
  111. print("\nr/c 0123456789ABCDEF\n");
  112. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  113. phex(row); print(": ");
  114. pbin_reverse16(matrix_get_row(row));
  115. print("\n");
  116. }
  117. }
  118. /* Column pin configuration
  119. */
  120. static void init_cols(void)
  121. {
  122. #ifdef BOARD_MAPLEMINI_STM32_F103
  123. // don't need pullup/down, since it's pulled down in hardware
  124. palSetPadMode(GPIOB, 8, PAL_MODE_INPUT);
  125. #else
  126. palSetPadMode(GPIOB, 8, PAL_MODE_INPUT_PULLDOWN);
  127. #endif
  128. }
  129. /* Returns status of switches(1:on, 0:off) */
  130. static matrix_row_t read_cols(void)
  131. {
  132. return ((palReadPad(GPIOB, 8)==PAL_LOW) ? 0 : (1<<0));
  133. // | ((palReadPad(...)==PAL_HIGH) ? 0 : (1<<1))
  134. }
  135. /* Row pin configuration
  136. */
  137. static void unselect_rows(void)
  138. {
  139. // palSetPadMode(GPIOA, GPIOA_PIN10, PAL_MODE_INPUT); // hi-Z
  140. }
  141. static void select_row(uint8_t row)
  142. {
  143. (void)row;
  144. // Output low to select
  145. // switch (row) {
  146. // case 0:
  147. // palSetPadMode(GPIOA, GPIOA_PIN10, PAL_MODE_OUTPUT_PUSHPULL);
  148. // palSetPad(GPIOA, GPIOA_PIN10, PAL_LOW);
  149. // break;
  150. // }
  151. }