2 Copyright 2012 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
33 static uint8_t debouncing = DEBOUNCE;
35 /* matrix state(1:on, 0:off) */
36 static matrix_row_t matrix[MATRIX_ROWS];
37 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
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);
46 uint8_t matrix_rows(void)
52 uint8_t matrix_cols(void)
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)
61 void matrix_init(void)
63 // initialize row and col
67 // initialize matrix state: all keys off
68 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
70 matrix_debouncing[i] = 0;
80 uint8_t matrix_scan(void)
82 for (uint8_t i = 0; i < MATRIX_ROWS; 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;
89 debug("bounce!: "); debug_hex(debouncing); debug("\n");
91 debouncing = DEBOUNCE;
100 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
101 matrix[i] = matrix_debouncing[i];
110 bool matrix_is_on(uint8_t row, uint8_t col)
112 return (matrix[row] & ((matrix_row_t)1<<col));
116 matrix_row_t matrix_get_row(uint8_t row)
121 void matrix_print(void)
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));
131 /* Column pin configuration
133 static void init_cols(void)
136 palSetPadMode(TEENSY_PIN6_IOPORT , TEENSY_PIN6, PAL_MODE_INPUT_PULLUP);
137 palSetPadMode(TEENSY_PIN7_IOPORT , TEENSY_PIN7, PAL_MODE_INPUT_PULLUP);
138 palSetPadMode(TEENSY_PIN8_IOPORT , TEENSY_PIN8, PAL_MODE_INPUT_PULLUP);
139 palSetPadMode(TEENSY_PIN9_IOPORT , TEENSY_PIN9, PAL_MODE_INPUT_PULLUP);
140 palSetPadMode(TEENSY_PIN10_IOPORT, TEENSY_PIN10, PAL_MODE_INPUT_PULLUP);
141 palSetPadMode(TEENSY_PIN11_IOPORT, TEENSY_PIN11, PAL_MODE_INPUT_PULLUP);
142 palSetPadMode(TEENSY_PIN12_IOPORT, TEENSY_PIN12, PAL_MODE_INPUT_PULLUP);
143 palSetPadMode(TEENSY_PIN14_IOPORT, TEENSY_PIN14, PAL_MODE_INPUT_PULLUP);
144 palSetPadMode(TEENSY_PIN15_IOPORT, TEENSY_PIN15, PAL_MODE_INPUT_PULLUP);
145 palSetPadMode(TEENSY_PIN16_IOPORT, TEENSY_PIN16, PAL_MODE_INPUT_PULLUP);
146 palSetPadMode(TEENSY_PIN17_IOPORT, TEENSY_PIN17, PAL_MODE_INPUT_PULLUP);
147 palSetPadMode(TEENSY_PIN18_IOPORT, TEENSY_PIN18, PAL_MODE_INPUT_PULLUP);
148 palSetPadMode(TEENSY_PIN19_IOPORT, TEENSY_PIN19, PAL_MODE_INPUT_PULLUP);
149 palSetPadMode(TEENSY_PIN20_IOPORT, TEENSY_PIN20, PAL_MODE_INPUT_PULLUP);
150 palSetPadMode(TEENSY_PIN21_IOPORT, TEENSY_PIN21, PAL_MODE_INPUT_PULLUP);
151 palSetPadMode(TEENSY_PIN22_IOPORT, TEENSY_PIN22, PAL_MODE_INPUT_PULLUP);
152 palSetPadMode(TEENSY_PIN23_IOPORT, TEENSY_PIN23, PAL_MODE_INPUT_PULLUP);
155 /* Returns status of switches(1:on, 0:off) */
156 static matrix_row_t read_cols(void)
159 ((palReadPad(TEENSY_PIN6_IOPORT , TEENSY_PIN6 )==PAL_HIGH) ? 0 : (1<<0))
160 | ((palReadPad(TEENSY_PIN7_IOPORT , TEENSY_PIN7 )==PAL_HIGH) ? 0 : (1<<1))
161 | ((palReadPad(TEENSY_PIN8_IOPORT , TEENSY_PIN8 )==PAL_HIGH) ? 0 : (1<<2))
162 | ((palReadPad(TEENSY_PIN9_IOPORT , TEENSY_PIN9 )==PAL_HIGH) ? 0 : (1<<3))
163 | ((palReadPad(TEENSY_PIN10_IOPORT, TEENSY_PIN10)==PAL_HIGH) ? 0 : (1<<4))
164 | ((palReadPad(TEENSY_PIN11_IOPORT, TEENSY_PIN11)==PAL_HIGH) ? 0 : (1<<5))
165 | ((palReadPad(TEENSY_PIN12_IOPORT, TEENSY_PIN12)==PAL_HIGH) ? 0 : (1<<6))
166 | ((palReadPad(TEENSY_PIN14_IOPORT, TEENSY_PIN14)==PAL_HIGH) ? 0 : (1<<7))
167 | ((palReadPad(TEENSY_PIN15_IOPORT, TEENSY_PIN15)==PAL_HIGH) ? 0 : (1<<8))
168 | ((palReadPad(TEENSY_PIN16_IOPORT, TEENSY_PIN16)==PAL_HIGH) ? 0 : (1<<9))
169 | ((palReadPad(TEENSY_PIN17_IOPORT, TEENSY_PIN17)==PAL_HIGH) ? 0 : (1<<10))
170 | ((palReadPad(TEENSY_PIN18_IOPORT, TEENSY_PIN18)==PAL_HIGH) ? 0 : (1<<11))
171 | ((palReadPad(TEENSY_PIN19_IOPORT, TEENSY_PIN19)==PAL_HIGH) ? 0 : (1<<12))
172 | ((palReadPad(TEENSY_PIN20_IOPORT, TEENSY_PIN20)==PAL_HIGH) ? 0 : (1<<13))
173 | ((palReadPad(TEENSY_PIN21_IOPORT, TEENSY_PIN21)==PAL_HIGH) ? 0 : (1<<14))
174 | ((palReadPad(TEENSY_PIN22_IOPORT, TEENSY_PIN22)==PAL_HIGH) ? 0 : (1<<15))
175 | ((palReadPad(TEENSY_PIN23_IOPORT, TEENSY_PIN23)==PAL_HIGH) ? 0 : (1<<16))
179 /* Row pin configuration
181 static void unselect_rows(void)
183 palSetPadMode(TEENSY_PIN5_IOPORT, TEENSY_PIN5, PAL_MODE_INPUT);
184 palSetPadMode(TEENSY_PIN4_IOPORT, TEENSY_PIN4, PAL_MODE_INPUT);
185 palSetPadMode(TEENSY_PIN3_IOPORT, TEENSY_PIN3, PAL_MODE_INPUT);
186 palSetPadMode(TEENSY_PIN2_IOPORT, TEENSY_PIN2, PAL_MODE_INPUT);
187 palSetPadMode(TEENSY_PIN1_IOPORT, TEENSY_PIN1, PAL_MODE_INPUT);
188 palSetPadMode(TEENSY_PIN0_IOPORT, TEENSY_PIN0, PAL_MODE_INPUT);
191 static void select_row(uint8_t row)
194 // Output low to select
197 palSetPadMode(TEENSY_PIN5_IOPORT, TEENSY_PIN5, PAL_MODE_OUTPUT_PUSHPULL);
198 palClearPad(TEENSY_PIN5_IOPORT, TEENSY_PIN5);
201 palSetPadMode(TEENSY_PIN4_IOPORT, TEENSY_PIN4, PAL_MODE_OUTPUT_PUSHPULL);
202 palClearPad(TEENSY_PIN4_IOPORT, TEENSY_PIN4);
205 palSetPadMode(TEENSY_PIN3_IOPORT, TEENSY_PIN3, PAL_MODE_OUTPUT_PUSHPULL);
206 palClearPad(TEENSY_PIN3_IOPORT, TEENSY_PIN3);
209 palSetPadMode(TEENSY_PIN2_IOPORT, TEENSY_PIN2, PAL_MODE_OUTPUT_PUSHPULL);
210 palClearPad(TEENSY_PIN2_IOPORT, TEENSY_PIN2);
213 palSetPadMode(TEENSY_PIN1_IOPORT, TEENSY_PIN1, PAL_MODE_OUTPUT_PUSHPULL);
214 palClearPad(TEENSY_PIN1_IOPORT, TEENSY_PIN1);
217 palSetPadMode(TEENSY_PIN0_IOPORT, TEENSY_PIN0, PAL_MODE_OUTPUT_PUSHPULL);
218 palClearPad(TEENSY_PIN0_IOPORT, TEENSY_PIN0);