Skip to content

Commit

Permalink
Per-encoder resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
fauxpark committed Sep 12, 2020
1 parent 3430907 commit 9a05ecd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 6 additions & 0 deletions docs/feature_encoders.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ Additionally, the resolution, which defines how many pulses the encoder register
#define ENCODER_RESOLUTION 4
```
It can also be defined per-encoder, by instead defining:
```c
#define ENCODER_RESOLUTIONS { 4, 2 }
```
## Split Keyboards
If you are using different pinouts for the encoders on each half of a split keyboard, you can define the pinout for the right half like this:
Expand Down
18 changes: 14 additions & 4 deletions quantum/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
// for memcpy
#include <string.h>

#ifndef ENCODER_RESOLUTION
#if !defined(ENCODER_RESOLUTIONS) && !defined(ENCODER_RESOLUTION)
# define ENCODER_RESOLUTION 4
#endif

Expand All @@ -34,6 +34,9 @@
#define NUMBER_OF_ENCODERS (sizeof(encoders_pad_a) / sizeof(pin_t))
static pin_t encoders_pad_a[] = ENCODERS_PAD_A;
static pin_t encoders_pad_b[] = ENCODERS_PAD_B;
#ifdef ENCODER_RESOLUTIONS
static uint8_t encoder_resolutions[] = ENCODER_RESOLUTIONS;
#endif

#ifndef ENCODER_DIRECTION_FLIP
# define ENCODER_CLOCKWISE true
Expand Down Expand Up @@ -87,19 +90,26 @@ void encoder_init(void) {

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

#ifdef ENCODER_RESOLUTIONS
int8_t resolution = encoder_resolutions[i];
#else
int8_t resolution = ENCODER_RESOLUTION;
#endif

#ifdef SPLIT_KEYBOARD
index += thisHand;
#endif
encoder_pulses[i] += encoder_LUT[state & 0xF];
if (encoder_pulses[i] >= ENCODER_RESOLUTION) {
if (encoder_pulses[i] >= resolution) {
encoder_value[index]++;
encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
}
if (encoder_pulses[i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
encoder_value[index]--;
encoder_update_kb(index, ENCODER_CLOCKWISE);
}
encoder_pulses[i] %= ENCODER_RESOLUTION;
encoder_pulses[i] %= resolution;
}

void encoder_read(void) {
Expand Down

0 comments on commit 9a05ecd

Please sign in to comment.