Updated slave encoder sync to reduce dropped pulses (#7325)

* Updated slave encoder sync to reduce dropped pulses

* Fixing encoder direction

* Encoder behavior fixes, tested

* Update keyboards/rgbkb/sol/keymaps/xulkal/rules.mk

To make fauxpark happy

Co-Authored-By: fauxpark <fauxpark@gmail.com>

* Update custom_encoder.c

* Update rules.mk
This commit is contained in:
XScorpion2 2019-11-15 17:01:50 -06:00 committed by Drashna Jaelre
parent f6b5f6db76
commit 0f0c73f14a

View file

@ -38,14 +38,15 @@ static pin_t encoders_pad_b[] = ENCODERS_PAD_B;
static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0}; static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0}; static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0};
static int8_t encoder_pulses[NUMBER_OF_ENCODERS] = {0};
#ifdef SPLIT_KEYBOARD #ifdef SPLIT_KEYBOARD
// right half encoders come over as second set of encoders // right half encoders come over as second set of encoders
static int8_t encoder_value[NUMBER_OF_ENCODERS * 2] = {0}; static uint8_t encoder_value[NUMBER_OF_ENCODERS * 2] = {0};
// row offsets for each hand // row offsets for each hand
static uint8_t thisHand, thatHand; static uint8_t thisHand, thatHand;
#else #else
static int8_t encoder_value[NUMBER_OF_ENCODERS] = {0}; static uint8_t encoder_value[NUMBER_OF_ENCODERS] = {0};
#endif #endif
__attribute__((weak)) void encoder_update_user(int8_t index, bool clockwise) {} __attribute__((weak)) void encoder_update_user(int8_t index, bool clockwise) {}
@ -78,14 +79,16 @@ void encoder_init(void) {
} }
static void encoder_update(int8_t index, uint8_t state) { static void encoder_update(int8_t index, uint8_t state) {
encoder_value[index] += encoder_LUT[state & 0xF]; encoder_pulses[index] += encoder_LUT[state & 0xF];
if (encoder_value[index] >= ENCODER_RESOLUTION) { if (encoder_pulses[index] >= ENCODER_RESOLUTION) {
encoder_update_kb(index, false); encoder_value[index]++;
}
if (encoder_value[index] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
encoder_update_kb(index, true); encoder_update_kb(index, true);
} }
encoder_value[index] %= ENCODER_RESOLUTION; if (encoder_pulses[index] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
encoder_value[index]--;
encoder_update_kb(index, false);
}
encoder_pulses[index] %= ENCODER_RESOLUTION;
} }
void encoder_read(void) { void encoder_read(void) {
@ -101,11 +104,22 @@ void encoder_read(void) {
} }
#ifdef SPLIT_KEYBOARD #ifdef SPLIT_KEYBOARD
void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, encoder_state, sizeof(encoder_state)); } void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, &encoder_value[thisHand], sizeof(uint8_t) * NUMBER_OF_ENCODERS); }
void encoder_update_raw(uint8_t* slave_state) { void encoder_update_raw(uint8_t* slave_state) {
for (int i = 0; i < NUMBER_OF_ENCODERS; i++) { for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
encoder_update(i + thatHand, slave_state[i]); uint8_t index = i + thatHand;
int8_t delta = slave_state[i] - encoder_value[index];
while (delta > 0) {
delta--;
encoder_value[index]++;
encoder_update_kb(index, true);
}
while (delta < 0) {
delta++;
encoder_value[index]--;
encoder_update_kb(index, false);
}
} }
} }
#endif #endif