diff options
Diffstat (limited to 'keyboards/preonic/rev3_drop/matrix.c')
-rw-r--r-- | keyboards/preonic/rev3_drop/matrix.c | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/keyboards/preonic/rev3_drop/matrix.c b/keyboards/preonic/rev3_drop/matrix.c new file mode 100644 index 000000000..392997e0a --- /dev/null +++ b/keyboards/preonic/rev3_drop/matrix.c | |||
@@ -0,0 +1,168 @@ | |||
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 | palSetPadMode(GPIOA, 3, PAL_MODE_INPUT_PULLDOWN); | ||
66 | palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_PULLDOWN); | ||
67 | |||
68 | memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t)); | ||
69 | memset(matrix_debouncing, 0, MATRIX_COLS * sizeof(matrix_row_t)); | ||
70 | |||
71 | matrix_init_quantum(); | ||
72 | } | ||
73 | |||
74 | uint8_t matrix_scan(void) { | ||
75 | // actual matrix | ||
76 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
77 | matrix_row_t data = 0; | ||
78 | |||
79 | // strobe col { B11, B10, B2, B1, A7, B0 } | ||
80 | switch (col) { | ||
81 | case 0: | ||
82 | palSetPad(GPIOB, 11); | ||
83 | break; | ||
84 | case 1: | ||
85 | palSetPad(GPIOB, 10); | ||
86 | break; | ||
87 | case 2: | ||
88 | palSetPad(GPIOB, 2); | ||
89 | break; | ||
90 | case 3: | ||
91 | palSetPad(GPIOB, 1); | ||
92 | break; | ||
93 | case 4: | ||
94 | palSetPad(GPIOA, 7); | ||
95 | break; | ||
96 | case 5: | ||
97 | palSetPad(GPIOB, 0); | ||
98 | break; | ||
99 | } | ||
100 | |||
101 | // need wait to settle pin state | ||
102 | wait_us(20); | ||
103 | |||
104 | // read row data { A10, A9, A8, B15, C13, C14, C15, A2 } | ||
105 | 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) | (palReadPad(GPIOA, 3) << 8) | (palReadPad(GPIOA, 6) << 9)); | ||
106 | |||
107 | // unstrobe col { B11, B10, B2, B1, A7, B0 } | ||
108 | switch (col) { | ||
109 | case 0: | ||
110 | palClearPad(GPIOB, 11); | ||
111 | break; | ||
112 | case 1: | ||
113 | palClearPad(GPIOB, 10); | ||
114 | break; | ||
115 | case 2: | ||
116 | palClearPad(GPIOB, 2); | ||
117 | break; | ||
118 | case 3: | ||
119 | palClearPad(GPIOB, 1); | ||
120 | break; | ||
121 | case 4: | ||
122 | palClearPad(GPIOA, 7); | ||
123 | break; | ||
124 | case 5: | ||
125 | palClearPad(GPIOB, 0); | ||
126 | break; | ||
127 | } | ||
128 | |||
129 | if (matrix_debouncing[col] != data) { | ||
130 | matrix_debouncing[col] = data; | ||
131 | debouncing = true; | ||
132 | debouncing_time = timer_read(); | ||
133 | } | ||
134 | } | ||
135 | |||
136 | if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) { | ||
137 | for (int row = 0; row < MATRIX_ROWS; row++) { | ||
138 | matrix[row] = 0; | ||
139 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
140 | matrix[row] |= ((matrix_debouncing[col] & (1 << row) ? 1 : 0) << col); | ||
141 | } | ||
142 | } | ||
143 | debouncing = false; | ||
144 | } | ||
145 | |||
146 | matrix_scan_quantum(); | ||
147 | |||
148 | return 1; | ||
149 | } | ||
150 | |||
151 | bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & (1 << col)); } | ||
152 | |||
153 | matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; } | ||
154 | |||
155 | void matrix_print(void) { | ||
156 | dprintf("\nr/c 01234567\n"); | ||
157 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
158 | dprintf("%X0: ", row); | ||
159 | matrix_row_t data = matrix_get_row(row); | ||
160 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
161 | if (data & (1 << col)) | ||
162 | dprintf("1"); | ||
163 | else | ||
164 | dprintf("0"); | ||
165 | } | ||
166 | dprintf("\n"); | ||
167 | } | ||
168 | } | ||