Advertisement
Guest User

Untitled

a guest
May 7th, 2016
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.52 KB | None | 0 0
  1. /*
  2. Copyright 2012 Jun Wako <wakojun@gmail.com>
  3.  
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17.  
  18. #include "ch.h"
  19. #include "hal.h"
  20.  
  21. /*
  22. * scan matrix
  23. */
  24. #include "print.h"
  25. #include "debug.h"
  26. #include "util.h"
  27. #include "matrix.h"
  28. #include "wait.h"
  29.  
  30. #ifndef DEBOUNCE
  31. # define DEBOUNCE 5
  32. #endif
  33. static uint8_t debouncing = DEBOUNCE;
  34.  
  35. /* matrix state(1:on, 0:off) */
  36. static matrix_row_t matrix[MATRIX_ROWS];
  37. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  38.  
  39. static matrix_row_t read_cols(void);
  40. static void init_cols(void);
  41. static void unselect_rows(void);
  42. static void select_row(uint8_t row);
  43.  
  44.  
  45. inline
  46. uint8_t matrix_rows(void)
  47. {
  48. return MATRIX_ROWS;
  49. }
  50.  
  51. inline
  52. uint8_t matrix_cols(void)
  53. {
  54. return MATRIX_COLS;
  55. }
  56.  
  57. #define LED_ON() do { palSetPad(TEENSY_PIN17_IOPORT, TEENSY_PIN17) ;} while (0)
  58. #define LED_OFF() do { palClearPad(TEENSY_PIN17_IOPORT, TEENSY_PIN17); } while (0)
  59. #define LED_TGL() do { palTogglePad(TEENSY_PIN17_IOPORT, TEENSY_PIN17); } while (0)
  60.  
  61. void matrix_init(void)
  62. {
  63. // initialize row and col
  64. unselect_rows();
  65. init_cols();
  66.  
  67. // initialize matrix state: all keys off
  68. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  69. matrix[i] = 0;
  70. matrix_debouncing[i] = 0;
  71. }
  72.  
  73. //debug
  74. debug_matrix = true;
  75. LED_ON();
  76. wait_ms(500);
  77. LED_OFF();
  78. }
  79.  
  80. uint8_t matrix_scan(void)
  81. {
  82. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  83. select_row(i);
  84. wait_us(30); // without this wait read unstable value.
  85. matrix_row_t cols = read_cols();
  86. if (matrix_debouncing[i] != cols) {
  87. matrix_debouncing[i] = cols;
  88. if (debouncing) {
  89. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  90. }
  91. debouncing = DEBOUNCE;
  92. }
  93. unselect_rows();
  94. }
  95.  
  96. if (debouncing) {
  97. if (--debouncing) {
  98. wait_ms(1);
  99. } else {
  100. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  101. matrix[i] = matrix_debouncing[i];
  102. }
  103. }
  104. }
  105.  
  106. return 1;
  107. }
  108.  
  109. inline
  110. bool matrix_is_on(uint8_t row, uint8_t col)
  111. {
  112. return (matrix[row] & ((matrix_row_t)1<<col));
  113. }
  114.  
  115. inline
  116. matrix_row_t matrix_get_row(uint8_t row)
  117. {
  118. return matrix[row];
  119. }
  120.  
  121. void matrix_print(void)
  122. {
  123. print("\nr/c 0123456789ABCDEF\n");
  124. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  125. phex(row); print(": ");
  126. pbin_reverse16(matrix_get_row(row));
  127. print("\n");
  128. }
  129. }
  130.  
  131. uint8_t matrix_key_count(void)
  132. {
  133. uint8_t count = 0;
  134. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  135. count += bitpop16(matrix[i]);
  136. }
  137. return count;
  138. }
  139.  
  140.  
  141. /* Column pin configuration
  142. */
  143. static void init_cols(void)
  144. {
  145. // internal pull-up
  146. palSetPadMode(TEENSY_PIN25_IOPORT, TEENSY_PIN25, PAL_MODE_INPUT_PULLDOWN);
  147. palSetPadMode(TEENSY_PIN24_IOPORT, TEENSY_PIN24, PAL_MODE_INPUT_PULLDOWN);
  148. palSetPadMode(TEENSY_PIN23_IOPORT, TEENSY_PIN23, PAL_MODE_INPUT_PULLDOWN);
  149. palSetPadMode(TEENSY_PIN22_IOPORT, TEENSY_PIN22, PAL_MODE_INPUT_PULLDOWN);
  150. palSetPadMode(TEENSY_PIN21_IOPORT, TEENSY_PIN21, PAL_MODE_INPUT_PULLDOWN);
  151. palSetPadMode(TEENSY_PIN20_IOPORT, TEENSY_PIN20, PAL_MODE_INPUT_PULLDOWN);
  152. palSetPadMode(TEENSY_PIN19_IOPORT, TEENSY_PIN19, PAL_MODE_INPUT_PULLDOWN);
  153. palSetPadMode(TEENSY_PIN18_IOPORT, TEENSY_PIN18, PAL_MODE_INPUT_PULLDOWN);
  154. palSetPadMode(TEENSY_PIN15_IOPORT, TEENSY_PIN15, PAL_MODE_INPUT_PULLDOWN);
  155. palSetPadMode(TEENSY_PIN14_IOPORT, TEENSY_PIN14, PAL_MODE_INPUT_PULLDOWN);
  156. palSetPadMode(TEENSY_PIN1_IOPORT, TEENSY_PIN1, PAL_MODE_INPUT_PULLDOWN);
  157. palSetPadMode(TEENSY_PIN12_IOPORT, TEENSY_PIN12, PAL_MODE_INPUT_PULLDOWN);
  158. palSetPadMode(TEENSY_PIN11_IOPORT, TEENSY_PIN11, PAL_MODE_INPUT_PULLDOWN);
  159. palSetPadMode(TEENSY_PIN10_IOPORT, TEENSY_PIN10, PAL_MODE_INPUT_PULLDOWN);
  160. palSetPadMode(TEENSY_PIN9_IOPORT, TEENSY_PIN9, PAL_MODE_INPUT_PULLDOWN);
  161. palSetPadMode(TEENSY_PIN8_IOPORT, TEENSY_PIN8, PAL_MODE_INPUT_PULLDOWN);
  162. }
  163.  
  164. /* Returns status of switches(1:on, 0:off) */
  165. static matrix_row_t read_cols(void)
  166. {
  167. return (((palReadPad(TEENSY_PIN25_IOPORT, TEENSY_PIN25)==PAL_LOW) ? 0 : (1<<1)) |
  168. ((palReadPad(TEENSY_PIN24_IOPORT, TEENSY_PIN24)==PAL_LOW) ? 0 : (1<<0)) |
  169. ((palReadPad(TEENSY_PIN23_IOPORT, TEENSY_PIN23)==PAL_LOW) ? 0 : (1<<2)) |
  170. ((palReadPad(TEENSY_PIN22_IOPORT, TEENSY_PIN22)==PAL_LOW) ? 0 : (1<<3)) |
  171. ((palReadPad(TEENSY_PIN21_IOPORT, TEENSY_PIN21)==PAL_LOW) ? 0 : (1<<4)) |
  172. ((palReadPad(TEENSY_PIN20_IOPORT, TEENSY_PIN20)==PAL_LOW) ? 0 : (1<<5)) |
  173. ((palReadPad(TEENSY_PIN19_IOPORT, TEENSY_PIN19)==PAL_LOW) ? 0 : (1<<6)) |
  174. ((palReadPad(TEENSY_PIN18_IOPORT, TEENSY_PIN18)==PAL_LOW) ? 0 : (1<<7)) |
  175. ((palReadPad(TEENSY_PIN15_IOPORT, TEENSY_PIN15)==PAL_LOW) ? 0 : (1<<8)) |
  176. ((palReadPad(TEENSY_PIN14_IOPORT, TEENSY_PIN14)==PAL_LOW) ? 0 : (1<<9)) |
  177. ((palReadPad(TEENSY_PIN1_IOPORT, TEENSY_PIN1)==PAL_LOW) ? 0 : (1<<10)) |
  178. ((palReadPad(TEENSY_PIN12_IOPORT, TEENSY_PIN12)==PAL_LOW) ? 0 : (1<<11)) |
  179. ((palReadPad(TEENSY_PIN11_IOPORT, TEENSY_PIN11)==PAL_LOW) ? 0 : (1<<12)) |
  180. ((palReadPad(TEENSY_PIN10_IOPORT, TEENSY_PIN10)==PAL_LOW) ? 0 : (1<<13)) |
  181. ((palReadPad(TEENSY_PIN9_IOPORT, TEENSY_PIN9)==PAL_LOW) ? 0 : (1<<14)) |
  182. ((palReadPad(TEENSY_PIN8_IOPORT, TEENSY_PIN8)==PAL_LOW) ? 0 : (1<<15)));
  183. // | ((palReadPad(...)==PAL_HIGH) ? 0 : (1<<1))
  184. }
  185.  
  186. /* Row pin configuration
  187. */
  188. static void unselect_rows(void)
  189. {
  190. palSetPadMode(TEENSY_PIN2_IOPORT, TEENSY_PIN2, PAL_MODE_INPUT); // hi-Z
  191. palSetPadMode(TEENSY_PIN3_IOPORT, TEENSY_PIN3, PAL_MODE_INPUT);
  192. palSetPadMode(TEENSY_PIN4_IOPORT, TEENSY_PIN4, PAL_MODE_INPUT);
  193. palSetPadMode(TEENSY_PIN5_IOPORT, TEENSY_PIN5, PAL_MODE_INPUT);
  194. palSetPadMode(TEENSY_PIN6_IOPORT, TEENSY_PIN6, PAL_MODE_INPUT);
  195. palSetPadMode(TEENSY_PIN7_IOPORT, TEENSY_PIN7, PAL_MODE_INPUT);
  196. }
  197.  
  198. static void select_row(uint8_t row)
  199. {
  200. (void)row;
  201. // Output low to select
  202. switch (row) {
  203. case 5:
  204. palSetPadMode(TEENSY_PIN2_IOPORT, TEENSY_PIN2, PAL_MODE_OUTPUT_PUSHPULL);
  205. palSetPad(TEENSY_PIN2_IOPORT, TEENSY_PIN2);
  206. break;
  207. case 4:
  208. palSetPadMode(TEENSY_PIN3_IOPORT, TEENSY_PIN3, PAL_MODE_OUTPUT_PUSHPULL);
  209. palSetPad(TEENSY_PIN3_IOPORT, TEENSY_PIN3);
  210. break;
  211. case 3:
  212. palSetPadMode(TEENSY_PIN4_IOPORT, TEENSY_PIN4, PAL_MODE_OUTPUT_PUSHPULL);
  213. palSetPad(TEENSY_PIN4_IOPORT, TEENSY_PIN4);
  214. break;
  215. case 2:
  216. palSetPadMode(TEENSY_PIN5_IOPORT, TEENSY_PIN5, PAL_MODE_OUTPUT_PUSHPULL);
  217. palSetPad(TEENSY_PIN5_IOPORT, TEENSY_PIN5);
  218. break;
  219. case 1:
  220. palSetPadMode(TEENSY_PIN6_IOPORT, TEENSY_PIN6, PAL_MODE_OUTPUT_PUSHPULL);
  221. palSetPad(TEENSY_PIN6_IOPORT, TEENSY_PIN6);
  222. break;
  223. case 0:
  224. palSetPadMode(TEENSY_PIN7_IOPORT, TEENSY_PIN7, PAL_MODE_OUTPUT_PUSHPULL);
  225. palSetPad(TEENSY_PIN7_IOPORT, TEENSY_PIN7);
  226. break;
  227. }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement