]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - keyboard/teensy3.2_tkl/matrix.c
core: Clean up code of Locking key support
[max/tmk_keyboard.git] / keyboard / teensy3.2_tkl / matrix.c
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 /* Column pin configuration
132  */
133 static void  init_cols(void)
134 {
135     // internal pull-up
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);
153 }
154
155 /* Returns status of switches(1:on, 0:off) */
156 static matrix_row_t read_cols(void)
157 {
158     return (
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))
176     );
177 }
178
179 /* Row pin configuration
180  */
181 static void unselect_rows(void)
182 {
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);
189 }
190
191 static void select_row(uint8_t row)
192 {
193     (void)row;
194     // Output low to select
195     switch (row) {
196         case 0:
197             palSetPadMode(TEENSY_PIN5_IOPORT, TEENSY_PIN5, PAL_MODE_OUTPUT_PUSHPULL);
198             palClearPad(TEENSY_PIN5_IOPORT, TEENSY_PIN5);
199             break;
200         case 1:
201             palSetPadMode(TEENSY_PIN4_IOPORT, TEENSY_PIN4, PAL_MODE_OUTPUT_PUSHPULL);
202             palClearPad(TEENSY_PIN4_IOPORT, TEENSY_PIN4);
203             break;
204         case 2:
205             palSetPadMode(TEENSY_PIN3_IOPORT, TEENSY_PIN3, PAL_MODE_OUTPUT_PUSHPULL);
206             palClearPad(TEENSY_PIN3_IOPORT, TEENSY_PIN3);
207             break;
208         case 3:
209             palSetPadMode(TEENSY_PIN2_IOPORT, TEENSY_PIN2, PAL_MODE_OUTPUT_PUSHPULL);
210             palClearPad(TEENSY_PIN2_IOPORT, TEENSY_PIN2);
211             break;
212         case 4:
213             palSetPadMode(TEENSY_PIN1_IOPORT, TEENSY_PIN1, PAL_MODE_OUTPUT_PUSHPULL);
214             palClearPad(TEENSY_PIN1_IOPORT, TEENSY_PIN1);
215             break;
216         case 5:
217             palSetPadMode(TEENSY_PIN0_IOPORT, TEENSY_PIN0, PAL_MODE_OUTPUT_PUSHPULL);
218             palClearPad(TEENSY_PIN0_IOPORT, TEENSY_PIN0);
219             break;
220     }
221 }