halx_driver
読み取り中…
検索中…
一致する文字列を見つけられません
amt21.hpp
[詳解]
1#pragma once
2
3#include <array>
4#include <cstdint>
5#include <optional>
6#include <utility>
7
8#include <halx/peripheral/uart.hpp>
9
10namespace halx::driver {
11
12enum class Amt21Resolution : uint8_t {
13 BIT_12 = 12,
14 BIT_14 = 14,
15};
16
18public:
19 Amt21Manager(peripheral::UartBase &uart) : uart_{uart} {}
20
21 std::optional<std::array<uint8_t, 2>> send_command(uint8_t address,
22 uint8_t command) {
23 uint8_t tx_buf = address | command;
24 std::array<uint8_t, 2> rx_buf;
25 uart_.flush();
26 if (!uart_.transmit(&tx_buf, 1, 5)) {
27 return std::nullopt;
28 }
29 if (!uart_.receive(rx_buf.data(), rx_buf.size(), 5)) {
30 return std::nullopt;
31 }
32 if (!test_checksum(rx_buf[0], rx_buf[1])) {
33 return std::nullopt;
34 }
35 return rx_buf;
36 }
37
38 bool send_extended_command(uint8_t address, uint8_t command) {
39 std::array<uint8_t, 2> buf{static_cast<uint8_t>(address | 0x02), command};
40 uart_.flush();
41 if (!uart_.transmit(buf.data(), buf.size(), 5)) {
42 return false;
43 }
44 return true;
45 }
46
47private:
48 peripheral::UartBase &uart_;
49
50 static inline bool test_checksum(uint8_t l, uint8_t h) {
51 bool k1 = !(bit(h, 5) ^ bit(h, 3) ^ bit(h, 1) ^ bit(l, 7) ^ bit(l, 5) ^
52 bit(l, 3) ^ bit(l, 1));
53 bool k0 = !(bit(h, 4) ^ bit(h, 2) ^ bit(h, 0) ^ bit(l, 6) ^ bit(l, 4) ^
54 bit(l, 2) ^ bit(l, 0));
55 return (k1 == bit(h, 7)) && (k0 == bit(h, 6));
56 }
57
58 static inline bool bit(uint8_t x, uint8_t i) { return ((x >> i) & 1) == 1; }
59};
60
61class Amt21 {
62public:
63 Amt21(Amt21Manager &manager, Amt21Resolution resolution, uint8_t address)
64 : manager_{manager}, resolution_{resolution}, address_{address} {}
65
66 std::optional<uint16_t> read_position() {
67 auto res = manager_.send_command(address_, 0x00);
68 if (!res) {
69 return std::nullopt;
70 }
71 return ((*res)[1] << 8 | (*res)[0]) &
72 ((1 << std::to_underlying(resolution_)) - 1);
73 }
74
75 std::optional<int16_t> read_turns() {
76 auto res = manager_.send_command(address_, 0x01);
77 if (!res) {
78 return std::nullopt;
79 }
80 int16_t turns = ((*res)[1] << 8 | (*res)[0]) & 0x3FFF;
81 if (turns & 0x2000) {
82 turns |= 0xC000;
83 }
84 return turns;
85 }
86
88 return manager_.send_extended_command(address_, 0x5E);
89 }
90
91 bool reset() { return manager_.send_extended_command(address_, 0x75); }
92
93private:
94 Amt21Manager &manager_;
95 Amt21Resolution resolution_;
96 uint8_t address_;
97};
98
99} // namespace halx::driver
Definition amt21.hpp:17
bool send_extended_command(uint8_t address, uint8_t command)
Definition amt21.hpp:38
Amt21Manager(peripheral::UartBase &uart)
Definition amt21.hpp:19
std::optional< std::array< uint8_t, 2 > > send_command(uint8_t address, uint8_t command)
Definition amt21.hpp:21
bool set_zero_point()
Definition amt21.hpp:87
std::optional< int16_t > read_turns()
Definition amt21.hpp:75
Amt21(Amt21Manager &manager, Amt21Resolution resolution, uint8_t address)
Definition amt21.hpp:63
std::optional< uint16_t > read_position()
Definition amt21.hpp:66
bool reset()
Definition amt21.hpp:91
Definition amt21.hpp:10
Amt21Resolution
Definition amt21.hpp:12
@ BIT_14
Definition amt21.hpp:14
@ BIT_12
Definition amt21.hpp:13