Advertisement
Guest User

Untitled

a guest
May 6th, 2016
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.47 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_PIN13_IOPORT, TEENSY_PIN13) ;} while (0)
  58. #define LED_OFF() do { palClearPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13); } while (0)
  59. #define LED_TGL() do { palTogglePad(TEENSY_PIN13_IOPORT, TEENSY_PIN13); } 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_PIN7_IOPORT, TEENSY_PIN7, PAL_MODE_INPUT_PULLUP);
  147. palSetPadMode(TEENSY_PIN6_IOPORT, TEENSY_PIN6, PAL_MODE_INPUT_PULLUP);
  148. palSetPadMode(TEENSY_PIN5_IOPORT, TEENSY_PIN5, PAL_MODE_INPUT_PULLUP);
  149. palSetPadMode(TEENSY_PIN4_IOPORT, TEENSY_PIN4, PAL_MODE_INPUT_PULLUP);
  150. palSetPadMode(TEENSY_PIN3_IOPORT, TEENSY_PIN3, PAL_MODE_INPUT_PULLUP);
  151. palSetPadMode(TEENSY_PIN2_IOPORT, TEENSY_PIN2, PAL_MODE_INPUT_PULLUP);
  152.  
  153. }
  154.  
  155. /* Returns status of switches(1:on, 0:off) */
  156. static matrix_row_t read_cols(void)
  157. {
  158. return (((palReadPad(TEENSY_PIN7_IOPORT, TEENSY_PIN7)==PAL_HIGH) ? 0 : (1<<5)) |
  159. ((palReadPad(TEENSY_PIN6_IOPORT, TEENSY_PIN6)==PAL_HIGH) ? 0 : (1<<4)) |
  160. ((palReadPad(TEENSY_PIN5_IOPORT, TEENSY_PIN5)==PAL_HIGH) ? 0 : (1<<3)) |
  161. ((palReadPad(TEENSY_PIN4_IOPORT, TEENSY_PIN4)==PAL_HIGH) ? 0 : (1<<2)) |
  162. ((palReadPad(TEENSY_PIN3_IOPORT, TEENSY_PIN3)==PAL_HIGH) ? 0 : (1<<1)) |
  163. ((palReadPad(TEENSY_PIN2_IOPORT, TEENSY_PIN2)==PAL_HIGH) ? 0 : (1<<0)));
  164. // | ((palReadPad(...)==PAL_HIGH) ? 0 : (1<<1))
  165. }
  166.  
  167. /* Row pin configuration
  168. */
  169. static void unselect_rows(void)
  170. {
  171. palSetPadMode(TEENSY_PIN25_IOPORT, TEENSY_PIN25, PAL_MODE_INPUT); // hi-Z
  172. palSetPadMode(TEENSY_PIN24_IOPORT, TEENSY_PIN24, PAL_MODE_INPUT);
  173. palSetPadMode(TEENSY_PIN23_IOPORT, TEENSY_PIN23, PAL_MODE_INPUT);
  174. palSetPadMode(TEENSY_PIN22_IOPORT, TEENSY_PIN22, PAL_MODE_INPUT);
  175. palSetPadMode(TEENSY_PIN21_IOPORT, TEENSY_PIN21, PAL_MODE_INPUT);
  176. palSetPadMode(TEENSY_PIN20_IOPORT, TEENSY_PIN20, PAL_MODE_INPUT);
  177. palSetPadMode(TEENSY_PIN19_IOPORT, TEENSY_PIN19, PAL_MODE_INPUT); // hi-Z
  178. palSetPadMode(TEENSY_PIN18_IOPORT, TEENSY_PIN18, PAL_MODE_INPUT);
  179. palSetPadMode(TEENSY_PIN15_IOPORT, TEENSY_PIN15, PAL_MODE_INPUT);
  180. palSetPadMode(TEENSY_PIN14_IOPORT, TEENSY_PIN14, PAL_MODE_INPUT);
  181. palSetPadMode(TEENSY_PIN13_IOPORT, TEENSY_PIN13, PAL_MODE_INPUT);
  182. palSetPadMode(TEENSY_PIN12_IOPORT, TEENSY_PIN12, PAL_MODE_INPUT);
  183. palSetPadMode(TEENSY_PIN11_IOPORT, TEENSY_PIN11, PAL_MODE_INPUT); // hi-Z
  184. palSetPadMode(TEENSY_PIN10_IOPORT, TEENSY_PIN10, PAL_MODE_INPUT);
  185. palSetPadMode(TEENSY_PIN9_IOPORT, TEENSY_PIN9, PAL_MODE_INPUT);
  186. palSetPadMode(TEENSY_PIN8_IOPORT, TEENSY_PIN8, PAL_MODE_INPUT); // hi-Z
  187. }
  188.  
  189. static void select_row(uint8_t row)
  190. {
  191. (void)row;
  192. // Output low to select
  193. switch (row) {
  194. case 0:
  195. palSetPadMode(TEENSY_PIN8_IOPORT, TEENSY_PIN8, PAL_MODE_OUTPUT_PUSHPULL);
  196. palClearPad(TEENSY_PIN8_IOPORT, TEENSY_PIN8);
  197. break;
  198. case 1:
  199. palSetPadMode(TEENSY_PIN9_IOPORT, TEENSY_PIN9, PAL_MODE_OUTPUT_PUSHPULL);
  200. palClearPad(TEENSY_PIN9_IOPORT, TEENSY_PIN9);
  201. break;
  202. case 2:
  203. palSetPadMode(TEENSY_PIN10_IOPORT, TEENSY_PIN10, PAL_MODE_OUTPUT_PUSHPULL);
  204. palClearPad(TEENSY_PIN10_IOPORT, TEENSY_PIN10);
  205. break;
  206. case 3:
  207. palSetPadMode(TEENSY_PIN11_IOPORT, TEENSY_PIN11, PAL_MODE_OUTPUT_PUSHPULL);
  208. palClearPad(TEENSY_PIN11_IOPORT, TEENSY_PIN11);
  209. break;
  210. case 4:
  211. palSetPadMode(TEENSY_PIN12_IOPORT, TEENSY_PIN12, PAL_MODE_OUTPUT_PUSHPULL);
  212. palClearPad(TEENSY_PIN12_IOPORT, TEENSY_PIN12);
  213. break;
  214. case 5:
  215. palSetPadMode(TEENSY_PIN13_IOPORT, TEENSY_PIN13, PAL_MODE_OUTPUT_PUSHPULL);
  216. palClearPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13);
  217. break;
  218. case 6:
  219. palSetPadMode(TEENSY_PIN14_IOPORT, TEENSY_PIN14, PAL_MODE_OUTPUT_PUSHPULL);
  220. palClearPad(TEENSY_PIN14_IOPORT, TEENSY_PIN14);
  221. break;
  222. case 7:
  223. palSetPadMode(TEENSY_PIN15_IOPORT, TEENSY_PIN15, PAL_MODE_OUTPUT_PUSHPULL);
  224. palClearPad(TEENSY_PIN15_IOPORT, TEENSY_PIN15);
  225. break;
  226. case 8:
  227. palSetPadMode(TEENSY_PIN18_IOPORT, TEENSY_PIN18, PAL_MODE_OUTPUT_PUSHPULL);
  228. palClearPad(TEENSY_PIN18_IOPORT, TEENSY_PIN18);
  229. break;
  230. case 9:
  231. palSetPadMode(TEENSY_PIN19_IOPORT, TEENSY_PIN19, PAL_MODE_OUTPUT_PUSHPULL);
  232. palClearPad(TEENSY_PIN19_IOPORT, TEENSY_PIN19);
  233. break;
  234. case 10:
  235. palSetPadMode(TEENSY_PIN20_IOPORT, TEENSY_PIN20, PAL_MODE_OUTPUT_PUSHPULL);
  236. palClearPad(TEENSY_PIN20_IOPORT, TEENSY_PIN20);
  237. break;
  238. case 11:
  239. palSetPadMode(TEENSY_PIN21_IOPORT, TEENSY_PIN21, PAL_MODE_OUTPUT_PUSHPULL);
  240. palClearPad(TEENSY_PIN21_IOPORT, TEENSY_PIN21);
  241. break;
  242. case 12:
  243. palSetPadMode(TEENSY_PIN22_IOPORT, TEENSY_PIN22, PAL_MODE_OUTPUT_PUSHPULL);
  244. palClearPad(TEENSY_PIN22_IOPORT, TEENSY_PIN22);
  245. break;
  246. case 13:
  247. palSetPadMode(TEENSY_PIN23_IOPORT, TEENSY_PIN23, PAL_MODE_OUTPUT_PUSHPULL);
  248. palClearPad(TEENSY_PIN23_IOPORT, TEENSY_PIN23);
  249. break;
  250. case 14:
  251. palSetPadMode(TEENSY_PIN25_IOPORT, TEENSY_PIN25, PAL_MODE_OUTPUT_PUSHPULL);
  252. palClearPad(TEENSY_PIN25_IOPORT, TEENSY_PIN25);
  253. break;
  254. case 15:
  255. palSetPadMode(TEENSY_PIN24_IOPORT, TEENSY_PIN24, PAL_MODE_OUTPUT_PUSHPULL);
  256. palClearPad(TEENSY_PIN24_IOPORT, TEENSY_PIN24);
  257. break;
  258. }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement