summary refs log tree commit diff
path: root/quantum/encoder.c
diff options
context:
space:
mode:
authorNick Brassel <nick@tzarc.org>2022-03-09 19:29:00 +1100
committerGitHub <noreply@github.com>2022-03-09 19:29:00 +1100
commit8d5eacb7dd76bfd45444ceb1efa9a9f840173552 (patch)
treeb6b8b641dd61f5de0c5b7ee1bf251f6a84043656 /quantum/encoder.c
parent7121a228eb204a0d697c97503ac7a28b762ab598 (diff)
Add support for encoder mapping. (#13286)
Diffstat (limited to 'quantum/encoder.c')
-rw-r--r--quantum/encoder.c30
1 files changed, 30 insertions, 0 deletions
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 <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
         }
     }