Skip to content

Commit c85b8ee

Browse files
committed
Merge branch 'quadrature_encoder' into callable
2 parents f10b7eb + 0dfc0be commit c85b8ee

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

util/quadrature_encoder.hh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
template<bool invert = false>
6+
class QuadratureEncoder {
7+
static constexpr uint8_t valid_ccw = 0b10000111 ^ (invert ? 0xff : 0x00);
8+
static constexpr uint8_t valid_cw = 0b01001011 ^ (invert ? 0xff : 0x00);
9+
10+
uint8_t prev_state{};
11+
12+
public:
13+
enum class State { Undefined, Right, Left };
14+
15+
State update(bool state_a, bool state_b) {
16+
const auto cur_state = state_a | (state_b << 1u);
17+
prev_state <<= 2;
18+
prev_state |= cur_state;
19+
switch (prev_state) {
20+
case valid_cw:
21+
return State::Right;
22+
case valid_ccw:
23+
return State::Left;
24+
default:
25+
return State::Undefined;
26+
}
27+
}
28+
};

0 commit comments

Comments
 (0)