diff options
author | QMK Bot <hello@qmk.fm> | 2021-10-02 00:13:49 +0000 |
---|---|---|
committer | QMK Bot <hello@qmk.fm> | 2021-10-02 00:13:49 +0000 |
commit | 481104053e9d6191cb93e0aeaf6ec938f9acdcb2 (patch) | |
tree | b983b895908b8665987287895390339d91f1c9e6 | |
parent | ef3176713f75b3d91c4b67bbf18b1a7e8df09dd1 (diff) | |
parent | 54b8d6a89182390533ccd1cb715fbaf59720bf3e (diff) | |
download | qmk_firmware-481104053e9d6191cb93e0aeaf6ec938f9acdcb2.tar.gz qmk_firmware-481104053e9d6191cb93e0aeaf6ec938f9acdcb2.zip |
Merge remote-tracking branch 'origin/master' into develop
-rw-r--r-- | keyboards/moonlander/matrix.c | 140 | ||||
-rw-r--r-- | keyboards/moonlander/rules.mk | 3 |
2 files changed, 28 insertions, 115 deletions
diff --git a/keyboards/moonlander/matrix.c b/keyboards/moonlander/matrix.c index 8f2610fc6..782f131a5 100644 --- a/keyboards/moonlander/matrix.c +++ b/keyboards/moonlander/matrix.c | |||
@@ -16,55 +16,26 @@ | |||
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
17 | */ | 17 | */ |
18 | 18 | ||
19 | |||
20 | |||
21 | #include <stdint.h> | ||
22 | #include <stdbool.h> | ||
23 | #include <string.h> | ||
24 | #include <hal.h> | ||
25 | #include "timer.h" | ||
26 | #include "wait.h" | ||
27 | #include "print.h" | ||
28 | #include "matrix.h" | ||
29 | #include "action.h" | ||
30 | #include "keycode.h" | ||
31 | #include <string.h> | ||
32 | #include "moonlander.h" | 19 | #include "moonlander.h" |
33 | #include "i2c_master.h" | 20 | #include "i2c_master.h" |
34 | #include "debounce.h" | ||
35 | 21 | ||
36 | /* | 22 | /* |
37 | #define MATRIX_ROW_PINS { B10, B11, B12, B13, B14, B15 } outputs | 23 | #define MATRIX_ROW_PINS { B10, B11, B12, B13, B14, B15 } outputs |
38 | #define MATRIX_COL_PINS { A0, A1, A2, A3, A6, A7, B0 } inputs | 24 | #define MATRIX_COL_PINS { A0, A1, A2, A3, A6, A7, B0 } inputs |
39 | */ | 25 | */ |
40 | /* matrix state(1:on, 0:off) */ | 26 | /* matrix state(1:on, 0:off) */ |
41 | static matrix_row_t matrix[MATRIX_ROWS]; | 27 | extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values |
42 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | 28 | extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values |
43 | static matrix_row_t matrix_debouncing_right[MATRIX_COLS]; | 29 | static matrix_row_t raw_matrix_right[MATRIX_COLS]; |
44 | static bool debouncing = false; | ||
45 | static uint16_t debouncing_time = 0; | ||
46 | static bool debouncing_right = false; | ||
47 | static uint16_t debouncing_time_right = 0; | ||
48 | 30 | ||
49 | #define ROWS_PER_HAND (MATRIX_ROWS / 2) | 31 | #define ROWS_PER_HAND (MATRIX_ROWS / 2) |
50 | 32 | #ifndef MOONLANDER_I2C_TIMEOUT | |
51 | #ifndef MATRIX_IO_DELAY | 33 | # define MOONLANDER_I2C_TIMEOUT 100 |
52 | # define MATRIX_IO_DELAY 20 | ||
53 | #endif | 34 | #endif |
54 | 35 | ||
55 | extern bool mcp23018_leds[3]; | 36 | extern bool mcp23018_leds[3]; |
56 | extern bool is_launching; | 37 | extern bool is_launching; |
57 | 38 | ||
58 | __attribute__((weak)) void matrix_init_user(void) {} | ||
59 | |||
60 | __attribute__((weak)) void matrix_scan_user(void) {} | ||
61 | |||
62 | __attribute__((weak)) void matrix_init_kb(void) { matrix_init_user(); } | ||
63 | |||
64 | __attribute__((weak)) void matrix_scan_kb(void) { matrix_scan_user(); } | ||
65 | |||
66 | __attribute__((weak)) void matrix_io_delay(void) { wait_us(MATRIX_IO_DELAY); } | ||
67 | |||
68 | bool mcp23018_initd = false; | 39 | bool mcp23018_initd = false; |
69 | static uint8_t mcp23018_reset_loop; | 40 | static uint8_t mcp23018_reset_loop; |
70 | 41 | ||
@@ -81,14 +52,14 @@ void mcp23018_init(void) { | |||
81 | mcp23018_tx[1] = 0b00000000; // A is output | 52 | mcp23018_tx[1] = 0b00000000; // A is output |
82 | mcp23018_tx[2] = 0b00111111; // B is inputs | 53 | mcp23018_tx[2] = 0b00111111; // B is inputs |
83 | 54 | ||
84 | if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx, 3, I2C_TIMEOUT)) { | 55 | if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx, 3, MOONLANDER_I2C_TIMEOUT)) { |
85 | dprintf("error hori\n"); | 56 | dprintf("error hori\n"); |
86 | } else { | 57 | } else { |
87 | mcp23018_tx[0] = 0x0C; // GPPUA | 58 | mcp23018_tx[0] = 0x0C; // GPPUA |
88 | mcp23018_tx[1] = 0b10000000; // A is not pulled-up | 59 | mcp23018_tx[1] = 0b10000000; // A is not pulled-up |
89 | mcp23018_tx[2] = 0b11111111; // B is pulled-up | 60 | mcp23018_tx[2] = 0b11111111; // B is pulled-up |
90 | 61 | ||
91 | if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx, 3, I2C_TIMEOUT)) { | 62 | if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx, 3, MOONLANDER_I2C_TIMEOUT)) { |
92 | dprintf("error hori\n"); | 63 | dprintf("error hori\n"); |
93 | } else { | 64 | } else { |
94 | mcp23018_initd = is_launching = true; | 65 | mcp23018_initd = is_launching = true; |
@@ -96,10 +67,9 @@ void mcp23018_init(void) { | |||
96 | } | 67 | } |
97 | } | 68 | } |
98 | 69 | ||
99 | void matrix_init(void) { | 70 | void matrix_init_custom(void) { |
100 | dprintf("matrix init\n"); | 71 | dprintf("matrix init\n"); |
101 | // debug_matrix = true; | 72 | // debug_matrix = true; |
102 | |||
103 | // outputs | 73 | // outputs |
104 | setPinOutput(B10); | 74 | setPinOutput(B10); |
105 | setPinOutput(B11); | 75 | setPinOutput(B11); |
@@ -117,16 +87,10 @@ void matrix_init(void) { | |||
117 | setPinInputLow(A7); | 87 | setPinInputLow(A7); |
118 | setPinInputLow(B0); | 88 | setPinInputLow(B0); |
119 | 89 | ||
120 | memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t)); | ||
121 | memset(matrix_debouncing, 0, MATRIX_ROWS * sizeof(matrix_row_t)); | ||
122 | memset(matrix_debouncing_right, 0, MATRIX_COLS * sizeof(matrix_row_t)); | ||
123 | |||
124 | mcp23018_init(); | 90 | mcp23018_init(); |
125 | |||
126 | matrix_init_quantum(); | ||
127 | } | 91 | } |
128 | 92 | ||
129 | uint8_t matrix_scan(void) { | 93 | bool matrix_scan_custom(matrix_row_t current_matrix[]) { |
130 | bool changed = false; | 94 | bool changed = false; |
131 | 95 | ||
132 | // Try to re-init right side | 96 | // Try to re-init right side |
@@ -151,7 +115,7 @@ uint8_t matrix_scan(void) { | |||
151 | matrix_row_t data = 0; | 115 | matrix_row_t data = 0; |
152 | // actual matrix | 116 | // actual matrix |
153 | for (uint8_t row = 0; row <= ROWS_PER_HAND; row++) { | 117 | for (uint8_t row = 0; row <= ROWS_PER_HAND; row++) { |
154 | // strobe row | 118 | // strobe row |
155 | switch (row) { | 119 | switch (row) { |
156 | case 0: writePinHigh(B10); break; | 120 | case 0: writePinHigh(B10); break; |
157 | case 1: writePinHigh(B11); break; | 121 | case 1: writePinHigh(B11); break; |
@@ -172,7 +136,7 @@ uint8_t matrix_scan(void) { | |||
172 | mcp23018_tx[1] = (0b01111111 & ~(1 << (row))) | ((uint8_t)!mcp23018_leds[2] << 7); // activate row | 136 | mcp23018_tx[1] = (0b01111111 & ~(1 << (row))) | ((uint8_t)!mcp23018_leds[2] << 7); // activate row |
173 | mcp23018_tx[2] = ((uint8_t)!mcp23018_leds[1] << 6) | ((uint8_t)!mcp23018_leds[0] << 7); // activate row | 137 | mcp23018_tx[2] = ((uint8_t)!mcp23018_leds[1] << 6) | ((uint8_t)!mcp23018_leds[0] << 7); // activate row |
174 | 138 | ||
175 | if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx, 3, I2C_TIMEOUT)) { | 139 | if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx, 3, MOONLANDER_I2C_TIMEOUT)) { |
176 | dprintf("error hori\n"); | 140 | dprintf("error hori\n"); |
177 | mcp23018_initd = false; | 141 | mcp23018_initd = false; |
178 | } | 142 | } |
@@ -180,22 +144,23 @@ uint8_t matrix_scan(void) { | |||
180 | // read col | 144 | // read col |
181 | 145 | ||
182 | mcp23018_tx[0] = 0x13; // GPIOB | 146 | mcp23018_tx[0] = 0x13; // GPIOB |
183 | if (MSG_OK != i2c_readReg(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx[0], &mcp23018_rx[0], 1, I2C_TIMEOUT)) { | 147 | if (MSG_OK != i2c_readReg(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx[0], &mcp23018_rx[0], 1, MOONLANDER_I2C_TIMEOUT)) { |
184 | dprintf("error vert\n"); | 148 | dprintf("error vert\n"); |
185 | mcp23018_initd = false; | 149 | mcp23018_initd = false; |
186 | } | 150 | } |
187 | 151 | ||
188 | data = ~(mcp23018_rx[0] & 0b00111111); | 152 | data = ~(mcp23018_rx[0] & 0b00111111); |
189 | // data = 0x01; | 153 | // data = 0x01; |
154 | } else { | ||
155 | data = 0; | ||
156 | } | ||
190 | 157 | ||
191 | if (matrix_debouncing_right[row] != data) { | 158 | if (raw_matrix_right[row] != data) { |
192 | matrix_debouncing_right[row] = data; | 159 | raw_matrix_right[row] = data; |
193 | debouncing_right = true; | 160 | changed = true; |
194 | debouncing_time_right = timer_read(); | ||
195 | changed = true; | ||
196 | } | ||
197 | } | 161 | } |
198 | 162 | ||
163 | |||
199 | // left side | 164 | // left side |
200 | if (row < ROWS_PER_HAND) { | 165 | if (row < ROWS_PER_HAND) { |
201 | // i2c comm incur enough wait time | 166 | // i2c comm incur enough wait time |
@@ -224,79 +189,28 @@ uint8_t matrix_scan(void) { | |||
224 | case 6: break; | 189 | case 6: break; |
225 | } | 190 | } |
226 | 191 | ||
227 | if (matrix_debouncing[row] != data) { | 192 | if (current_matrix[row] != data) { |
228 | matrix_debouncing[row] = data; | 193 | current_matrix[row] = data; |
229 | debouncing = true; | ||
230 | debouncing_time = timer_read(); | ||
231 | changed = true; | 194 | changed = true; |
232 | } | 195 | } |
233 | } | 196 | } |
234 | } | 197 | } |
235 | 198 | for (uint8_t row = 0; row < ROWS_PER_HAND; row++) { | |
236 | // Debounce both hands | 199 | current_matrix[11 - row] = 0; |
237 | if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) { | 200 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { |
238 | for (int row = 0; row < ROWS_PER_HAND; row++) { | 201 | current_matrix[11 - row] |= ((raw_matrix_right[6 - col] & (1 << row) ? 1 : 0) << col); |
239 | matrix[row] = matrix_debouncing[row]; | ||
240 | } | ||
241 | debouncing = false; | ||
242 | } | ||
243 | |||
244 | if (debouncing_right && timer_elapsed(debouncing_time_right) > DEBOUNCE && mcp23018_initd) { | ||
245 | for (int row = 0; row < ROWS_PER_HAND; row++) { | ||
246 | matrix[11 - row] = 0; | ||
247 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
248 | matrix[11 - row] |= ((matrix_debouncing_right[6 - col] & (1 << row) ? 1 : 0) << col); | ||
249 | } | ||
250 | } | ||
251 | debouncing_right = false; | ||
252 | } | ||
253 | |||
254 | matrix_scan_quantum(); | ||
255 | |||
256 | return (uint8_t)changed; | ||
257 | } | ||
258 | |||
259 | bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & (1 << col)); } | ||
260 | |||
261 | matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; } | ||
262 | |||
263 | void matrix_print(void) { | ||
264 | dprintf("\nr/c 01234567\n"); | ||
265 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
266 | dprintf("%X0: ", row); | ||
267 | matrix_row_t data = matrix_get_row(row); | ||
268 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
269 | if (data & (1 << col)) | ||
270 | dprintf("1"); | ||
271 | else | ||
272 | dprintf("0"); | ||
273 | } | 202 | } |
274 | dprintf("\n"); | ||
275 | } | 203 | } |
204 | return changed; | ||
276 | } | 205 | } |
277 | 206 | ||
278 | // DO NOT REMOVE | 207 | // DO NOT REMOVE |
279 | // Needed for proper wake/sleep | 208 | // Needed for proper wake/sleep |
280 | void matrix_power_up(void) { | 209 | void matrix_power_up(void) { |
281 | bool temp_launching = is_launching; | 210 | bool temp_launching = is_launching; |
282 | // outputs | ||
283 | setPinOutput(B10); | ||
284 | setPinOutput(B11); | ||
285 | setPinOutput(B12); | ||
286 | setPinOutput(B13); | ||
287 | setPinOutput(B14); | ||
288 | setPinOutput(B15); | ||
289 | 211 | ||
290 | // inputs | 212 | matrix_init_custom(); |
291 | setPinInputLow(A0); | ||
292 | setPinInputLow(A1); | ||
293 | setPinInputLow(A2); | ||
294 | setPinInputLow(A3); | ||
295 | setPinInputLow(A6); | ||
296 | setPinInputLow(A7); | ||
297 | setPinInputLow(B0); | ||
298 | 213 | ||
299 | mcp23018_init(); | ||
300 | is_launching = temp_launching; | 214 | is_launching = temp_launching; |
301 | if (!is_launching) { | 215 | if (!is_launching) { |
302 | ML_LED_1(false); | 216 | ML_LED_1(false); |
diff --git a/keyboards/moonlander/rules.mk b/keyboards/moonlander/rules.mk index 23bf7b708..ed448ff43 100644 --- a/keyboards/moonlander/rules.mk +++ b/keyboards/moonlander/rules.mk | |||
@@ -19,8 +19,7 @@ NKRO_ENABLE = yes # USB Nkey Rollover | |||
19 | BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality | 19 | BACKLIGHT_ENABLE = no # Enable keyboard backlight functionality |
20 | RGBLIGHT_ENABLE = no # Enable keyboard RGB underglow | 20 | RGBLIGHT_ENABLE = no # Enable keyboard RGB underglow |
21 | AUDIO_ENABLE = yes # Audio output | 21 | AUDIO_ENABLE = yes # Audio output |
22 | CUSTOM_MATRIX = yes | 22 | CUSTOM_MATRIX = lite |
23 | DEBOUNCE_TYPE = custom | ||
24 | SWAP_HANDS_ENABLE = yes | 23 | SWAP_HANDS_ENABLE = yes |
25 | RGB_MATRIX_ENABLE = yes | 24 | RGB_MATRIX_ENABLE = yes |
26 | RGB_MATRIX_DRIVER = IS31FL3731 | 25 | RGB_MATRIX_DRIVER = IS31FL3731 |