Skip to content

Commit c3e2c18

Browse files
committed
revert encoder c
1 parent 36964cb commit c3e2c18

1 file changed

Lines changed: 102 additions & 38 deletions

File tree

Lines changed: 102 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,136 @@
1-
#include "quantum.h"
1+
// Copyright 2024 splitkb.com (support@splitkb.com)
2+
// SPDX-License-Identifier: GPL-2.0-or-later
3+
24
#include "split_util.h"
5+
#include "atomic_util.h"
36

47
#ifdef SPLIT_KEYBOARD
58
# define ROWS_PER_HAND (MATRIX_ROWS / 2)
69
#else
710
# define ROWS_PER_HAND (MATRIX_ROWS)
811
#endif
912

10-
#if defined(MATRIX_ROW_PINS_RIGHT) || defined(MATRIX_COL_PINS_RIGHT)
11-
#define SPLIT_MUTABLE
13+
#ifdef MATRIX_ROW_PINS_RIGHT
14+
# define SPLIT_MUTABLE_ROW
15+
#else
16+
# define SPLIT_MUTABLE_ROW const
17+
#endif
18+
#ifdef MATRIX_COL_PINS_RIGHT
19+
# define SPLIT_MUTABLE_COL
1220
#else
13-
#define SPLIT_MUTABLE const
21+
# define SPLIT_MUTABLE_COL const
22+
#endif
23+
24+
#ifndef MATRIX_INPUT_PRESSED_STATE
25+
# define MATRIX_INPUT_PRESSED_STATE 0
1426
#endif
1527

16-
static SPLIT_MUTABLE pin_t row_pins[ROWS_PER_HAND] = MATRIX_ROW_PINS;
17-
static SPLIT_MUTABLE pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
28+
#ifdef DIRECT_PINS
29+
static SPLIT_MUTABLE pin_t direct_pins[ROWS_PER_HAND][MATRIX_COLS] = DIRECT_PINS;
30+
#elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
31+
# ifdef MATRIX_ROW_PINS
32+
static SPLIT_MUTABLE_ROW pin_t row_pins[ROWS_PER_HAND] = MATRIX_ROW_PINS;
33+
# endif // MATRIX_ROW_PINS
34+
# ifdef MATRIX_COL_PINS
35+
static SPLIT_MUTABLE_COL pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
36+
# endif // MATRIX_COL_PINS
37+
#endif
38+
39+
void matrix_init_kb(void) {
1840

19-
void matrix_init_user(void) {
2041
gpio_set_pin_input_high(HLC_ENCODER_BUTTON);
2142

43+
// Also need to define here otherwise right half is swapped
2244
if (!isLeftHand) {
23-
#ifdef MATRIX_ROW_PINS_RIGHT
24-
const pin_t row_pins_right[ROWS_PER_HAND] = MATRIX_ROW_PINS_RIGHT;
25-
for (uint8_t i = 0; i < ROWS_PER_HAND; i++) row_pins[i] = row_pins_right[i];
26-
#endif
27-
#ifdef MATRIX_COL_PINS_RIGHT
28-
const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
29-
for (uint8_t i = 0; i < MATRIX_COLS; i++) col_pins[i] = col_pins_right[i];
30-
#endif
45+
# ifdef MATRIX_ROW_PINS_RIGHT
46+
const pin_t row_pins_right[ROWS_PER_HAND] = MATRIX_ROW_PINS_RIGHT;
47+
for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
48+
row_pins[i] = row_pins_right[i];
49+
}
50+
# endif
51+
# ifdef MATRIX_COL_PINS_RIGHT
52+
const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT;
53+
for (uint8_t i = 0; i < MATRIX_COLS; i++) {
54+
col_pins[i] = col_pins_right[i];
55+
}
56+
# endif
3157
}
3258
}
3359

34-
static inline void select_row(uint8_t row) {
35-
if (row_pins[row] != NO_PIN) {
36-
gpio_set_pin_output(row_pins[row]);
37-
gpio_write_pin_low(row_pins[row]);
60+
static inline void setPinOutput_writeLow(pin_t pin) {
61+
ATOMIC_BLOCK_FORCEON {
62+
gpio_set_pin_output(pin);
63+
gpio_write_pin_low(pin);
3864
}
3965
}
4066

41-
static inline void unselect_row(uint8_t row) {
42-
if (row_pins[row] != NO_PIN) {
43-
gpio_set_pin_input_high(row_pins[row]);
67+
static inline void setPinOutput_writeHigh(pin_t pin) {
68+
ATOMIC_BLOCK_FORCEON {
69+
gpio_set_pin_output(pin);
70+
gpio_write_pin_high(pin);
71+
}
72+
}
73+
74+
static inline void setPinInputHigh_atomic(pin_t pin) {
75+
ATOMIC_BLOCK_FORCEON {
76+
gpio_set_pin_input_high(pin);
77+
}
78+
}
79+
80+
static inline uint8_t readMatrixPin(pin_t pin) {
81+
if (pin != NO_PIN) {
82+
return (gpio_read_pin(pin) == MATRIX_INPUT_PRESSED_STATE) ? 0 : 1;
83+
} else {
84+
return 1;
85+
}
86+
}
87+
88+
// THIS FUNCTION IS CHANGED, removed NO_PIN check
89+
static bool select_row(uint8_t row) {
90+
pin_t pin = row_pins[row];
91+
setPinOutput_writeLow(pin);
92+
return true;
93+
}
94+
95+
static void unselect_row(uint8_t row) {
96+
pin_t pin = row_pins[row];
97+
if (pin != NO_PIN) {
98+
# ifdef MATRIX_UNSELECT_DRIVE_HIGH
99+
setPinOutput_writeHigh(pin);
100+
# else
101+
setPinInputHigh_atomic(pin);
102+
# endif
44103
}
45104
}
46105

47106
void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
107+
// Start with a clear matrix row
48108
matrix_row_t current_row_value = 0;
49109

50-
// Custom handling for encoder button on the last row
110+
if (!select_row(current_row)) { // Select row
111+
return; // skip NO_PIN row
112+
}
113+
matrix_output_select_delay();
114+
115+
// ↓↓↓ THIS HAS BEEN ADDED/CHANGED
51116
if (current_row == (ROWS_PER_HAND - 1)) {
52-
if (!gpio_read_pin(HLC_ENCODER_BUTTON)) {
53-
current_row_value |= (1 << 0);
117+
current_row_value |= ((!gpio_read_pin(HLC_ENCODER_BUTTON)) & 1) << 0;
118+
} else {
119+
// For each col...
120+
matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
121+
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++, row_shifter <<= 1) {
122+
uint8_t pin_state = readMatrixPin(col_pins[col_index]);
123+
124+
// Populate the matrix row with the state of the col pin
125+
current_row_value |= pin_state ? 0 : row_shifter;
54126
}
55127
}
56-
// Standard column reading for other rows
57-
else {
58-
select_row(current_row);
59-
matrix_output_select_delay();
60-
61-
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
62-
if (col_pins[col] != NO_PIN && !gpio_read_pin(col_pins[col])) {
63-
current_row_value |= (1 << col);
64-
}
65-
}
128+
// ↑↑↑ THIS HAS BEEN ADDED/CHANGED
66129

67-
unselect_row(current_row);
68-
matrix_output_unselect_delay(current_row, current_row_value != 0);
69-
}
130+
// Unselect row
131+
unselect_row(current_row);
132+
matrix_output_unselect_delay(current_row, current_row_value != 0); // wait for all Col signals to go HIGH
70133

134+
// Update the matrix
71135
current_matrix[current_row] = current_row_value;
72136
}

0 commit comments

Comments
 (0)