30 :
EncoderBase{1 << utility::to_underlying(resolution)}, spi_{spi},
31 cs_{cs}, resolution_{resolution}, mode_{mode} {
37 std::array<uint8_t, 2> command{0x00, 0x00};
39 if (!send_command(command.data(),
reinterpret_cast<uint8_t *
>(&response),
44 int16_t count = response & (cpr - 1);
51 int16_t delta = count - prev_count_;
52 if (delta > (cpr / 2)) {
54 }
else if (delta < -(cpr / 2)) {
66 std::array<uint8_t, 2> command{0x00, 0x70};
67 std::array<uint8_t, 2> response{};
68 if (!send_command(command.data(), response.data(), command.size())) {
81 int16_t prev_count_ = 0;
83 bool send_command(
const uint8_t *command, uint8_t *response,
size_t size) {
85 for (
size_t i = 0; i < size; ++i) {
91 for (
size_t i = 0; i < size; i += 2) {
92 if (!checksum(response[i], response[i + 1])) {
99 bool checksum(uint8_t l, uint8_t h) {
100 bool k1 = !(bit(h, 5) ^ bit(h, 3) ^ bit(h, 1) ^ bit(l, 7) ^ bit(l, 5) ^
101 bit(l, 3) ^ bit(l, 1));
102 bool k0 = !(bit(h, 4) ^ bit(h, 2) ^ bit(h, 0) ^ bit(l, 6) ^ bit(l, 4) ^
103 bit(l, 2) ^ bit(l, 0));
104 return (k1 == bit(h, 7)) && (k0 == bit(h, 6));
107 bool bit(uint8_t x, uint8_t i) {
return ((x >> i) & 1) == 1; }