tutrcos
読み取り中…
検索中…
一致する文字列を見つけられません
amt21_soft_de.hpp
[詳解]
1#pragma once
2
3#include "main.h"
4
5#include <array>
6#include <cstdint>
7
10
11#include "encoder_base.hpp"
12
13namespace tutrcos {
14namespace module {
15
16class AMT21_SoftDE : public EncoderBase {
17public:
18 enum class Resolution : uint8_t {
19 _12 = 12,
20 _14 = 14,
21 };
22
23 enum class Mode {
26 };
27
29 Resolution resolution, Mode mode, uint8_t address)
30 : EncoderBase{1 << utility::to_underlying(resolution)}, uart_{uart},
31 huart_{uart.get_hal_handle()}, de_{de}, resolution_{resolution},
32 mode_{mode}, address_{address} {}
33
34 bool update() {
35 uint16_t cpr = 1 << utility::to_underlying(resolution_);
36 uint16_t response;
37 if (!send_command(0x00, reinterpret_cast<uint8_t *>(&response))) {
38 return false;
39 }
40
41 int16_t count = response & (cpr - 1);
42 switch (mode_) {
43 case Mode::SINGLE_TURN: {
44 set_count(count);
45 break;
46 }
47 case Mode::MULTI_TURN: {
48 int16_t delta = count - prev_count_;
49 if (delta > (cpr / 2)) {
50 delta -= cpr;
51 } else if (delta < -(cpr / 2)) {
52 delta += cpr;
53 }
54 set_count(get_count() + delta);
55 prev_count_ = count;
56 break;
57 }
58 }
59 return true;
60 }
61
63 if (!send_extended_command(0x5E)) {
64 return false;
65 }
66 prev_count_ = 0;
67 set_count(0);
68 return true;
69 }
70
71private:
72 peripheral::UART &uart_;
73 UART_HandleTypeDef *huart_;
75 Resolution resolution_;
76 Mode mode_;
77 uint8_t address_;
78 int16_t prev_count_ = 0;
79
80 bool send_command(uint8_t command, uint8_t *response) {
81 uint8_t data = address_ | command;
82 uart_.flush();
83 de_.write(true);
84 if (HAL_UART_Transmit(huart_, &data, 1, 1) != HAL_OK) {
85 return false;
86 }
87 de_.write(false);
88 uart_.receive(reinterpret_cast<uint8_t *>(response), 2, 1);
89 return checksum(response[0], response[1]);
90 }
91
92 bool send_extended_command(uint8_t command) {
93 std::array<uint8_t, 2> data{static_cast<uint8_t>(address_ | 0x02), command};
94 de_.write(true);
95 if (HAL_UART_Transmit(huart_, data.data(), data.size(), 1) != HAL_OK) {
96 return false;
97 }
98 de_.write(false);
99 return true;
100 }
101
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));
108 }
109
110 bool bit(uint8_t x, uint8_t i) { return ((x >> i) & 1) == 1; }
111};
112
113} // namespace module
114} // namespace tutrcos
Definition amt21_soft_de.hpp:16
Resolution
Definition amt21_soft_de.hpp:18
bool update()
Definition amt21_soft_de.hpp:34
Mode
Definition amt21_soft_de.hpp:23
AMT21_SoftDE(peripheral::UART &uart, peripheral::GPIO &de, Resolution resolution, Mode mode, uint8_t address)
Definition amt21_soft_de.hpp:28
bool set_zero_point()
Definition amt21_soft_de.hpp:62
Definition encoder_base.hpp:10
void set_count(int64_t count)
Definition encoder_base.hpp:28
int64_t get_count()
Definition encoder_base.hpp:16
Definition gpio.hpp:39
void write(bool state)
Definition gpio.hpp:54
Definition uart.hpp:51
bool receive(uint8_t *data, size_t size, uint32_t timeout)
Definition uart.hpp:85
void flush()
Definition uart.hpp:101
constexpr std::underlying_type_t< T > to_underlying(T value) noexcept
Definition utility.hpp:23
Definition kernel.hpp:7