diff options
Diffstat (limited to 'keyboards/planck/rev6_drop/matrix.c')
-rw-r--r-- | keyboards/planck/rev6_drop/matrix.c | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/keyboards/planck/rev6_drop/matrix.c b/keyboards/planck/rev6_drop/matrix.c new file mode 100644 index 000000000..49e115d02 --- /dev/null +++ b/keyboards/planck/rev6_drop/matrix.c | |||
@@ -0,0 +1,166 @@ | |||
1 | /* | ||
2 | * Copyright 2018 Jack Humbert <jack.humb@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 <stdint.h> | ||
19 | #include <stdbool.h> | ||
20 | #include <string.h> | ||
21 | #include "hal.h" | ||
22 | #include "timer.h" | ||
23 | #include "wait.h" | ||
24 | #include "debug.h" | ||
25 | #include "matrix.h" | ||
26 | |||
27 | /* | ||
28 | * col: { B11, B10, B2, B1, A7, B0 } | ||
29 | * row: { A10, A9, A8, B15, C13, C14, C15, A2 } | ||
30 | */ | ||
31 | /* matrix state(1:on, 0:off) */ | ||
32 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
33 | static matrix_row_t matrix_debouncing[MATRIX_COLS]; | ||
34 | static bool debouncing = false; | ||
35 | static uint16_t debouncing_time = 0; | ||
36 | |||
37 | __attribute__((weak)) void matrix_init_user(void) {} | ||
38 | |||
39 | __attribute__((weak)) void matrix_scan_user(void) {} | ||
40 | |||
41 | __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); } | ||
42 | |||
43 | __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); } | ||
44 | |||
45 | void matrix_init(void) { | ||
46 | dprintf("matrix init\n"); | ||
47 | // debug_matrix = true; | ||
48 | |||
49 | // actual matrix setup | ||
50 | palSetPadMode(GPIOB, 11, PAL_MODE_OUTPUT_PUSHPULL); | ||
51 | palSetPadMode(GPIOB, 10, PAL_MODE_OUTPUT_PUSHPULL); | ||
52 | palSetPadMode(GPIOB, 2, PAL_MODE_OUTPUT_PUSHPULL); | ||
53 | palSetPadMode(GPIOB, 1, PAL_MODE_OUTPUT_PUSHPULL); | ||
54 | palSetPadMode(GPIOA, 7, PAL_MODE_OUTPUT_PUSHPULL); | ||
55 | palSetPadMode(GPIOB, 0, PAL_MODE_OUTPUT_PUSHPULL); | ||
56 | |||
57 | palSetPadMode(GPIOA, 10, PAL_MODE_INPUT_PULLDOWN); | ||
58 | palSetPadMode(GPIOA, 9, PAL_MODE_INPUT_PULLDOWN); | ||
59 | palSetPadMode(GPIOA, 8, PAL_MODE_INPUT_PULLDOWN); | ||
60 | palSetPadMode(GPIOB, 15, PAL_MODE_INPUT_PULLDOWN); | ||
61 | palSetPadMode(GPIOC, 13, PAL_MODE_INPUT_PULLDOWN); | ||
62 | palSetPadMode(GPIOC, 14, PAL_MODE_INPUT_PULLDOWN); | ||
63 | palSetPadMode(GPIOC, 15, PAL_MODE_INPUT_PULLDOWN); | ||
64 | palSetPadMode(GPIOA, 2, PAL_MODE_INPUT_PULLDOWN); | ||
65 | |||
66 | memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t)); | ||
67 | memset(matrix_debouncing, 0, MATRIX_COLS * sizeof(matrix_row_t)); | ||
68 | |||
69 | matrix_init_quantum(); | ||
70 | } | ||
71 | |||
72 | uint8_t matrix_scan(void) { | ||
73 | // actual matrix | ||
74 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
75 | matrix_row_t data = 0; | ||
76 | |||
77 | // strobe col { B11, B10, B2, B1, A7, B0 } | ||
78 | switch (col) { | ||
79 | case 0: | ||
80 | palSetPad(GPIOB, 11); | ||
81 | break; | ||
82 | case 1: | ||
83 | palSetPad(GPIOB, 10); | ||
84 | break; | ||
85 | case 2: | ||
86 | palSetPad(GPIOB, 2); | ||
87 | break; | ||
88 | case 3: | ||
89 | palSetPad(GPIOB, 1); | ||
90 | break; | ||
91 | case 4: | ||
92 | palSetPad(GPIOA, 7); | ||
93 | break; | ||
94 | case 5: | ||
95 | palSetPad(GPIOB, 0); | ||
96 | break; | ||
97 | } | ||
98 | |||
99 | // need wait to settle pin state | ||
100 | wait_us(20); | ||
101 | |||
102 | // read row data { A10, A9, A8, B15, C13, C14, C15, A2 } | ||
103 | data = ((palReadPad(GPIOA, 10) << 0) | (palReadPad(GPIOA, 9) << 1) | (palReadPad(GPIOA, 8) << 2) | (palReadPad(GPIOB, 15) << 3) | (palReadPad(GPIOC, 13) << 4) | (palReadPad(GPIOC, 14) << 5) | (palReadPad(GPIOC, 15) << 6) | (palReadPad(GPIOA, 2) << 7)); | ||
104 | |||
105 | // unstrobe col { B11, B10, B2, B1, A7, B0 } | ||
106 | switch (col) { | ||
107 | case 0: | ||
108 | palClearPad(GPIOB, 11); | ||
109 | break; | ||
110 | case 1: | ||
111 | palClearPad(GPIOB, 10); | ||
112 | break; | ||
113 | case 2: | ||
114 | palClearPad(GPIOB, 2); | ||
115 | break; | ||
116 | case 3: | ||
117 | palClearPad(GPIOB, 1); | ||
118 | break; | ||
119 | case 4: | ||
120 | palClearPad(GPIOA, 7); | ||
121 | break; | ||
122 | case 5: | ||
123 | palClearPad(GPIOB, 0); | ||
124 | break; | ||
125 | } | ||
126 | |||
127 | if (matrix_debouncing[col] != data) { | ||
128 | matrix_debouncing[col] = data; | ||
129 | debouncing = true; | ||
130 | debouncing_time = timer_read(); | ||
131 | } | ||
132 | } | ||
133 | |||
134 | if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) { | ||
135 | for (int row = 0; row < MATRIX_ROWS; row++) { | ||
136 | matrix[row] = 0; | ||
137 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
138 | matrix[row] |= ((matrix_debouncing[col] & (1 << row) ? 1 : 0) << col); | ||
139 | } | ||
140 | } | ||
141 | debouncing = false; | ||
142 | } | ||
143 | |||
144 | matrix_scan_quantum(); | ||
145 | |||
146 | return 1; | ||
147 | } | ||
148 | |||
149 | bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & (1 << col)); } | ||
150 | |||
151 | matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; } | ||
152 | |||
153 | void matrix_print(void) { | ||
154 | dprintf("\nr/c 01234567\n"); | ||
155 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
156 | dprintf("%X0: ", row); | ||
157 | matrix_row_t data = matrix_get_row(row); | ||
158 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
159 | if (data & (1 << col)) | ||
160 | dprintf("1"); | ||
161 | else | ||
162 | dprintf("0"); | ||
163 | } | ||
164 | dprintf("\n"); | ||
165 | } | ||
166 | } | ||