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.

292 lines
9.7 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. # include "action.h"
  25. # define ENCODER_MAP_KEY_DELAY TAP_CODE_DELAY
  26. #endif
  27. #if !defined(ENCODER_RESOLUTIONS) && !defined(ENCODER_RESOLUTION)
  28. # define ENCODER_RESOLUTION 4
  29. #endif
  30. #if !defined(ENCODERS_PAD_A) || !defined(ENCODERS_PAD_B)
  31. # error "No encoder pads defined by ENCODERS_PAD_A and ENCODERS_PAD_B"
  32. #endif
  33. extern volatile bool isLeftHand;
  34. static pin_t encoders_pad_a[NUM_ENCODERS_MAX_PER_SIDE] = ENCODERS_PAD_A;
  35. static pin_t encoders_pad_b[NUM_ENCODERS_MAX_PER_SIDE] = ENCODERS_PAD_B;
  36. #ifdef ENCODER_RESOLUTIONS
  37. static uint8_t encoder_resolutions[NUM_ENCODERS] = ENCODER_RESOLUTIONS;
  38. #endif
  39. #ifndef ENCODER_DIRECTION_FLIP
  40. # define ENCODER_CLOCKWISE true
  41. # define ENCODER_COUNTER_CLOCKWISE false
  42. #else
  43. # define ENCODER_CLOCKWISE false
  44. # define ENCODER_COUNTER_CLOCKWISE true
  45. #endif
  46. static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
  47. static uint8_t encoder_state[NUM_ENCODERS] = {0};
  48. static int8_t encoder_pulses[NUM_ENCODERS] = {0};
  49. // encoder counts
  50. static uint8_t thisCount;
  51. #ifdef SPLIT_KEYBOARD
  52. // encoder offsets for each hand
  53. static uint8_t thisHand, thatHand;
  54. // encoder counts for each hand
  55. static uint8_t thatCount;
  56. #endif
  57. static uint8_t encoder_value[NUM_ENCODERS] = {0};
  58. __attribute__((weak)) void encoder_wait_pullup_charge(void) {
  59. wait_us(100);
  60. }
  61. __attribute__((weak)) bool encoder_update_user(uint8_t index, bool clockwise) {
  62. return true;
  63. }
  64. __attribute__((weak)) bool encoder_update_kb(uint8_t index, bool clockwise) {
  65. bool res = encoder_update_user(index, clockwise);
  66. #if !defined(ENCODER_TESTS)
  67. if (res) {
  68. if (clockwise) {
  69. # if defined(EXTRAKEY_ENABLE)
  70. tap_code_delay(KC_VOLU, 10);
  71. # elif defined(MOUSEKEY_ENABLE)
  72. tap_code_delay(KC_MS_WH_UP, 10);
  73. # else
  74. tap_code_delay(KC_PGDN, 10);
  75. # endif
  76. } else {
  77. # if defined(EXTRAKEY_ENABLE)
  78. tap_code_delay(KC_VOLD, 10);
  79. # elif defined(MOUSEKEY_ENABLE)
  80. tap_code_delay(KC_MS_WH_DOWN, 10);
  81. # else
  82. tap_code_delay(KC_PGUP, 10);
  83. # endif
  84. }
  85. }
  86. #endif // ENCODER_TESTS
  87. return res;
  88. }
  89. __attribute__((weak)) bool should_process_encoder(void) {
  90. return is_keyboard_master();
  91. }
  92. void encoder_init(void) {
  93. #ifdef SPLIT_KEYBOARD
  94. thisHand = isLeftHand ? 0 : NUM_ENCODERS_LEFT;
  95. thatHand = NUM_ENCODERS_LEFT - thisHand;
  96. thisCount = isLeftHand ? NUM_ENCODERS_LEFT : NUM_ENCODERS_RIGHT;
  97. thatCount = isLeftHand ? NUM_ENCODERS_RIGHT : NUM_ENCODERS_LEFT;
  98. #else // SPLIT_KEYBOARD
  99. thisCount = NUM_ENCODERS;
  100. #endif
  101. #ifdef ENCODER_TESTS
  102. // Annoying that we have to clear out values during initialisation here, but
  103. // because all the arrays are static locals, rerunning tests in the same
  104. // executable doesn't reset any of these. Kinda crappy having test-only code
  105. // here, but it's the simplest solution.
  106. memset(encoder_value, 0, sizeof(encoder_value));
  107. memset(encoder_state, 0, sizeof(encoder_state));
  108. memset(encoder_pulses, 0, sizeof(encoder_pulses));
  109. static const pin_t encoders_pad_a_left[] = ENCODERS_PAD_A;
  110. static const pin_t encoders_pad_b_left[] = ENCODERS_PAD_B;
  111. for (uint8_t i = 0; i < thisCount; i++) {
  112. encoders_pad_a[i] = encoders_pad_a_left[i];
  113. encoders_pad_b[i] = encoders_pad_b_left[i];
  114. }
  115. #endif
  116. #if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
  117. // Re-initialise the pads if it's the right-hand side
  118. if (!isLeftHand) {
  119. static const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT;
  120. static const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT;
  121. for (uint8_t i = 0; i < thisCount; i++) {
  122. encoders_pad_a[i] = encoders_pad_a_right[i];
  123. encoders_pad_b[i] = encoders_pad_b_right[i];
  124. }
  125. }
  126. #endif // defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
  127. // Encoder resolutions is handled purely master-side, so concatenate the two arrays
  128. #if defined(SPLIT_KEYBOARD) && defined(ENCODER_RESOLUTIONS)
  129. # if defined(ENCODER_RESOLUTIONS_RIGHT)
  130. static const uint8_t encoder_resolutions_right[NUM_ENCODERS_RIGHT] = ENCODER_RESOLUTIONS_RIGHT;
  131. # else // defined(ENCODER_RESOLUTIONS_RIGHT)
  132. static const uint8_t encoder_resolutions_right[NUM_ENCODERS_RIGHT] = ENCODER_RESOLUTIONS;
  133. # endif // defined(ENCODER_RESOLUTIONS_RIGHT)
  134. for (uint8_t i = 0; i < NUM_ENCODERS_RIGHT; i++) {
  135. encoder_resolutions[NUM_ENCODERS_LEFT + i] = encoder_resolutions_right[i];
  136. }
  137. #endif // defined(SPLIT_KEYBOARD) && defined(ENCODER_RESOLUTIONS)
  138. for (uint8_t i = 0; i < thisCount; i++) {
  139. setPinInputHigh(encoders_pad_a[i]);
  140. setPinInputHigh(encoders_pad_b[i]);
  141. }
  142. encoder_wait_pullup_charge();
  143. for (uint8_t i = 0; i < thisCount; i++) {
  144. encoder_state[i] = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  145. }
  146. }
  147. #ifdef ENCODER_MAP_ENABLE
  148. static void encoder_exec_mapping(uint8_t index, bool clockwise) {
  149. // The delays below cater for Windows and its wonderful requirements.
  150. action_exec(clockwise ? MAKE_ENCODER_CW_EVENT(index, true) : MAKE_ENCODER_CCW_EVENT(index, true));
  151. # if ENCODER_MAP_KEY_DELAY > 0
  152. wait_ms(ENCODER_MAP_KEY_DELAY);
  153. # endif // ENCODER_MAP_KEY_DELAY > 0
  154. action_exec(clockwise ? MAKE_ENCODER_CW_EVENT(index, false) : MAKE_ENCODER_CCW_EVENT(index, false));
  155. # if ENCODER_MAP_KEY_DELAY > 0
  156. wait_ms(ENCODER_MAP_KEY_DELAY);
  157. # endif // ENCODER_MAP_KEY_DELAY > 0
  158. }
  159. #endif // ENCODER_MAP_ENABLE
  160. static bool encoder_update(uint8_t index, uint8_t state) {
  161. bool changed = false;
  162. uint8_t i = index;
  163. #ifdef ENCODER_RESOLUTIONS
  164. const uint8_t resolution = encoder_resolutions[i];
  165. #else
  166. const uint8_t resolution = ENCODER_RESOLUTION;
  167. #endif
  168. #ifdef SPLIT_KEYBOARD
  169. index += thisHand;
  170. #endif
  171. encoder_pulses[i] += encoder_LUT[state & 0xF];
  172. #ifdef ENCODER_DEFAULT_POS
  173. if ((encoder_pulses[i] >= resolution) || (encoder_pulses[i] <= -resolution) || ((state & 0x3) == ENCODER_DEFAULT_POS)) {
  174. if (encoder_pulses[i] >= 1) {
  175. #else
  176. if (encoder_pulses[i] >= resolution) {
  177. #endif
  178. encoder_value[index]++;
  179. changed = true;
  180. #ifdef SPLIT_KEYBOARD
  181. if (should_process_encoder())
  182. #endif // SPLIT_KEYBOARD
  183. #ifdef ENCODER_MAP_ENABLE
  184. encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE);
  185. #else // ENCODER_MAP_ENABLE
  186. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  187. #endif // ENCODER_MAP_ENABLE
  188. }
  189. #ifdef ENCODER_DEFAULT_POS
  190. if (encoder_pulses[i] <= -1) {
  191. #else
  192. if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
  193. #endif
  194. encoder_value[index]--;
  195. changed = true;
  196. #ifdef SPLIT_KEYBOARD
  197. if (should_process_encoder())
  198. #endif // SPLIT_KEYBOARD
  199. #ifdef ENCODER_MAP_ENABLE
  200. encoder_exec_mapping(index, ENCODER_CLOCKWISE);
  201. #else // ENCODER_MAP_ENABLE
  202. encoder_update_kb(index, ENCODER_CLOCKWISE);
  203. #endif // ENCODER_MAP_ENABLE
  204. }
  205. encoder_pulses[i] %= resolution;
  206. #ifdef ENCODER_DEFAULT_POS
  207. encoder_pulses[i] = 0;
  208. }
  209. #endif
  210. return changed;
  211. }
  212. bool encoder_read(void) {
  213. bool changed = false;
  214. for (uint8_t i = 0; i < thisCount; i++) {
  215. uint8_t new_status = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  216. if ((encoder_state[i] & 0x3) != new_status) {
  217. encoder_state[i] <<= 2;
  218. encoder_state[i] |= new_status;
  219. changed |= encoder_update(i, encoder_state[i]);
  220. }
  221. }
  222. return changed;
  223. }
  224. #ifdef SPLIT_KEYBOARD
  225. void last_encoder_activity_trigger(void);
  226. void encoder_state_raw(uint8_t *slave_state) {
  227. memcpy(slave_state, &encoder_value[thisHand], sizeof(uint8_t) * thisCount);
  228. }
  229. void encoder_update_raw(uint8_t *slave_state) {
  230. bool changed = false;
  231. for (uint8_t i = 0; i < thatCount; i++) { // Note inverted logic -- we want the opposite side
  232. const uint8_t index = i + thatHand;
  233. int8_t delta = slave_state[i] - encoder_value[index];
  234. while (delta > 0) {
  235. delta--;
  236. encoder_value[index]++;
  237. changed = true;
  238. # ifdef ENCODER_MAP_ENABLE
  239. encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE);
  240. # else // ENCODER_MAP_ENABLE
  241. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  242. # endif // ENCODER_MAP_ENABLE
  243. }
  244. while (delta < 0) {
  245. delta++;
  246. encoder_value[index]--;
  247. changed = true;
  248. # ifdef ENCODER_MAP_ENABLE
  249. encoder_exec_mapping(index, ENCODER_CLOCKWISE);
  250. # else // ENCODER_MAP_ENABLE
  251. encoder_update_kb(index, ENCODER_CLOCKWISE);
  252. # endif // ENCODER_MAP_ENABLE
  253. }
  254. }
  255. // Update the last encoder input time -- handled external to encoder_read() when we're running a split
  256. if (changed) last_encoder_activity_trigger();
  257. }
  258. #endif