tutrcos
読み取り中…
検索中…
一致する文字列を見つけられません
amt22.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 AMT22 : public EncoderBase {
17public:
18 enum class Resolution : uint8_t {
19 _12 = 12,
20 _14 = 14,
21 };
22
23 enum class Mode {
26 };
27
29 Mode mode)
30 : EncoderBase{1 << utility::to_underlying(resolution)}, spi_{spi},
31 cs_{cs}, resolution_{resolution}, mode_{mode} {
32 cs_.write(true);
33 }
34
35 bool update() {
36 uint16_t cpr = 1 << utility::to_underlying(resolution_);
37 std::array<uint8_t, 2> command{0x00, 0x00};
38 uint16_t response;
39 if (!send_command(command.data(), reinterpret_cast<uint8_t *>(&response),
40 command.size())) {
41 return false;
42 }
43
44 int16_t count = response & (cpr - 1);
45 switch (mode_) {
46 case Mode::SINGLE_TURN: {
47 set_count(count);
48 break;
49 }
50 case Mode::MULTI_TURN: {
51 int16_t delta = count - prev_count_;
52 if (delta > (cpr / 2)) {
53 delta -= cpr;
54 } else if (delta < -(cpr / 2)) {
55 delta += cpr;
56 }
57 set_count(get_count() + delta);
58 prev_count_ = count;
59 break;
60 }
61 }
62 return true;
63 }
64
66 std::array<uint8_t, 2> command{0x00, 0x70};
67 std::array<uint8_t, 2> response{};
68 if (!send_command(command.data(), response.data(), command.size())) {
69 return false;
70 }
71 prev_count_ = 0;
72 set_count(0);
73 return true;
74 }
75
76private:
77 peripheral::SPI &spi_;
79 Resolution resolution_;
80 Mode mode_;
81 int16_t prev_count_ = 0;
82
83 bool send_command(const uint8_t *command, uint8_t *response, size_t size) {
84 cs_.write(false);
85 for (size_t i = 0; i < size; ++i) {
86 if (!spi_.transmit_receive(&command[i], &response[size - i - 1], 1, 1)) {
87 return false;
88 }
89 }
90 cs_.write(true);
91 for (size_t i = 0; i < size; i += 2) {
92 if (!checksum(response[i], response[i + 1])) {
93 return false;
94 }
95 }
96 return true;
97 }
98
99 bool checksum(uint8_t l, uint8_t h) {
100 bool k1 = !(bit(h, 5) ^ bit(h, 3) ^ bit(h, 1) ^ bit(l, 7) ^ bit(l, 5) ^
101 bit(l, 3) ^ bit(l, 1));
102 bool k0 = !(bit(h, 4) ^ bit(h, 2) ^ bit(h, 0) ^ bit(l, 6) ^ bit(l, 4) ^
103 bit(l, 2) ^ bit(l, 0));
104 return (k1 == bit(h, 7)) && (k0 == bit(h, 6));
105 }
106
107 bool bit(uint8_t x, uint8_t i) { return ((x >> i) & 1) == 1; }
108};
109
110} // namespace module
111} // namespace tutrcos
Definition amt22.hpp:16
Resolution
Definition amt22.hpp:18
bool set_zero_point()
Definition amt22.hpp:65
bool update()
Definition amt22.hpp:35
AMT22(peripheral::SPI &spi, peripheral::GPIO &cs, Resolution resolution, Mode mode)
Definition amt22.hpp:28
Mode
Definition amt22.hpp:23
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 spi.hpp:15
bool transmit_receive(const uint8_t *tx_data, uint8_t *rx_data, size_t size, uint32_t timeout)
Definition spi.hpp:53
constexpr std::underlying_type_t< T > to_underlying(T value) noexcept
Definition utility.hpp:23
Definition kernel.hpp:7