Skip to content

Commit

Permalink
Use abstract codes
Browse files Browse the repository at this point in the history
  • Loading branch information
ykeisuke committed Jun 25, 2024
1 parent f52be72 commit e34700f
Showing 1 changed file with 10 additions and 64 deletions.
74 changes: 10 additions & 64 deletions keyboards/planck/rev7/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <hal_pal.h>
#include <math.h>
#include "wait.h"
#include "encoder.h"

// STM32-specific watchdog config calculations
// timeout = 31.25us * PR * (RL + 1)
Expand Down Expand Up @@ -113,73 +112,20 @@ bool matrix_scan_custom(matrix_row_t current_matrix[]) {
}

#if defined(ENCODER_ENABLE) || defined(ENCODER_MAP_ENABLE)

#if !defined(PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY)
# define PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY 20
#endif
#if !defined(ENCODER_RESOLUTION)
# define ENCODER_RESOLUTION 4
# define PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY 10
#endif

#define ENCODER_CLOCKWISE true
#define ENCODER_COUNTER_CLOCKWISE false

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};

static void encoder_handle_state_change(uint8_t index, uint8_t state) {
uint8_t i = index;

const uint8_t resolution = ENCODER_RESOLUTION;

encoder_pulses[i] += encoder_LUT[state & 0xF];

if (encoder_pulses[i] >= resolution) {
encoder_queue_event(index, ENCODER_COUNTER_CLOCKWISE);
}

// direction is arbitrary here, but this clockwise
if (encoder_pulses[i] <= -resolution) {
encoder_queue_event(index, ENCODER_CLOCKWISE);
}
encoder_pulses[i] %= resolution;

void encoder_quadrature_init_pin(uint8_t index, bool pad_b) {
}

void encoder_driver_task(void) {

// set up C/rows for encoder read
for (int i = 0; i < MATRIX_ROWS; i++) {
gpio_set_pin_output(matrix_row_pins[i]);
gpio_write_pin_high(matrix_row_pins[i]);
}

// set up A & B for reading
gpio_set_pin_input_high(B12);
gpio_set_pin_input_high(B13);

for (int i = 0; i < MATRIX_ROWS; i++) {
gpio_write_pin_low(matrix_row_pins[i]);
wait_us(PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY);
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_handle_state_change(i, encoder_state[i]);
}
gpio_write_pin_high(matrix_row_pins[i]);
}

// revert A & B to matrix state
gpio_set_pin_input_low(B12);
gpio_set_pin_input_low(B13);

// revert C/rows to matrix state
for (int i = 0; i < MATRIX_ROWS; i++) {
gpio_set_pin_input_low(matrix_row_pins[i]);
}

uint8_t encoder_quadrature_read_pin(uint8_t index, bool pad_b) {
pin_t col_pin = pad_b ? B13 : B12;
gpio_set_pin_output(col_pin);
gpio_write_pin_high(col_pin);
wait_us(PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY);
uint8_t ret = gpio_read_pin(matrix_row_pins[index]) ? 0 : 1;
gpio_set_pin_input_low(col_pin);
return ret;
}

#endif // ENCODER_ENABLE || ENCODER_MAP_ENABLE

0 comments on commit e34700f

Please sign in to comment.