You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
8.4 KiB

2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
2020 November 28 Breaking Changes Update (#11053) * Branch point for 2020 November 28 Breaking Change * Remove matrix_col_t to allow MATRIX_ROWS > 32 (#10183) * Add support for soft serial to ATmega32U2 (#10204) * Change MIDI velocity implementation to allow direct control of velocity value (#9940) * Add ability to build a subset of all keyboards based on platform. * Actually use eeprom_driver_init(). * Make bootloader_jump weak for ChibiOS. (#10417) * Joystick 16-bit support (#10439) * Per-encoder resolutions (#10259) * Share button state from mousekey to pointing_device (#10179) * Add hotfix for chibios keyboards not wake (#10088) * Add advanced/efficient RGB Matrix Indicators (#8564) * Naming change. * Support for STM32 GPIOF,G,H,I,J,K (#10206) * Add milc as a dependency and remove the installed milc (#10563) * ChibiOS upgrade: early init conversions (#10214) * ChibiOS upgrade: configuration file migrator (#9952) * Haptic and solenoid cleanup (#9700) * XD75 cleanup (#10524) * OLED display update interval support (#10388) * Add definition based on currently-selected serial driver. (#10716) * New feature: Retro Tapping per key (#10622) * Allow for modification of output RGB values when using rgblight/rgb_matrix. (#10638) * Add housekeeping task callbacks so that keyboards/keymaps are capable of executing code for each main loop iteration. (#10530) * Rescale both ChibiOS and AVR backlighting. * Reduce Helix keyboard build variation (#8669) * Minor change to behavior allowing display updates to continue between task ticks (#10750) * Some GPIO manipulations in matrix.c change to atomic. (#10491) * qmk cformat (#10767) * [Keyboard] Update the Speedo firmware for v3.0 (#10657) * Maartenwut/Maarten namechange to evyd13/Evy (#10274) * [quantum] combine repeated lines of code (#10837) * Add step sequencer feature (#9703) * aeboards/ext65 refactor (#10820) * Refactor xelus/dawn60 for Rev2 later (#10584) * add DEBUG_MATRIX_SCAN_RATE_ENABLE to common_features.mk (#10824) * [Core] Added `add_oneshot_mods` & `del_oneshot_mods` (#10549) * update chibios os usb for the otg driver (#8893) * Remove HD44780 References, Part 4 (#10735) * [Keyboard] Add Valor FRL TKL (+refactor) (#10512) * Fix cursor position bug in oled_write_raw functions (#10800) * Fixup version.h writing when using SKIP_VERSION=yes (#10972) * Allow for certain code in the codebase assuming length of string. (#10974) * Add AT90USB support for serial.c (#10706) * Auto shift: support repeats and early registration (#9826) * Rename ledmatrix.h to match .c file (#7949) * Split RGB_MATRIX_ENABLE into _ENABLE and _DRIVER (#10231) * Split LED_MATRIX_ENABLE into _ENABLE and _DRIVER (#10840) * Merge point for 2020 Nov 28 Breaking Change
3 years ago
  1. /*
  2. * Copyright 2018 Jack Humbert <jack.humb@gmail.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "encoder.h"
  18. #ifdef SPLIT_KEYBOARD
  19. # include "split_util.h"
  20. #endif
  21. // for memcpy
  22. #include <string.h>
  23. #ifndef ENCODER_MAP_KEY_DELAY
  24. # define ENCODER_MAP_KEY_DELAY 2
  25. #endif
  26. #if !defined(ENCODER_RESOLUTIONS) && !defined(ENCODER_RESOLUTION)
  27. # define ENCODER_RESOLUTION 4
  28. #endif
  29. #if !defined(ENCODERS_PAD_A) || !defined(ENCODERS_PAD_B)
  30. # error "No encoder pads defined by ENCODERS_PAD_A and ENCODERS_PAD_B"
  31. #endif
  32. extern volatile bool isLeftHand;
  33. static pin_t encoders_pad_a[NUM_ENCODERS_MAX_PER_SIDE] = ENCODERS_PAD_A;
  34. static pin_t encoders_pad_b[NUM_ENCODERS_MAX_PER_SIDE] = ENCODERS_PAD_B;
  35. #ifdef ENCODER_RESOLUTIONS
  36. static uint8_t encoder_resolutions[NUM_ENCODERS] = ENCODER_RESOLUTIONS;
  37. #endif
  38. #ifndef ENCODER_DIRECTION_FLIP
  39. # define ENCODER_CLOCKWISE true
  40. # define ENCODER_COUNTER_CLOCKWISE false
  41. #else
  42. # define ENCODER_CLOCKWISE false
  43. # define ENCODER_COUNTER_CLOCKWISE true
  44. #endif
  45. static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
  46. static uint8_t encoder_state[NUM_ENCODERS] = {0};
  47. static int8_t encoder_pulses[NUM_ENCODERS] = {0};
  48. // encoder counts
  49. static uint8_t thisCount;
  50. #ifdef SPLIT_KEYBOARD
  51. // encoder offsets for each hand
  52. static uint8_t thisHand, thatHand;
  53. // encoder counts for each hand
  54. static uint8_t thatCount;
  55. #endif
  56. static uint8_t encoder_value[NUM_ENCODERS] = {0};
  57. __attribute__((weak)) void encoder_wait_pullup_charge(void) {
  58. wait_us(100);
  59. }
  60. __attribute__((weak)) bool encoder_update_user(uint8_t index, bool clockwise) {
  61. return true;
  62. }
  63. __attribute__((weak)) bool encoder_update_kb(uint8_t index, bool clockwise) {
  64. return encoder_update_user(index, clockwise);
  65. }
  66. void encoder_init(void) {
  67. #ifdef SPLIT_KEYBOARD
  68. thisHand = isLeftHand ? 0 : NUM_ENCODERS_LEFT;
  69. thatHand = NUM_ENCODERS_LEFT - thisHand;
  70. thisCount = isLeftHand ? NUM_ENCODERS_LEFT : NUM_ENCODERS_RIGHT;
  71. thatCount = isLeftHand ? NUM_ENCODERS_RIGHT : NUM_ENCODERS_LEFT;
  72. #else // SPLIT_KEYBOARD
  73. thisCount = NUM_ENCODERS;
  74. #endif
  75. #ifdef ENCODER_TESTS
  76. // Annoying that we have to clear out values during initialisation here, but
  77. // because all the arrays are static locals, rerunning tests in the same
  78. // executable doesn't reset any of these. Kinda crappy having test-only code
  79. // here, but it's the simplest solution.
  80. memset(encoder_value, 0, sizeof(encoder_value));
  81. memset(encoder_state, 0, sizeof(encoder_state));
  82. memset(encoder_pulses, 0, sizeof(encoder_pulses));
  83. static const pin_t encoders_pad_a_left[] = ENCODERS_PAD_A;
  84. static const pin_t encoders_pad_b_left[] = ENCODERS_PAD_B;
  85. for (uint8_t i = 0; i < thisCount; i++) {
  86. encoders_pad_a[i] = encoders_pad_a_left[i];
  87. encoders_pad_b[i] = encoders_pad_b_left[i];
  88. }
  89. #endif
  90. #if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
  91. // Re-initialise the pads if it's the right-hand side
  92. if (!isLeftHand) {
  93. static const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT;
  94. static const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT;
  95. for (uint8_t i = 0; i < thisCount; i++) {
  96. encoders_pad_a[i] = encoders_pad_a_right[i];
  97. encoders_pad_b[i] = encoders_pad_b_right[i];
  98. }
  99. }
  100. #endif // defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
  101. // Encoder resolutions is handled purely master-side, so concatenate the two arrays
  102. #if defined(SPLIT_KEYBOARD) && defined(ENCODER_RESOLUTIONS)
  103. # if defined(ENCODER_RESOLUTIONS_RIGHT)
  104. static const uint8_t encoder_resolutions_right[NUM_ENCODERS_RIGHT] = ENCODER_RESOLUTIONS_RIGHT;
  105. # else // defined(ENCODER_RESOLUTIONS_RIGHT)
  106. static const uint8_t encoder_resolutions_right[NUM_ENCODERS_RIGHT] = ENCODER_RESOLUTIONS;
  107. # endif // defined(ENCODER_RESOLUTIONS_RIGHT)
  108. for (uint8_t i = 0; i < NUM_ENCODERS_RIGHT; i++) {
  109. encoder_resolutions[NUM_ENCODERS_LEFT + i] = encoder_resolutions_right[i];
  110. }
  111. #endif // defined(SPLIT_KEYBOARD) && defined(ENCODER_RESOLUTIONS)
  112. for (uint8_t i = 0; i < thisCount; i++) {
  113. setPinInputHigh(encoders_pad_a[i]);
  114. setPinInputHigh(encoders_pad_b[i]);
  115. }
  116. encoder_wait_pullup_charge();
  117. for (uint8_t i = 0; i < thisCount; i++) {
  118. encoder_state[i] = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  119. }
  120. }
  121. #ifdef ENCODER_MAP_ENABLE
  122. static void encoder_exec_mapping(uint8_t index, bool clockwise) {
  123. // The delays below cater for Windows and its wonderful requirements.
  124. action_exec(clockwise ? ENCODER_CW_EVENT(index, true) : ENCODER_CCW_EVENT(index, true));
  125. wait_ms(ENCODER_MAP_KEY_DELAY);
  126. action_exec(clockwise ? ENCODER_CW_EVENT(index, false) : ENCODER_CCW_EVENT(index, false));
  127. wait_ms(ENCODER_MAP_KEY_DELAY);
  128. }
  129. #endif // ENCODER_MAP_ENABLE
  130. static bool encoder_update(uint8_t index, uint8_t state) {
  131. bool changed = false;
  132. uint8_t i = index;
  133. #ifdef ENCODER_RESOLUTIONS
  134. const uint8_t resolution = encoder_resolutions[i];
  135. #else
  136. const uint8_t resolution = ENCODER_RESOLUTION;
  137. #endif
  138. #ifdef SPLIT_KEYBOARD
  139. index += thisHand;
  140. #endif
  141. encoder_pulses[i] += encoder_LUT[state & 0xF];
  142. if (encoder_pulses[i] >= resolution) {
  143. encoder_value[index]++;
  144. changed = true;
  145. #ifdef ENCODER_MAP_ENABLE
  146. encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE);
  147. #else // ENCODER_MAP_ENABLE
  148. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  149. #endif // ENCODER_MAP_ENABLE
  150. }
  151. if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
  152. encoder_value[index]--;
  153. changed = true;
  154. #ifdef ENCODER_MAP_ENABLE
  155. encoder_exec_mapping(index, ENCODER_CLOCKWISE);
  156. #else // ENCODER_MAP_ENABLE
  157. encoder_update_kb(index, ENCODER_CLOCKWISE);
  158. #endif // ENCODER_MAP_ENABLE
  159. }
  160. encoder_pulses[i] %= resolution;
  161. #ifdef ENCODER_DEFAULT_POS
  162. if ((state & 0x3) == ENCODER_DEFAULT_POS) {
  163. encoder_pulses[i] = 0;
  164. }
  165. #endif
  166. return changed;
  167. }
  168. bool encoder_read(void) {
  169. bool changed = false;
  170. for (uint8_t i = 0; i < thisCount; i++) {
  171. uint8_t new_status = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  172. if ((encoder_state[i] & 0x3) != new_status) {
  173. encoder_state[i] <<= 2;
  174. encoder_state[i] |= new_status;
  175. changed |= encoder_update(i, encoder_state[i]);
  176. }
  177. }
  178. return changed;
  179. }
  180. #ifdef SPLIT_KEYBOARD
  181. void last_encoder_activity_trigger(void);
  182. void encoder_state_raw(uint8_t *slave_state) {
  183. memcpy(slave_state, &encoder_value[thisHand], sizeof(uint8_t) * thisCount);
  184. }
  185. void encoder_update_raw(uint8_t *slave_state) {
  186. bool changed = false;
  187. for (uint8_t i = 0; i < thatCount; i++) { // Note inverted logic -- we want the opposite side
  188. const uint8_t index = i + thatHand;
  189. int8_t delta = slave_state[i] - encoder_value[index];
  190. while (delta > 0) {
  191. delta--;
  192. encoder_value[index]++;
  193. changed = true;
  194. # ifdef ENCODER_MAP_ENABLE
  195. encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE);
  196. # else // ENCODER_MAP_ENABLE
  197. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  198. # endif // ENCODER_MAP_ENABLE
  199. }
  200. while (delta < 0) {
  201. delta++;
  202. encoder_value[index]--;
  203. changed = true;
  204. # ifdef ENCODER_MAP_ENABLE
  205. encoder_exec_mapping(index, ENCODER_CLOCKWISE);
  206. # else // ENCODER_MAP_ENABLE
  207. encoder_update_kb(index, ENCODER_CLOCKWISE);
  208. # endif // ENCODER_MAP_ENABLE
  209. }
  210. }
  211. // Update the last encoder input time -- handled external to encoder_read() when we're running a split
  212. if (changed) last_encoder_activity_trigger();
  213. }
  214. #endif