Nrfx_qdec double trigger for one turn

I am having an issue with the nrfx_qdec driver in zephyr, I am trying to simply detect when the turn happens and the direction of the turn is.
Most of the time this is triggered fine, but sometimes for one turn of the encoder I get 2 triggers, I am trying to figure out how I should best debounce this, whether in the qdec configuration 
or something on the my side in data_ready_handler for example.
I am using NRF connect SDK v2.9.1 in VScode
MCU is nrf52810_xxAA_REV2
qdec dts config 
&qdec {
    status = "okay";
    pinctrl-0 = <&qdec_default>;
    pinctrl-1 = <&qdec_sleep>;
    pinctrl-names = "default", "sleep";

    steps = <20>;
    led-pre = <0>;
};

Here is the code I am using to test out the qdec.

static
void data_ready_handler(const struct device *dev, const struct sensor_trigger *trig)
{
    struct sensor_value value;

    int err = sensor_sample_fetch_chan(qdec_dev, SENSOR_CHAN_ROTATION);

    if (err)
    {
        LOG_ERR("Cannot fetch sensor sample (err %d)", err);
        return;
    }

    err = sensor_channel_get(qdec_dev, SENSOR_CHAN_ROTATION, &value);
    if (err)
    {
        LOG_ERR("Cannot get sensor value");
        return;
    }

    int rc;

    LOG_INF("Position = %d degrees\n", value.val1);
    if (value.val1 > 0)
    {
        LOG_INF("+\n");
        device_state = RUNNING;
        uint8_t brightness = get_brightness();
        if (brightness != increment_brightness())
        {
            // only set if the brightness changed
            set_segment_led();
        }
    }
    else if (value.val1 < 0)
    {
        LOG_INF("-\n");

        device_state = RUNNING;
        uint8_t brightness = get_brightness();
        if (brightness != decrement_brightness())
        {
            // only set if the brightness changed
            set_segment_led();
        }
    }
}

void qdec_setup()
{
    struct sensor_value val;
    int rc;

#ifdef QUAD_ENC
    static const struct sensor_trigger qdec_trig = {
        .type = SENSOR_TRIG_DATA_READY,
        .chan = SENSOR_CHAN_ROTATION,
    };
    // setup the callback for when the qdec is triggered
    int err = sensor_trigger_set(qdec_dev, (struct sensor_trigger *)&qdec_trig,
                                 data_ready_handler);
    if (err)
    {
        LOG_ERR("Cannot setup trigger");
    }
    return;
#endif
}
Should I be reading the sensor differently or configuring it to debounce? It seems that the double turns happen within under 100ms of each other.
Parents Reply Children
Related