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_PIN2_IOPORT, TEENSY_PIN2, PAL_MODE_INPUT_PULLUP);
139 /* Returns status of switches(1:on, 0:off) */
140 static matrix_row_t read_cols(void)
142 return ((palReadPad(TEENSY_PIN2_IOPORT, TEENSY_PIN2)==PAL_HIGH) ? 0 : (1<<0));
143 // | ((palReadPad(...)==PAL_HIGH) ? 0 : (1<<1))
146 /* Row pin configuration
148 static void unselect_rows(void)
150 palSetPadMode(TEENSY_PIN5_IOPORT, TEENSY_PIN5, PAL_MODE_INPUT); // hi-Z
153 static void select_row(uint8_t row)
156 // Output low to select
159 palSetPadMode(TEENSY_PIN5_IOPORT, TEENSY_PIN5, PAL_MODE_OUTPUT_PUSHPULL);
160 palClearPad(TEENSY_PIN5_IOPORT, TEENSY_PIN5);