Skip to content

Commit a7c349b

Browse files
committed
Add quarter-click logic to #1045
1 parent 95454ec commit a7c349b

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,21 @@ static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
4343
new_state = (new_state << 1) + (new_state ^ nrf_gpio_pin_read(self->pin_b));
4444

4545
uint8_t change = (new_state - self->state) & 0x03;
46-
if (change == 1) self->position++;
47-
else if (change == 3) self->position--;
46+
if (change == 1) self->quarter++;
47+
else if (change == 3) self->quarter--;
4848
// ignore other state transitions
4949

5050
self->state = new_state;
51+
52+
// logic from the atmel-samd port: provides some damping and scales movement
53+
// down by 4:1.
54+
if (self->quarter >= 4) {
55+
self->position++;
56+
self->quarter = 0;
57+
} else if (self->quarter <= -4) {
58+
self->position--;
59+
self->quarter = 0;
60+
}
5161
}
5262

5363
void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ typedef struct {
3636
uint8_t pin_a;
3737
uint8_t pin_b;
3838
uint8_t state;
39+
int8_t quarter;
3940
mp_int_t position;
4041
} rotaryio_incrementalencoder_obj_t;
4142

0 commit comments

Comments
 (0)