Skip to content

Commit d69ca4a

Browse files
committed
nrf: switch to shared softencoder implementation
1 parent 3aec103 commit d69ca4a

File tree

3 files changed

+5
-30
lines changed

3 files changed

+5
-30
lines changed

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

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "common-hal/rotaryio/IncrementalEncoder.h"
28+
#include "shared-module/rotaryio/IncrementalEncoder.h"
2829
#include "nrfx_gpiote.h"
2930

3031
#include "py/runtime.h"
@@ -44,25 +45,7 @@ static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
4445
uint8_t new_state = nrf_gpio_pin_read(self->pin_a);
4546
new_state = (new_state << 1) + (new_state ^ nrf_gpio_pin_read(self->pin_b));
4647

47-
uint8_t change = (new_state - self->state) & 0x03;
48-
if (change == 1) {
49-
self->quarter++;
50-
} else if (change == 3) {
51-
self->quarter--;
52-
}
53-
// ignore other state transitions
54-
55-
self->state = new_state;
56-
57-
// logic from the atmel-samd port: provides some damping and scales movement
58-
// down by 4:1.
59-
if (self->quarter >= 4) {
60-
self->position++;
61-
self->quarter = 0;
62-
} else if (self->quarter <= -4) {
63-
self->position--;
64-
self->quarter = 0;
65-
}
48+
shared_module_softencoder_state_update(self, new_state);
6649
}
6750

6851
void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t *self,
@@ -110,12 +93,3 @@ void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_o
11093
self->pin_a = NO_PIN;
11194
self->pin_b = NO_PIN;
11295
}
113-
114-
mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementalencoder_obj_t *self) {
115-
return self->position;
116-
}
117-
118-
void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalencoder_obj_t *self,
119-
mp_int_t new_position) {
120-
self->position = new_position;
121-
}

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-
uint8_t state;
39-
int8_t quarter;
38+
int8_t quarter_count : 4;
39+
uint8_t state : 4;
4040
mp_int_t position;
4141
} rotaryio_incrementalencoder_obj_t;
4242

ports/nrf/mpconfigport.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ CIRCUITPY_RTC ?= 1
4040
CIRCUITPY_FREQUENCYIO = 0
4141

4242
CIRCUITPY_RGBMATRIX ?= 1
43+
CIRCUITPY_ROTARYIO_SOFTENCODER = 1
4344
CIRCUITPY_FRAMEBUFFERIO ?= 1
4445

4546
CIRCUITPY_COUNTIO = 0

0 commit comments

Comments
 (0)