1
0
Fork 0

Add support for encoder mapping. (#13286)

This commit is contained in:
Nick Brassel 2022-03-09 19:29:00 +11:00 committed by GitHub
parent 7121a228eb
commit 8d5eacb7dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 279 additions and 53 deletions

View file

@ -23,6 +23,10 @@
// for memcpy
#include <string.h>
#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
}
}