From 95454ecde0af2772b9bb009acac444bfa682088d Mon Sep 17 00:00:00 2001 From: Nick Moore Date: Tue, 5 Feb 2019 15:50:35 +1100 Subject: [PATCH] useful output from rotaryio adafruit/circuitpython#1045 --- .../common-hal/rotaryio/IncrementalEncoder.c | 24 ++++++++++++++----- .../common-hal/rotaryio/IncrementalEncoder.h | 1 + 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c index b55ce75d6b..a4cf60c878 100644 --- a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c @@ -29,14 +29,25 @@ #include "py/runtime.h" -// obj array to map pin -> self since nrfx hide the mapping +#include + +// obj array to map pin number -> self since nrfx hide the mapping static rotaryio_incrementalencoder_obj_t *_objs[NUMBER_OF_PINS]; static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { rotaryio_incrementalencoder_obj_t *self = _objs[pin]; if (!self) return; - self->position++; + // reads a state 0 .. 3 *in order*. + uint8_t new_state = nrf_gpio_pin_read(self->pin_a); + new_state = (new_state << 1) + (new_state ^ nrf_gpio_pin_read(self->pin_b)); + + uint8_t change = (new_state - self->state) & 0x03; + if (change == 1) self->position++; + else if (change == 3) self->position--; + // ignore other state transitions + + self->state = new_state; } void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self, @@ -75,9 +86,13 @@ void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_o _objs[self->pin_a] = NULL; _objs[self->pin_b] = NULL; + nrfx_gpiote_in_event_disable(self->pin_a); + nrfx_gpiote_in_event_disable(self->pin_b); + nrfx_gpiote_in_uninit(self->pin_a); + nrfx_gpiote_in_uninit(self->pin_b); reset_pin_number(self->pin_a); - self->pin_a = NO_PIN; reset_pin_number(self->pin_b); + self->pin_a = NO_PIN; self->pin_b = NO_PIN; } @@ -89,6 +104,3 @@ void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalenc mp_int_t new_position) { self->position = new_position; } - -void incrementalencoder_interrupt_handler(uint8_t channel) { -} diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h index 73723bc0c4..eba857ac77 100644 --- a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h @@ -35,6 +35,7 @@ typedef struct { mp_obj_base_t base; uint8_t pin_a; uint8_t pin_b; + uint8_t state; mp_int_t position; } rotaryio_incrementalencoder_obj_t;