Skip to content

Commit 3566d1c

Browse files
committed
Refactor to check for changed pins, and return enum with signed int values.
1 parent c85b8ee commit 3566d1c

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

util/quadrature_encoder.hh

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@ class QuadratureEncoder {
1010
uint8_t prev_state{};
1111

1212
public:
13-
enum class State { Undefined, Right, Left };
13+
enum Direction { None = 0, CW = 1, CCW = -1 };
1414

15-
State update(bool state_a, bool state_b) {
15+
Direction get_motion(bool state_a, bool state_b) {
1616
const auto cur_state = state_a | (state_b << 1u);
17+
18+
if ((prev_state & 0b11) == cur_state)
19+
return None;
20+
1721
prev_state <<= 2;
1822
prev_state |= cur_state;
23+
1924
switch (prev_state) {
2025
case valid_cw:
21-
return State::Right;
26+
return CW;
2227
case valid_ccw:
23-
return State::Left;
28+
return CCW;
2429
default:
25-
return State::Undefined;
30+
return None;
2631
}
2732
}
2833
};

0 commit comments

Comments
 (0)