diff --git a/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h index 1e2430c4ba..2560997a97 100644 --- a/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h +++ b/ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h @@ -35,10 +35,10 @@ typedef struct { mp_obj_base_t base; uint8_t pin_a; uint8_t pin_b; - uint8_t eic_channel_a:4; - uint8_t eic_channel_b:4; - uint8_t state:4; // - int8_t quarter_count:4; // count intermediate transitions between detents + uint8_t eic_channel_a; + uint8_t eic_channel_b; + uint8_t state; // + int8_t quarter_count; // count intermediate transitions between detents mp_int_t position; } rotaryio_incrementalencoder_obj_t; diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c index 85030135bf..8b46c3bd79 100644 --- a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.c @@ -41,9 +41,9 @@ static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) { return; } - // 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 new_state = + ((uint8_t) nrf_gpio_pin_read(self->pin_a) << 1) | + (uint8_t) nrf_gpio_pin_read(self->pin_b); shared_module_softencoder_state_update(self, new_state); } diff --git a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h index 1f1d1a7640..3693131056 100644 --- a/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h +++ b/ports/nrf/common-hal/rotaryio/IncrementalEncoder.h @@ -35,8 +35,8 @@ typedef struct { mp_obj_base_t base; uint8_t pin_a; uint8_t pin_b; - int8_t quarter_count : 4; - uint8_t state : 4; + uint8_t state; // + int8_t quarter_count; // count intermediate transitions between detents mp_int_t position; } rotaryio_incrementalencoder_obj_t; diff --git a/ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.c b/ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.c index 10785c172c..045f111ca3 100644 --- a/ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.c +++ b/ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.c @@ -61,9 +61,12 @@ STATIC void incrementalencoder_interrupt_handler(void *self_in); void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t *self, const mcu_pin_obj_t *pin_a, const mcu_pin_obj_t *pin_b) { mp_obj_t pins[] = {MP_OBJ_FROM_PTR(pin_a), MP_OBJ_FROM_PTR(pin_b)}; + // Start out with swapped to match behavior with other ports. + self->swapped = true; if (!common_hal_rp2pio_pins_are_sequential(2, pins)) { pins[0] = MP_OBJ_FROM_PTR(pin_b); pins[1] = MP_OBJ_FROM_PTR(pin_a); + self->swapped = false; if (!common_hal_rp2pio_pins_are_sequential(2, pins)) { mp_raise_RuntimeError(translate("Pins must be sequential")); } @@ -115,6 +118,13 @@ STATIC void incrementalencoder_interrupt_handler(void *self_in) { // Bypass all the logic of StateMachine.c:_transfer, we need something // very simple and fast for an interrupt! uint8_t new_state = self->state_machine.pio->rxf[self->state_machine.state_machine]; + if (self->swapped) { + if (new_state == 0x1) { + new_state = 0x2; + } else if (new_state == 0x2) { + new_state = 0x1; + } + } shared_module_softencoder_state_update(self, new_state); } } diff --git a/ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.h b/ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.h index f794560275..6745d95a1c 100644 --- a/ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.h +++ b/ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.h @@ -34,7 +34,8 @@ typedef struct { mp_obj_base_t base; rp2pio_statemachine_obj_t state_machine; - uint8_t state : 4; // - int8_t quarter_count : 4; // count intermediate transitions between detents + uint8_t state; // + int8_t quarter_count; // count intermediate transitions between detents + bool swapped; // Did the pins need to be swapped to be sequential? mp_int_t position; } rotaryio_incrementalencoder_obj_t;