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