|
25 | 25 | */
|
26 | 26 |
|
27 | 27 | #include "common-hal/rotaryio/IncrementalEncoder.h"
|
| 28 | +#include "shared-module/rotaryio/IncrementalEncoder.h" |
28 | 29 |
|
29 | 30 | #include "atmel_start_pins.h"
|
30 | 31 |
|
@@ -68,11 +69,9 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
|
68 | 69 | self->position = 0;
|
69 | 70 | self->quarter_count = 0;
|
70 | 71 |
|
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, |
74 | 73 | ((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)); |
76 | 75 |
|
77 | 76 | claim_pin(pin_a);
|
78 | 77 | claim_pin(pin_b);
|
@@ -106,66 +105,12 @@ void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_o
|
106 | 105 | self->pin_b = NO_PIN;
|
107 | 106 | }
|
108 | 107 |
|
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 |
| - |
118 | 108 | void incrementalencoder_interrupt_handler(uint8_t channel) {
|
119 | 109 | rotaryio_incrementalencoder_obj_t* self = get_eic_channel_data(channel);
|
120 | 110 |
|
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 = |
154 | 112 | ((uint8_t) gpio_get_pin_level(self->pin_a) << 1) |
|
155 | 113 | (uint8_t) gpio_get_pin_level(self->pin_b);
|
156 | 114 |
|
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); |
171 | 116 | }
|
0 commit comments