zmkfirmware / zmk

ZMK Firmware Repository
https://zmk.dev/
MIT License
2.83k stars 2.86k forks source link

EC11 encoder does not work as expected #2666

Open mynameiswutong opened 4 days ago

mynameiswutong commented 4 days ago

I created a new arm and shields and placed them in the folder corresponding to zmk \ app \ boards, then compiled them. However, I found that the encoder was not working properly. No matter how I rotated it, the encoder alternately triggered [CW_KEY] and [CW_KEY]. That is to say, if [CW_KEY] was triggered during the first rotation, the next one must be [CCW_KEY]; Has anyone encountered a similar situation before? Please help me, thank you very much!

This is the complete code file :code.zip

This is the log information I received: log

Here are my and JansnRF52.overlay file:

/*
 * Copyright (c) 2020 The ZMK Contributors
 *
 * SPDX-License-Identifier: MIT
 */

#include <dt-bindings/zmk/matrix_transform.h>

/ {
    chosen {
        zmk,kscan = &kscan0;
        zmk,matrix_transform = &default_transform;
        zmk,underglow = &led_strip;
    };

    kscan0: kscan {
        compatible = "zmk,kscan-gpio-matrix";
        wakeup-source;

        diode-direction = "col2row";
        row-gpios
            = <&gpio1 1 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
            , <&gpio1 3 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
            , <&gpio0 9 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
            , <&gpio0 10 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
            , <&gpio0 11 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
            , <&gpio0 12 (GPIO_ACTIVE_HIGH | GPIO_PULL_DOWN)>
            ;
        col-gpios
            = <&gpio0 13 GPIO_ACTIVE_HIGH>
            , <&gpio0 14 GPIO_ACTIVE_HIGH>
            , <&gpio0 15 GPIO_ACTIVE_HIGH>
            , <&gpio0 16 GPIO_ACTIVE_HIGH>
            ;
    };

    default_transform: keymap_transform_0 {
        compatible = "zmk,matrix-transform";
        columns = <4>;
        rows = <6>;
        // ┌───┬───┬───┬───┐
        // │Hom│end│del│mut│
        // ├───┼───┼───┼───┤
        // │NUM│ / │ * │ - │
        // ├───┼───┼───┼───┤
        // │ 7 │ 8 │ 9 │ + │
        // ├───┼───┼───┼───┤
        // │ 4 │ 5 │ 6 │FN │
        // ├───┼───┼───┼───┤
        // │ 1 │ 2 │ 3 │RET│
        // ├───┼───┼───┼───┤
        // │ 0 │   │ . │RET│
        // └───┴───┴───┴───┘

        map = <
            RC(0,0) RC(0,1) RC(0,2) RC(0,3)
            RC(1,0) RC(1,1) RC(1,2) RC(1,3)
            RC(2,0) RC(2,1) RC(2,2) RC(2,3)
            RC(3,0) RC(3,1) RC(3,2) RC(3,3)
            RC(4,0) RC(4,1) RC(4,2) RC(4,3)
            RC(5,0) RC(5,1) RC(5,2) RC(5,3)
        >;
    };

    lencoder: lencoder { 
        compatible = "alps,ec11";
        a-gpios = <&gpio0 7 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>;
        b-gpios = <&gpio0 8 (GPIO_ACTIVE_HIGH | GPIO_PULL_UP)>;
        steps = <30>;
        status = "okay";
    };

    sensors: sensors {
        compatible = "zmk,keymap-sensors";
        sensors = <&lencoder>;
        triggers-per-rotation = <30>;
    };

};

Here are my and JansnRF52.keymap file:

/*
 * Copyright (c) 2020 The ZMK Contributors
 *
 * SPDX-License-Identifier: MIT
 */

#include <behaviors.dtsi>
#include <dt-bindings/zmk/keys.h>
#include <dt-bindings/zmk/bt.h>
#include <dt-bindings/zmk/rgb.h>

#define DEFAULT 0
#define LOWER 1
#define MAGIC 2

/ {
    behaviors {
        rgb_encoder: rgb_encoder {
            compatible = "zmk,behavior-sensor-rotate";
            #sensor-binding-cells = <0>;
            bindings = <&rgb_ug RGB_BRI>, <&rgb_ug RGB_BRD>;
        };
    };
    keymap0: keymap {
        compatible = "zmk,keymap";

        default_layer {
            // ------------------------------------------------------------------------------------------
            // | ESC |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |  9  |  0  |  -  |  =  |   BKSP   |
            // | TAB  |  Q  |  W  |  E  |  R  |  T  |  Y  |  U  |  I  |  O  |  P  |  [  |  ]  |    \    |
            // | CAPS  |  A  |  S  |  D  |  F  |  G  |  H  |  J  |  K  |  L  |  ;  |  '  |     ENTER    |
            // ------------------------------------------------------------------------------------------
            bindings = <
                &to 0   &to 1    &to 2     &kp C_MUTE
                &bt BT_SEL 1  &none       &none        &none 
                &kp KP_N7  &kp KP_N8  &kp KP_N9   &kp KP_PLUS
                &kp KP_N4  &kp KP_N5  &kp KP_N6   &to 2
                &kp KP_N1  &kp KP_N2  &kp KP_N3   &kp KP_ENTER
                &kp KP_N0  &none      &kp KP_DOT  &none 
            >;
            sensor-bindings = <&inc_dec_kp KP_N0 KP_N1>;
        };

        lower_layer {
            bindings = <
                &to 0   &to 1    &to 2     &rgb_ug RGB_TOG
                &bt BT_SEL 1  &none       &none        &none 
                &kp KP_N7  &kp KP_N8  &kp KP_N9   &kp KP_PLUS
                &kp KP_N4  &kp KP_N5  &kp KP_N6   &to 2
                &kp KP_N1  &kp KP_N2  &kp KP_N3   &kp KP_ENTER
                &kp KP_N0  &none      &kp KP_DOT  &none 
            >;
        };

        fn_layer {
            bindings = <
                &bt BT_CLR    &sys_reset  &bootloader  &rgb_ug RGB_EFF
                &bt BT_SEL 0  &to 0       &to 1        &rgb_ug RGB_TOG 
                &bt BT_SEL 1  &kp C_VOL_UP       &kp K_VOL_DN        &to 2 
                &bt BT_SEL 2  &none       &none        &none
                &bt BT_SEL 3  &none       &none        &none 
                &bt BT_SEL 4  &none       &none        &none
            >;
            sensor-bindings = <&rgb_encoder>;
        };
    };
};
caksoylar commented 4 days ago

This is usually a symptom of your steps/triggers-per-rotation set incorrectly. You should find those values from the datasheet of your encoder.

If you can't, you can count triggers-per-rotation by counting how many detents you have per a full rotation. steps will usually be 2x or 4x the value of triggers-per-rotation, which you can try to figure out via trial and error.

mynameiswutong commented 4 days ago

Thank you very much for your help. I set triggers-per-rotation to the number of detent (30) of the rotary encoder, steps to 60, and then compiled it; But when I rotate my encoder, there is no output. Here is my log, please help me check again where the problem is. Thank you! log2 Snipaste_2024-11-24_07-32-21 Snipaste_2024-11-24_07-42-28

caksoylar commented 2 days ago

I am not sure the exact relation between data sheet numbers on detents and pulses vs. steps/triggers, I think detents should be triggers at least. The repo has a couple keyboards with triggers-per-rotation = <30>; and steps = <120>;, might be worth trying?

mynameiswutong commented 20 hours ago

Thanks for your suggestion, I made the changes as you suggested but when I rotate the encoder it does not give any output