Add encoder abstraction. (#21548)

This commit is contained in:
Nick Brassel 2024-02-18 21:17:15 +11:00 committed by GitHub
parent 2eb9ff8efd
commit 9d9cdaaa2d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
50 changed files with 863 additions and 653 deletions

View file

@ -26,7 +26,14 @@
},
"encoder": {
"rotary": [
{"pin_a": "B12", "pin_b": "B13"}
{"pin_a": "B12", "pin_b": "B13"},
{"pin_a": "B12", "pin_b": "B13"},
{"pin_a": "B12", "pin_b": "B13"},
{"pin_a": "B12", "pin_b": "B13"},
{"pin_a": "B12", "pin_b": "B13"},
{"pin_a": "B12", "pin_b": "B13"},
{"pin_a": "B12", "pin_b": "B13"},
{"pin_a": "B12", "pin_b": "B13"}
]
},
"features": {

View file

@ -32,31 +32,16 @@
#define STM32_IWDG_RL_MS(s) STM32_IWDG_RL_US(s * 1000.0)
#define STM32_IWDG_RL_S(s) STM32_IWDG_RL_US(s * 1000000.0)
#if !defined(PLANCK_ENCODER_RESOLUTION)
# define PLANCK_ENCODER_RESOLUTION 4
#endif
#if !defined(PLANCK_WATCHDOG_TIMEOUT)
# define PLANCK_WATCHDOG_TIMEOUT 1.0
#endif
#ifdef ENCODER_MAP_ENABLE
#error "The encoder map feature is not currently supported by the Planck's encoder matrix"
#endif
/* matrix state(1:on, 0:off) */
static pin_t matrix_row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
static pin_t matrix_col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
static matrix_row_t matrix_inverted[MATRIX_COLS];
#ifdef ENCODER_ENABLE
int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
uint8_t encoder_state[8] = {0};
int8_t encoder_pulses[8] = {0};
uint8_t encoder_value[8] = {0};
#endif
void matrix_init_custom(void) {
// actual matrix setup - cols
for (int i = 0; i < MATRIX_COLS; i++) {
@ -84,31 +69,6 @@ void matrix_init_custom(void) {
#endif
}
#ifdef ENCODER_ENABLE
bool encoder_update(uint8_t index, uint8_t state) {
bool changed = false;
uint8_t i = index;
encoder_pulses[i] += encoder_LUT[state & 0xF];
if (encoder_pulses[i] >= PLANCK_ENCODER_RESOLUTION) {
encoder_value[index]++;
changed = true;
encoder_update_kb(index, false);
}
if (encoder_pulses[i] <= -PLANCK_ENCODER_RESOLUTION) {
encoder_value[index]--;
changed = true;
encoder_update_kb(index, true);
}
encoder_pulses[i] %= PLANCK_ENCODER_RESOLUTION;
#ifdef ENCODER_DEFAULT_POS
encoder_pulses[i] = 0;
#endif
return changed;
}
#endif
bool matrix_scan_custom(matrix_row_t current_matrix[]) {
#ifndef PLANCK_WATCHDOG_DISABLE
// reset watchdog
@ -149,40 +109,16 @@ bool matrix_scan_custom(matrix_row_t current_matrix[]) {
changed |= old != current_matrix[row];
}
#ifdef ENCODER_ENABLE
// encoder-matrix functionality
// set up C/rows for encoder read
for (int i = 0; i < MATRIX_ROWS; i++) {
setPinOutput(matrix_row_pins[i]);
writePinHigh(matrix_row_pins[i]);
}
// set up A & B for reading
setPinInputHigh(B12);
setPinInputHigh(B13);
for (int i = 0; i < MATRIX_ROWS; i++) {
writePinLow(matrix_row_pins[i]);
wait_us(10);
uint8_t new_status = (palReadPad(GPIOB, 12) << 0) | (palReadPad(GPIOB, 13) << 1);
if ((encoder_state[i] & 0x3) != new_status) {
encoder_state[i] <<= 2;
encoder_state[i] |= new_status;
encoder_update(i, encoder_state[i]);
}
writePinHigh(matrix_row_pins[i]);
}
// revert A & B to matrix state
setPinInputLow(B12);
setPinInputLow(B13);
// revert C/rows to matrix state
for (int i = 0; i < MATRIX_ROWS; i++) {
setPinInputLow(matrix_row_pins[i]);
}
#endif
return changed;
}
uint8_t encoder_quadrature_read_pin(uint8_t index, bool pad_b) {
pin_t pin = pad_b ? B13: B12;
setPinInputHigh(pin);
writePinLow(matrix_row_pins[index]);
wait_us(10);
uint8_t ret = readPin(pin) ? 1 : 0;
setPinInputLow(matrix_row_pins[index]);
setPinInputLow(pin);
return ret;
}