29 :
EncoderBase{1 << utility::to_underlying(resolution)}, uart_{uart},
30 resolution_{resolution}, mode_{mode}, address_{address} {}
35 if (!send_command(0x00,
reinterpret_cast<uint8_t *
>(&response))) {
39 int16_t count = response & (cpr - 1);
46 int16_t delta = count - prev_count_;
47 if (delta > (cpr / 2)) {
49 }
else if (delta < -(cpr / 2)) {
61 if (!send_extended_command(0x5E)) {
74 int16_t prev_count_ = 0;
76 bool send_command(uint8_t command, uint8_t *response) {
77 uint8_t data = address_ | command;
82 uart_.
receive(
reinterpret_cast<uint8_t *
>(response), 2, 1);
83 return checksum(response[0], response[1]);
86 bool send_extended_command(uint8_t command) {
87 std::array<uint8_t, 2> data{
static_cast<uint8_t
>(address_ | 0x02), command};
88 if (!uart_.
transmit(data.data(), data.size(), 1)) {
94 bool checksum(uint8_t l, uint8_t h) {
95 bool k1 = !(bit(h, 5) ^ bit(h, 3) ^ bit(h, 1) ^ bit(l, 7) ^ bit(l, 5) ^
96 bit(l, 3) ^ bit(l, 1));
97 bool k0 = !(bit(h, 4) ^ bit(h, 2) ^ bit(h, 0) ^ bit(l, 6) ^ bit(l, 4) ^
98 bit(l, 2) ^ bit(l, 0));
99 return (k1 == bit(h, 7)) && (k0 == bit(h, 6));
102 bool bit(uint8_t x, uint8_t i) {
return ((x >> i) & 1) == 1; }