diff --git a/docs/_summary.md b/docs/_summary.md index 318c526a854..75bc5cc7849 100644 --- a/docs/_summary.md +++ b/docs/_summary.md @@ -34,6 +34,7 @@ * [Bootmagic](feature_bootmagic.md) * [Command](feature_command.md) * [Dynamic Macros](feature_dynamic_macros.md) + * [Encoders](feature_encoders.md) * [Grave Escape](feature_grave_esc.md) * [Key Lock](feature_key_lock.md) * [Layouts](feature_layouts.md) diff --git a/docs/feature_encoders.md b/docs/feature_encoders.md new file mode 100644 index 00000000000..f482eefec83 --- /dev/null +++ b/docs/feature_encoders.md @@ -0,0 +1,41 @@ +# Encoders + +Basic encoders are supported by adding this to your `rules.mk`: + + ENCODER_ENABLE = yes + +and this to your `config.h`: + + #define NUMBER_OF_ENCODERS 1 + #define ENCODERS_PAD_A { B12 } + #define ENCODERS_PAD_B { B13 } + +Each PAD_A/B variable defines an array so multiple encoders can be defined, e.g.: + + #define ENCODERS_PAD_A { encoder1a, encoder2a } + #define ENCODERS_PAD_B { encoder1a, encoder2b } + +If your encoder's clockwise directions are incorrect, you can swap the A & B pad definitions. + +Additionally, the resolution can be specified in the same file (the default & suggested is 4): + + #define ENCODER_RESOLUTION 4 + +## Callbacks + +The callback functions can be inserted into your `.c`: + + void encoder_update_kb(uint8_t index, bool clockwise) { + encoder_update_user(index, clockwise); + } + +or `keymap.c`: + + void encoder_update_user(uint8_t index, bool clockwise) { + + } + + +## Hardware + +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. diff --git a/keyboards/planck/keymaps/default/keymap.c b/keyboards/planck/keymaps/default/keymap.c index 545ca422998..8c452c8dfb5 100644 --- a/keyboards/planck/keymaps/default/keymap.c +++ b/keyboards/planck/keymaps/default/keymap.c @@ -259,7 +259,7 @@ uint16_t muse_counter = 0; uint8_t muse_offset = 70; uint16_t muse_tempo = 50; -void encoder_update(bool clockwise) { +void encoder_update_user(int8_t index, bool clockwise) { if (muse_mode) { if (IS_LAYER_ON(_RAISE)) { if (clockwise) { diff --git a/quantum/encoder.c b/quantum/encoder.c index 022cc1c01c3..103ff8289bd 100644 --- a/quantum/encoder.c +++ b/quantum/encoder.c @@ -35,11 +35,11 @@ static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0}; static int8_t encoder_value[NUMBER_OF_ENCODERS] = {0}; __attribute__ ((weak)) -void encoder_update_user(bool clockwise) { } +void encoder_update_user(int8_t index, bool clockwise) { } __attribute__ ((weak)) -void encoder_update_kb(bool clockwise) { - encoder_update_user(clockwise); +void encoder_update_kb(int8_t index, bool clockwise) { + encoder_update_user(index, clockwise); } void encoder_init(void) { @@ -57,10 +57,10 @@ void encoder_read(void) { encoder_state[i] |= (readPad(encoders_pad_a[i]) << 0) | (readPad(encoders_pad_b[i]) << 1); encoder_value[i] += encoder_LUT[encoder_state[i] & 0xF]; if (encoder_value[i] >= ENCODER_RESOLUTION) { - encoder_update_kb(COUNTRECLOCKWISE); + encoder_update_kb(i, COUNTRECLOCKWISE); } if (encoder_value[i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise - encoder_update_kb(CLOCKWISE); + encoder_update_kb(i, CLOCKWISE); } encoder_value[i] %= ENCODER_RESOLUTION; }