Skip to content

Commit 3aec103

Browse files
committed
atmel-samd: switch to shared softencoder implementation
1 parent 3ce0b51 commit 3aec103

File tree

3 files changed

+8
-61
lines changed

3 files changed

+8
-61
lines changed

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

Lines changed: 5 additions & 60 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

2930
#include "atmel_start_pins.h"
3031

@@ -68,11 +69,9 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
6869
self->position = 0;
6970
self->quarter_count = 0;
7071

71-
// Top two bits of self->last_state don't matter, because they'll be gone as soon as
72-
// interrupt handler is called.
73-
self->last_state =
72+
shared_module_softencoder_state_init(self,
7473
((uint8_t) gpio_get_pin_level(self->pin_a) << 1) |
75-
(uint8_t) gpio_get_pin_level(self->pin_b);
74+
(uint8_t) gpio_get_pin_level(self->pin_b));
7675

7776
claim_pin(pin_a);
7877
claim_pin(pin_b);
@@ -106,66 +105,12 @@ void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_o
106105
self->pin_b = NO_PIN;
107106
}
108107

109-
mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementalencoder_obj_t* self) {
110-
return self->position;
111-
}
112-
113-
void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalencoder_obj_t* self,
114-
mp_int_t new_position) {
115-
self->position = new_position;
116-
}
117-
118108
void incrementalencoder_interrupt_handler(uint8_t channel) {
119109
rotaryio_incrementalencoder_obj_t* self = get_eic_channel_data(channel);
120110

121-
// This table also works for detent both at 11 and 00
122-
// For 11 at detent:
123-
// Turning cw: 11->01->00->10->11
124-
// Turning ccw: 11->10->00->01->11
125-
// For 00 at detent:
126-
// Turning cw: 00->10->11->10->00
127-
// Turning ccw: 00->01->11->10->00
128-
129-
// index table by state <oldA><oldB><newA><newB>
130-
#define BAD 7
131-
static const int8_t transitions[16] = {
132-
0, // 00 -> 00 no movement
133-
-1, // 00 -> 01 3/4 ccw (11 detent) or 1/4 ccw (00 at detent)
134-
+1, // 00 -> 10 3/4 cw or 1/4 cw
135-
BAD, // 00 -> 11 non-Gray-code transition
136-
+1, // 01 -> 00 2/4 or 4/4 cw
137-
0, // 01 -> 01 no movement
138-
BAD, // 01 -> 10 non-Gray-code transition
139-
-1, // 01 -> 11 4/4 or 2/4 ccw
140-
-1, // 10 -> 00 2/4 or 4/4 ccw
141-
BAD, // 10 -> 01 non-Gray-code transition
142-
0, // 10 -> 10 no movement
143-
+1, // 10 -> 11 4/4 or 2/4 cw
144-
BAD, // 11 -> 00 non-Gray-code transition
145-
+1, // 11 -> 01 1/4 or 3/4 cw
146-
-1, // 11 -> 10 1/4 or 3/4 ccw
147-
0, // 11 -> 11 no movement
148-
};
149-
150-
// Shift the old AB bits to the "old" position, and set the new AB bits.
151-
// TODO(tannewt): If we need more speed then read the pin directly. gpio_get_pin_level has
152-
// smarts to compensate for pin direction we don't need.
153-
self->last_state = (self->last_state & 0x3) << 2 |
111+
uint8_t new_state =
154112
((uint8_t) gpio_get_pin_level(self->pin_a) << 1) |
155113
(uint8_t) gpio_get_pin_level(self->pin_b);
156114

157-
int8_t quarter_incr = transitions[self->last_state];
158-
if (quarter_incr == BAD) {
159-
// Missed a transition. We don't know which way we're going, so do nothing.
160-
return;
161-
}
162-
163-
self->quarter_count += quarter_incr;
164-
if (self->quarter_count >= 4) {
165-
self->position += 1;
166-
self->quarter_count = 0;
167-
} else if (self->quarter_count <= -4) {
168-
self->position -= 1;
169-
self->quarter_count = 0;
170-
}
115+
shared_module_softencoder_state_update(self, new_state);
171116
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ typedef struct {
3737
uint8_t pin_b;
3838
uint8_t eic_channel_a:4;
3939
uint8_t eic_channel_b:4;
40-
uint8_t last_state:4; // <old A><old B><new A><new B>
40+
uint8_t state:4; // <old A><old B>
4141
int8_t quarter_count:4; // count intermediate transitions between detents
4242
mp_int_t position;
4343
} rotaryio_incrementalencoder_obj_t;

ports/atmel-samd/mpconfigport.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ USB_SERIAL_NUMBER_LENGTH = 32
2525
# Number of USB endpoint pairs.
2626
USB_NUM_EP = 8
2727

28+
CIRCUITPY_ROTARYIO_SOFTENCODER = 1
29+
2830
######################################################################
2931
# Put samd21-only choices here.
3032

0 commit comments

Comments
 (0)