From 8d5eacb7dd76bfd45444ceb1efa9a9f840173552 Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Wed, 9 Mar 2022 19:29:00 +1100 Subject: Add support for encoder mapping. (#13286) --- quantum/encoder.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'quantum/encoder.c') diff --git a/quantum/encoder.c b/quantum/encoder.c index 0a3d6f577c..105bed0147 100644 --- a/quantum/encoder.c +++ b/quantum/encoder.c @@ -23,6 +23,10 @@ // for memcpy #include +#ifndef ENCODER_MAP_KEY_DELAY +# define ENCODER_MAP_KEY_DELAY 2 +#endif + #if !defined(ENCODER_RESOLUTIONS) && !defined(ENCODER_RESOLUTION) # define ENCODER_RESOLUTION 4 #endif @@ -135,6 +139,16 @@ void encoder_init(void) { } } +#ifdef ENCODER_MAP_ENABLE +static void encoder_exec_mapping(uint8_t index, bool clockwise) { + // The delays below cater for Windows and its wonderful requirements. + action_exec(clockwise ? ENCODER_CW_EVENT(index, true) : ENCODER_CCW_EVENT(index, true)); + wait_ms(ENCODER_MAP_KEY_DELAY); + action_exec(clockwise ? ENCODER_CW_EVENT(index, false) : ENCODER_CCW_EVENT(index, false)); + wait_ms(ENCODER_MAP_KEY_DELAY); +} +#endif // ENCODER_MAP_ENABLE + static bool encoder_update(uint8_t index, uint8_t state) { bool changed = false; uint8_t i = index; @@ -152,12 +166,20 @@ static bool encoder_update(uint8_t index, uint8_t state) { if (encoder_pulses[i] >= resolution) { encoder_value[index]++; changed = true; +#ifdef ENCODER_MAP_ENABLE + encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE); +#else // ENCODER_MAP_ENABLE encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE); +#endif // ENCODER_MAP_ENABLE } if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise encoder_value[index]--; changed = true; +#ifdef ENCODER_MAP_ENABLE + encoder_exec_mapping(index, ENCODER_CLOCKWISE); +#else // ENCODER_MAP_ENABLE encoder_update_kb(index, ENCODER_CLOCKWISE); +#endif // ENCODER_MAP_ENABLE } encoder_pulses[i] %= resolution; #ifdef ENCODER_DEFAULT_POS @@ -197,13 +219,21 @@ void encoder_update_raw(uint8_t *slave_state) { delta--; encoder_value[index]++; changed = true; +# ifdef ENCODER_MAP_ENABLE + encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE); +# else // ENCODER_MAP_ENABLE encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE); +# endif // ENCODER_MAP_ENABLE } while (delta < 0) { delta++; encoder_value[index]--; changed = true; +# ifdef ENCODER_MAP_ENABLE + encoder_exec_mapping(index, ENCODER_CLOCKWISE); +# else // ENCODER_MAP_ENABLE encoder_update_kb(index, ENCODER_CLOCKWISE); +# endif // ENCODER_MAP_ENABLE } } -- cgit v1.2.3