We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents f10b7eb + 0dfc0be commit c85b8eeCopy full SHA for c85b8ee
util/quadrature_encoder.hh
@@ -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