Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3875 proposed fix for dead spot when reversing #3967

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions ports/atmel-samd/common-hal/rotaryio/IncrementalEncoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,13 @@ void incrementalencoder_interrupt_handler(uint8_t channel) {
}

self->quarter_count += quarter_incr;
if (self->quarter_count >= 4) {
self->position += 1;
self->quarter_count = 0;
} else if (self->quarter_count <= -4) {
self->position -= 1;
self->quarter_count = 0;
if (self->last_state == 2 && self->quarter_count != 0)
if (self->quarter_count > 0) {
self->position += 1;
self->quarter_count = 0;
} else {
self->position -= 1;
self->quarter_count = 0;
}
}
}
25 changes: 17 additions & 8 deletions ports/nrf/common-hal/rotaryio/IncrementalEncoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
rotaryio_incrementalencoder_obj_t *self = _objs[pin];
if (!self) return;

// reads a state 0 .. 3 *in order*.
// pins a and b (in truth table) are in Gray code order
// a | b | a<<1 | a^b | a<<1 + a^b (decimal Gray code)
// --+---+------+-----+-----------
// 0 | 0 | 0 | 0 | 0
// 0 | 1 | 0 | 1 | 1
// 1 | 1 | 2 | 0 | 2
// 1 | 0 | 2 | 1 | 3
// new_state is a decimal Gray code 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));

Expand All @@ -51,15 +58,17 @@ static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {

// logic from the atmel-samd port: provides some damping and scales movement
// down by 4:1.
if (self->quarter >= 4) {
self->position++;
self->quarter = 0;
} else if (self->quarter <= -4) {
self->position--;
self->quarter = 0;
if (new_state == 2 && self->quarter != 0) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if a different encoder (with detent logic levels of pin_a=0 and pin_b=0) needs to be supported, this line of code would effectively need to check new_state == detent_decimal_gray_code where detent_decimal_gray_code = 0. not a hard coded '2'

for now this PR is not accommodating configuration of a detent_decimal_gray_code 😊

if (self->quarter > 0) {
self->position++;
self->quarter = 0;
}
else {
self->position--;
self->quarter = 0;
}
}
}

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) {

Expand Down