30 :
EncoderBase{1 << utility::to_underlying(resolution)}, uart_{uart},
31 huart_{uart.get_hal_handle()}, de_{de}, resolution_{resolution},
32 mode_{mode}, address_{address} {}
37 if (!send_command(0x00,
reinterpret_cast<uint8_t *
>(&response))) {
41 int16_t count = response & (cpr - 1);
48 int16_t delta = count - prev_count_;
49 if (delta > (cpr / 2)) {
51 }
else if (delta < -(cpr / 2)) {
63 if (!send_extended_command(0x5E)) {
73 UART_HandleTypeDef *huart_;
78 int16_t prev_count_ = 0;
80 bool send_command(uint8_t command, uint8_t *response) {
81 uint8_t data = address_ | command;
84 if (HAL_UART_Transmit(huart_, &data, 1, 1) != HAL_OK) {
88 uart_.
receive(
reinterpret_cast<uint8_t *
>(response), 2, 1);
89 return checksum(response[0], response[1]);
92 bool send_extended_command(uint8_t command) {
93 std::array<uint8_t, 2> data{
static_cast<uint8_t
>(address_ | 0x02), command};
95 if (HAL_UART_Transmit(huart_, data.data(), data.size(), 1) != HAL_OK) {
102 bool checksum(uint8_t l, uint8_t h) {
103 bool k1 = !(bit(h, 5) ^ bit(h, 3) ^ bit(h, 1) ^ bit(l, 7) ^ bit(l, 5) ^
104 bit(l, 3) ^ bit(l, 1));
105 bool k0 = !(bit(h, 4) ^ bit(h, 2) ^ bit(h, 0) ^ bit(l, 6) ^ bit(l, 4) ^
106 bit(l, 2) ^ bit(l, 0));
107 return (k1 == bit(h, 7)) && (k0 == bit(h, 6));
110 bool bit(uint8_t x, uint8_t i) {
return ((x >> i) & 1) == 1; }
AMT21_SoftDE(peripheral::UART &uart, peripheral::GPIO &de, Resolution resolution, Mode mode, uint8_t address)
Definition amt21_soft_de.hpp:28