Skip to content

Commit

Permalink
qmk側developブランチを部分的に取り込み
Browse files Browse the repository at this point in the history
  • Loading branch information
ykeisuke committed Jul 12, 2024
1 parent 9ad3edb commit 7ed9863
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 75 deletions.
6 changes: 4 additions & 2 deletions keyboards/planck/rev7/keymaps/vial/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@
#define MOUSEKEY_WHEEL_INTERVAL 50
#define MOUSEKEY_WHEEL_DELTA 2

// エンコーダー関連
#define PLANCK_ENCODER_SETTLE_PIN_DELAY 20
/*
* Encoder options
*/
#define PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY 11
#define ENCODER_MAP_KEY_DELAY 10
#define ENCODER_RESOLUTION 4

Expand Down
86 changes: 16 additions & 70 deletions keyboards/planck/rev7/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "matrix.h"
#include <hal_pal.h>
#include <math.h>
#include "encoder.h"
#include "wait.h"

// STM32-specific watchdog config calculations
// timeout = 31.25us * PR * (RL + 1)
Expand All @@ -35,24 +35,12 @@
# define PLANCK_WATCHDOG_TIMEOUT 1.0
#endif

#if !defined(PLANCK_ENCODER_SETTLE_PIN_DELAY)
# define PLANCK_ENCODER_SETTLE_PIN_DELAY 20
#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++) {
Expand Down Expand Up @@ -80,7 +68,6 @@ void matrix_init_custom(void) {
#endif
}


bool matrix_scan_custom(matrix_row_t current_matrix[]) {
#ifndef PLANCK_WATCHDOG_DISABLE
// reset watchdog
Expand All @@ -97,7 +84,7 @@ bool matrix_scan_custom(matrix_row_t current_matrix[]) {
gpio_write_pin_high(matrix_col_pins[col]);

// need wait to settle pin state
wait_us(PLANCK_ENCODER_SETTLE_PIN_DELAY);
wait_us(20);

// read row data
for (int row = 0; row < MATRIX_ROWS; row++) {
Expand All @@ -124,62 +111,21 @@ bool matrix_scan_custom(matrix_row_t current_matrix[]) {
return changed;
}

#if defined(ENCODER_ENABLE) || defined(ENCODER_MAP_ENABLE)
#if !defined(PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY)
# define PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY 10
#endif

#define ENCODER_CLOCKWISE true
#define ENCODER_COUNTER_CLOCKWISE false

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) {
//printf("encoder_queue_event: %d, %d\n", index, ENCODER_COUNTER_CLOCKWISE);
encoder_queue_event(index, ENCODER_COUNTER_CLOCKWISE);
}

// direction is arbitrary here, but this clockwise
if (encoder_pulses[i] <= -resolution) {
//printf("encoder_queue_event: %d, %d\n", index, ENCODER_CLOCKWISE);
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_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
36 changes: 33 additions & 3 deletions keyboards/planck/rev7/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_to

## Encoders

Encoders must have matching pulse & detent resolutions (e.g. 24/24) for the scanning to work properly. Multiple encoders can be used at the same time, and are zero-indexed (compared to being one-indexed on the PCB's silkscreen) in the `encoder_update_user(uint8_t index, bool clockwise)` function:
Encoders must have matching pulse & detent resolutions (e.g. 24/24) for the scanning to work properly. Multiple encoders can be used at the same time.
If an encoder has a switch built-in, it's connected to the key at that location with index number:

```
,-----------------------------------------------------------------------------------.
Expand All @@ -28,6 +29,35 @@ Encoders must have matching pulse & detent resolutions (e.g. 24/24) for the scan
`-----------------------------------------------------------------------------------'
```

Planck rev7 supports `ENCODER_ENABLE` and `ENCODER_MAP_ENABLE`. If both `ENCODER_MAP_ENABLE` and `ENCODER_ENABLE` are defined, `ENCODER_MAP_ENABLE` takes precedence. On the default keymap, each encoder will play its own rising/falling tone sequence when rotated, and will reset the pitch after one second of inactivity.

### With ENCODER_ENABLE

Define it as follows in `rules.mk`:

```
ENCODER_ENABLE = yes
```

Zero-indexed (compared to being one-indexed on the PCB's silkscreen) in the `encoder_update_user(uint8_t index, bool clockwise)` function.

### With ENCODER_MAP_ENABLE

Define it as follows in `rules.mk`:

```
ENCODER_ENABLE = yes
ENCODER_MAP_ENABLE = yes
```

If you enable `ENCODER_MAP_ENABLE`, define `const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS]` and configure your keycodes. If you enable `ENCODER_MAP_ENABLE`, `encoder_update_user` is not used directly.

Additionally, you can use the following `config.h` options:

```c
#define ENCODER_MAP_KEY_DELAY 10
#define ENCODER_RESOLUTION 4
```
## Some Planck-specific config.h options:
Expand All @@ -36,6 +66,6 @@ Encoders must have matching pulse & detent resolutions (e.g. 24/24) for the scan
#define PLANCK_WATCHDOG_TIMEOUT 1.0
// disables the watchdog timer - you may want to disable the watchdog timer if you use longer macros
#define PLANCK_WATCHDOG_DISABLE
// the resolution of the encoders used in the encoder matrix
#define PLANCK_ENCODER_RESOLUTION 4
// Sets the time to wait for the rotary encoder pin state to stabilize while scanning (Default is 20(us))
#define PLANCK_ENCODER_SETTLE_PIN_STATE_DELAY 20
```

0 comments on commit 7ed9863

Please sign in to comment.