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.

188 lines
6.0 KiB

  1. # Encoders
  2. Basic encoders are supported by adding this to your `rules.mk`:
  3. ```make
  4. ENCODER_ENABLE = yes
  5. ```
  6. and this to your `config.h`:
  7. ```c
  8. #define ENCODERS_PAD_A { B12 }
  9. #define ENCODERS_PAD_B { B13 }
  10. ```
  11. Each PAD_A/B variable defines an array so multiple encoders can be defined, e.g.:
  12. ```c
  13. #define ENCODERS_PAD_A { encoder1a, encoder2a }
  14. #define ENCODERS_PAD_B { encoder1b, encoder2b }
  15. ```
  16. If your encoder's clockwise directions are incorrect, you can swap the A & B pad definitions. They can also be flipped with a define:
  17. ```c
  18. #define ENCODER_DIRECTION_FLIP
  19. ```
  20. Additionally, the resolution, which defines how many pulses the encoder registers between each detent, can be defined with:
  21. ```c
  22. #define ENCODER_RESOLUTION 4
  23. ```
  24. It can also be defined per-encoder, by instead defining:
  25. ```c
  26. #define ENCODER_RESOLUTIONS { 4, 2 }
  27. ```
  28. For 4ร— encoders you also can assign default position if encoder skips pulses when it changes direction. For example, if your encoder send high level on both pins by default, define this:
  29. ```c
  30. #define ENCODER_DEFAULT_POS 0x3
  31. ```
  32. ## Split Keyboards
  33. If you are using different pinouts for the encoders on each half of a split keyboard, you can define the pinout (and optionally, resolutions) for the right half like this:
  34. ```c
  35. #define ENCODERS_PAD_A_RIGHT { encoder1a, encoder2a }
  36. #define ENCODERS_PAD_B_RIGHT { encoder1b, encoder2b }
  37. #define ENCODER_RESOLUTIONS_RIGHT { 2, 4 }
  38. ```
  39. If the `_RIGHT` definitions aren't specified in your `config.h`, then the non-`_RIGHT` versions will be applied to both sides of the split.
  40. Additionally, if one side does not have an encoder, you can specify `{}` for the pins/resolution -- for example, a split keyboard with only a right-side encoder:
  41. ```c
  42. #define ENCODERS_PAD_A { }
  43. #define ENCODERS_PAD_B { }
  44. #define ENCODER_RESOLUTIONS { }
  45. #define ENCODERS_PAD_A_RIGHT { B12 }
  46. #define ENCODERS_PAD_B_RIGHT { B13 }
  47. #define ENCODER_RESOLUTIONS_RIGHT { 4 }
  48. ```
  49. ## Encoder map :id=encoder-map
  50. Encoder mapping may be added to your `keymap.c`, which replicates the normal keyswitch layer handling functionality, but with encoders. Add this to your `rules.mk`:
  51. ```make
  52. ENCODER_MAP_ENABLE = yes
  53. ```
  54. Your `keymap.c` will then need an encoder mapping defined (for four layers and two encoders):
  55. ```c
  56. #if defined(ENCODER_MAP_ENABLE)
  57. const uint16_t PROGMEM encoder_map[][NUM_ENCODERS][2] = {
  58. [_BASE] = { ENCODER_CCW_CW(KC_MS_WH_UP, KC_MS_WH_DOWN), ENCODER_CCW_CW(KC_VOLD, KC_VOLU) },
  59. [_LOWER] = { ENCODER_CCW_CW(RGB_HUD, RGB_HUI), ENCODER_CCW_CW(RGB_SAD, RGB_SAI) },
  60. [_RAISE] = { ENCODER_CCW_CW(RGB_VAD, RGB_VAI), ENCODER_CCW_CW(RGB_SPD, RGB_SPI) },
  61. [_ADJUST] = { ENCODER_CCW_CW(RGB_RMOD, RGB_MOD), ENCODER_CCW_CW(KC_RIGHT, KC_LEFT) },
  62. };
  63. #endif
  64. ```
  65. ## Callbacks
  66. When not using `ENCODER_MAP_ENABLE = yes`, the callback functions can be inserted into your `<keyboard>.c`:
  67. ```c
  68. bool encoder_update_kb(uint8_t index, bool clockwise) {
  69. return encoder_update_user(index, clockwise);
  70. }
  71. ```
  72. or `keymap.c`:
  73. ```c
  74. bool encoder_update_user(uint8_t index, bool clockwise) {
  75. if (index == 0) { /* First encoder */
  76. if (clockwise) {
  77. tap_code_delay(KC_VOLU, 10);
  78. } else {
  79. tap_code_delay(KC_VOLD, 10);
  80. }
  81. } else if (index == 1) { /* Second encoder */
  82. if (clockwise) {
  83. rgb_matrix_increase_hue();
  84. } else {
  85. rgb_matrix_decrease_hue();
  86. }
  87. }
  88. return false;
  89. }
  90. ```
  91. !> If you return `true`, it will allow the keyboard level code to run as well. Returning `false` will override the keyboard level code, depending on how the keyboard function is set up.
  92. Layer conditions can also be used with the callback function like the following:
  93. ```c
  94. bool encoder_update_user(uint8_t index, bool clockwise) {
  95. switch(get_highest_layer(layer_state|default_layer_state)) {
  96. case 0:
  97. if (index == 0) {
  98. if (clockwise) {
  99. tap_code(KC_PGDN);
  100. } else {
  101. tap_code(KC_PGUP);
  102. }
  103. } else if (index == 1) {
  104. if (clockwise) {
  105. rgb_matrix_increase_speed();
  106. } else {
  107. rgb_matrix_decrease_speed();
  108. }
  109. }
  110. break;
  111. case 1:
  112. if (index == 0) {
  113. if (clockwise) {
  114. tap_code(KC_WH_D);
  115. } else {
  116. tap_code(KC_WH_U);
  117. }
  118. } else if (index == 1) {
  119. if (clockwise) {
  120. tap_code_delay(KC_VOLU, 10);
  121. } else {
  122. tap_code_delay(KC_VOLD, 10);
  123. }
  124. }
  125. break;
  126. }
  127. return false;
  128. }
  129. ```
  130. ?> Media and mouse countrol keycodes such as `KC_VOLU` and `KC_WH_D` requires `EXTRAKEY_ENABLE = yes` and `MOUSEKEY_ENABLE = yes` respectively in user's `rules.mk` if they are not enabled as default on keyboard level configuration.
  131. ## Hardware
  132. The A an B lines of the encoders should be wired directly to the MCU, and the C/common lines should be wired to ground.
  133. ## Multiple Encoders
  134. Multiple encoders may share pins so long as each encoder has a distinct pair of pins when the following conditions are met:
  135. - using detent encoders
  136. - pads must be high at the detent stability point which is called 'default position' in QMK
  137. - no more than two encoders sharing a pin can be turned at the same time
  138. For example you can support two encoders using only 3 pins like this
  139. ```
  140. #define ENCODERS_PAD_A { B1, B1 }
  141. #define ENCODERS_PAD_B { B2, B3 }
  142. ```
  143. You could even support three encoders using only three pins (one per encoder) however in this configuration, rotating two encoders which share pins simultaneously will often generate incorrect output. For example:
  144. ```
  145. #define ENCODERS_PAD_A { B1, B1, B2 }
  146. #define ENCODERS_PAD_B { B2, B3, B3 }
  147. ```
  148. Here rotating Encoder 0 `B1 B2` and Encoder 1 `B1 B3` could be interpreted as rotating Encoder 2 `B2 B3` or `B3 B2` depending on the timing. This may still be a useful configuration depending on your use case