Skip to content

Commit 61e33a5

Browse files
committed
fix nrf ISR; make direction consistent across ports; save code size
1 parent 280aeff commit 61e33a5

File tree

5 files changed

+22
-11
lines changed

5 files changed

+22
-11
lines changed

ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ typedef struct {
3535
mp_obj_base_t base;
3636
uint8_t pin_a;
3737
uint8_t pin_b;
38-
uint8_t eic_channel_a:4;
39-
uint8_t eic_channel_b:4;
40-
uint8_t state:4; // <old A><old B>
41-
int8_t quarter_count:4; // count intermediate transitions between detents
38+
uint8_t eic_channel_a;
39+
uint8_t eic_channel_b;
40+
uint8_t state; // <old A><old B>
41+
int8_t quarter_count; // count intermediate transitions between detents
4242
mp_int_t position;
4343
} rotaryio_incrementalencoder_obj_t;
4444

ports/nrf/common-hal/rotaryio/IncrementalEncoder.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
4141
return;
4242
}
4343

44-
// reads a state 0 .. 3 *in order*.
45-
uint8_t new_state = nrf_gpio_pin_read(self->pin_a);
46-
new_state = (new_state << 1) + (new_state ^ nrf_gpio_pin_read(self->pin_b));
44+
uint8_t new_state =
45+
((uint8_t) nrf_gpio_pin_read(self->pin_a) << 1) |
46+
(uint8_t) nrf_gpio_pin_read(self->pin_b);
4747

4848
shared_module_softencoder_state_update(self, new_state);
4949
}

ports/nrf/common-hal/rotaryio/IncrementalEncoder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ typedef struct {
3535
mp_obj_base_t base;
3636
uint8_t pin_a;
3737
uint8_t pin_b;
38-
int8_t quarter_count : 4;
39-
uint8_t state : 4;
38+
uint8_t state; // <old A><old B>
39+
int8_t quarter_count; // count intermediate transitions between detents
4040
mp_int_t position;
4141
} rotaryio_incrementalencoder_obj_t;
4242

ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,12 @@ STATIC void incrementalencoder_interrupt_handler(void *self_in);
6161
void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t *self,
6262
const mcu_pin_obj_t *pin_a, const mcu_pin_obj_t *pin_b) {
6363
mp_obj_t pins[] = {MP_OBJ_FROM_PTR(pin_a), MP_OBJ_FROM_PTR(pin_b)};
64+
// Start out with swapped to match behavior with other ports.
65+
self->swapped = true;
6466
if (!common_hal_rp2pio_pins_are_sequential(2, pins)) {
6567
pins[0] = MP_OBJ_FROM_PTR(pin_b);
6668
pins[1] = MP_OBJ_FROM_PTR(pin_a);
69+
self->swapped = false;
6770
if (!common_hal_rp2pio_pins_are_sequential(2, pins)) {
6871
mp_raise_RuntimeError(translate("Pins must be sequential"));
6972
}
@@ -115,6 +118,13 @@ STATIC void incrementalencoder_interrupt_handler(void *self_in) {
115118
// Bypass all the logic of StateMachine.c:_transfer, we need something
116119
// very simple and fast for an interrupt!
117120
uint8_t new_state = self->state_machine.pio->rxf[self->state_machine.state_machine];
121+
if (self->swapped) {
122+
if (new_state == 0x1) {
123+
new_state = 0x2;
124+
} else if (new_state == 0x2) {
125+
new_state = 0x1;
126+
}
127+
}
118128
shared_module_softencoder_state_update(self, new_state);
119129
}
120130
}

ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
typedef struct {
3535
mp_obj_base_t base;
3636
rp2pio_statemachine_obj_t state_machine;
37-
uint8_t state : 4; // <old A><old B><new A><new B>
38-
int8_t quarter_count : 4; // count intermediate transitions between detents
37+
uint8_t state; // <old A><old B>
38+
int8_t quarter_count; // count intermediate transitions between detents
39+
bool swapped; // Did the pins need to be swapped to be sequential?
3940
mp_int_t position;
4041
} rotaryio_incrementalencoder_obj_t;

0 commit comments

Comments
 (0)