aboutsummaryrefslogtreecommitdiff
path: root/keyboards/moonlander/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/moonlander/matrix.c')
-rw-r--r--keyboards/moonlander/matrix.c158
1 files changed, 82 insertions, 76 deletions
diff --git a/keyboards/moonlander/matrix.c b/keyboards/moonlander/matrix.c
index dbd44d161..8f2610fc6 100644
--- a/keyboards/moonlander/matrix.c
+++ b/keyboards/moonlander/matrix.c
@@ -129,10 +129,29 @@ void matrix_init(void) {
129uint8_t matrix_scan(void) { 129uint8_t matrix_scan(void) {
130 bool changed = false; 130 bool changed = false;
131 131
132 // Try to re-init right side
133 if (!mcp23018_initd) {
134 if (++mcp23018_reset_loop == 0) {
135 // if (++mcp23018_reset_loop >= 1300) {
136 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
137 // this will be approx bit more frequent than once per second
138 print("trying to reset mcp23018\n");
139 mcp23018_init();
140 if (!mcp23018_initd) {
141 print("left side not responding\n");
142 } else {
143 print("left side attached\n");
144#ifdef RGB_MATRIX_ENABLE
145 rgb_matrix_init();
146#endif
147 }
148 }
149 }
150
132 matrix_row_t data = 0; 151 matrix_row_t data = 0;
133 // actual matrix 152 // actual matrix
134 for (uint8_t row = 0; row < ROWS_PER_HAND; row++) { 153 for (uint8_t row = 0; row <= ROWS_PER_HAND; row++) {
135 // strobe row 154 // strobe row
136 switch (row) { 155 switch (row) {
137 case 0: writePinHigh(B10); break; 156 case 0: writePinHigh(B10); break;
138 case 1: writePinHigh(B11); break; 157 case 1: writePinHigh(B11); break;
@@ -140,94 +159,81 @@ uint8_t matrix_scan(void) {
140 case 3: writePinHigh(B13); break; 159 case 3: writePinHigh(B13); break;
141 case 4: writePinHigh(B14); break; 160 case 4: writePinHigh(B14); break;
142 case 5: writePinHigh(B15); break; 161 case 5: writePinHigh(B15); break;
162 case 6: break; // Left hand has 6 rows
143 } 163 }
144 164
145 // need wait to settle pin state
146 matrix_io_delay();
147
148 // read col data
149 data = (
150 (readPin(A0) << 0 ) |
151 (readPin(A1) << 1 ) |
152 (readPin(A2) << 2 ) |
153 (readPin(A3) << 3 ) |
154 (readPin(A6) << 4 ) |
155 (readPin(A7) << 5 ) |
156 (readPin(B0) << 6 )
157 );
158
159 // unstrobe row
160 switch (row) {
161 case 0: writePinLow(B10); break;
162 case 1: writePinLow(B11); break;
163 case 2: writePinLow(B12); break;
164 case 3: writePinLow(B13); break;
165 case 4: writePinLow(B14); break;
166 case 5: writePinLow(B15); break;
167 }
168
169 if (matrix_debouncing[row] != data) {
170 matrix_debouncing[row] = data;
171 debouncing = true;
172 debouncing_time = timer_read();
173 changed = true;
174 }
175 }
176
177 for (uint8_t row = 0; row <= ROWS_PER_HAND; row++) {
178 // right side 165 // right side
179 166 if (mcp23018_initd) {
180 if (!mcp23018_initd) { 167 // #define MCP23_ROW_PINS { GPB5, GBP4, GBP3, GBP2, GBP1, GBP0 } outputs
181 if (++mcp23018_reset_loop == 0) { 168 // #define MCP23_COL_PINS { GPA0, GBA1, GBA2, GBA3, GBA4, GBA5, GBA6 } inputs
182 // if (++mcp23018_reset_loop >= 1300) { 169
183 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans 170 // select row
184 // this will be approx bit more frequent than once per second 171 mcp23018_tx[0] = 0x12; // GPIOA
185 print("trying to reset mcp23018\n"); 172 mcp23018_tx[1] = (0b01111111 & ~(1 << (row))) | ((uint8_t)!mcp23018_leds[2] << 7); // activate row
186 mcp23018_init(); 173 mcp23018_tx[2] = ((uint8_t)!mcp23018_leds[1] << 6) | ((uint8_t)!mcp23018_leds[0] << 7); // activate row
187 if (!mcp23018_initd) { 174
188 print("left side not responding\n"); 175 if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx, 3, I2C_TIMEOUT)) {
189 } else { 176 dprintf("error hori\n");
190 print("left side attached\n"); 177 mcp23018_initd = false;
191#ifdef RGB_MATRIX_ENABLE
192 rgb_matrix_init();
193#endif
194 }
195 } 178 }
196 }
197 179
198 // #define MCP23_ROW_PINS { GPB5, GBP4, GBP3, GBP2, GBP1, GBP0 } outputs 180 // read col
199 // #define MCP23_COL_PINS { GPA0, GBA1, GBA2, GBA3, GBA4, GBA5, GBA6 } inputs
200 181
201 // select row 182 mcp23018_tx[0] = 0x13; // GPIOB
202 183 if (MSG_OK != i2c_readReg(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx[0], &mcp23018_rx[0], 1, I2C_TIMEOUT)) {
203 mcp23018_tx[0] = 0x12; // GPIOA 184 dprintf("error vert\n");
204 mcp23018_tx[1] = (0b01111111 & ~(1 << (row))) | ((uint8_t)!mcp23018_leds[2] << 7); // activate row 185 mcp23018_initd = false;
205 mcp23018_tx[2] = ((uint8_t)!mcp23018_leds[1] << 6) | ((uint8_t)!mcp23018_leds[0] << 7); // activate row 186 }
206
207 if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx, 3, I2C_TIMEOUT)) {
208 dprintf("error hori\n");
209 mcp23018_initd = false;
210 }
211 187
212 // read col 188 data = ~(mcp23018_rx[0] & 0b00111111);
189 // data = 0x01;
213 190
214 mcp23018_tx[0] = 0x13; // GPIOB 191 if (matrix_debouncing_right[row] != data) {
215 if (MSG_OK != i2c_readReg(MCP23018_DEFAULT_ADDRESS << 1, mcp23018_tx[0], &mcp23018_rx[0], 1, I2C_TIMEOUT)) { 192 matrix_debouncing_right[row] = data;
216 dprintf("error vert\n"); 193 debouncing_right = true;
217 mcp23018_initd = false; 194 debouncing_time_right = timer_read();
195 changed = true;
196 }
218 } 197 }
219 198
220 data = ~(mcp23018_rx[0] & 0b00111111); 199 // left side
221 // data = 0x01; 200 if (row < ROWS_PER_HAND) {
201 // i2c comm incur enough wait time
202 if (!mcp23018_initd) {
203 // need wait to settle pin state
204 matrix_io_delay();
205 }
206 // read col data
207 data = (
208 (readPin(A0) << 0 ) |
209 (readPin(A1) << 1 ) |
210 (readPin(A2) << 2 ) |
211 (readPin(A3) << 3 ) |
212 (readPin(A6) << 4 ) |
213 (readPin(A7) << 5 ) |
214 (readPin(B0) << 6 )
215 );
216 // unstrobe row
217 switch (row) {
218 case 0: writePinLow(B10); break;
219 case 1: writePinLow(B11); break;
220 case 2: writePinLow(B12); break;
221 case 3: writePinLow(B13); break;
222 case 4: writePinLow(B14); break;
223 case 5: writePinLow(B15); break;
224 case 6: break;
225 }
222 226
223 if (matrix_debouncing_right[row] != data) { 227 if (matrix_debouncing[row] != data) {
224 matrix_debouncing_right[row] = data; 228 matrix_debouncing[row] = data;
225 debouncing_right = true; 229 debouncing = true;
226 debouncing_time_right = timer_read(); 230 debouncing_time = timer_read();
227 changed = true; 231 changed = true;
232 }
228 } 233 }
229 } 234 }
230 235
236 // Debounce both hands
231 if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) { 237 if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) {
232 for (int row = 0; row < ROWS_PER_HAND; row++) { 238 for (int row = 0; row < ROWS_PER_HAND; row++) {
233 matrix[row] = matrix_debouncing[row]; 239 matrix[row] = matrix_debouncing[row];