M5UnitUnified 0.0.2 git rev:a353e8c
Loading...
Searching...
No Matches
adapter.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
12#ifndef M5_UNIT_COMPONENT_ADAPTER_HPP
13#define M5_UNIT_COMPONENT_ADAPTER_HPP
14
15#include <cstdint>
16#include <cstddef>
17#include <memory>
18#include <M5HAL.hpp>
19
20class TwoWire;
21
22namespace m5 {
23namespace unit {
28class Adapter {
29public:
32 explicit Adapter(const uint8_t addr);
33 Adapter(TwoWire& wire, const uint8_t addr);
34 Adapter(m5::hal::bus::Bus* bus, const uint8_t addr);
35 Adapter(m5::hal::bus::Bus& bus, const uint8_t addr) : Adapter(&bus, addr)
36 {
37 }
39 Adapter(const Adapter&) = delete;
40
41 Adapter(Adapter&&) noexcept = default;
42
43 Adapter& operator=(const Adapter&) = delete;
44
45 Adapter& operator=(Adapter&&) noexcept = default;
46
47 virtual ~Adapter() = default;
48
49 inline uint8_t address() const
50 {
51 return _impl->address();
52 }
53 inline uint32_t clock() const
54 {
55 return _impl->clock();
56 }
57 inline void setClock(const uint32_t clock)
58 {
59 return _impl->setClock(clock);
60 }
62 Adapter* duplicate(const uint8_t addr);
63
65 m5::hal::error::error_t generalCall(const uint8_t* data, const size_t len);
66
69
70 inline m5::hal::error::error_t readWithTransaction(uint8_t* data, const size_t len)
71 {
72 return _impl->readWithTransaction(data, len);
73 }
75 inline m5::hal::error::error_t writeWithTransaction(const uint8_t* data, const size_t len, const bool stop = true)
76 {
77 return _impl->writeWithTransaction(data, len, stop);
78 }
80 inline m5::hal::error::error_t writeWithTransaction(const uint8_t reg, const uint8_t* data, const size_t len,
81 const bool stop = true)
82 {
83 return _impl->writeWithTransaction(reg, data, len, stop);
84 }
86 inline m5::hal::error::error_t writeWithTransaction(const uint16_t reg, const uint8_t* data, const size_t len,
87 const bool stop = true)
88 {
89 return _impl->writeWithTransaction(reg, data, len, stop);
90 }
92
94 class Impl {
95 public:
96 Impl() = default;
97 explicit Impl(const uint8_t addr) : _addr(addr)
98 {
99 }
100 Impl(const uint8_t addr, const uint32_t clock) : _addr(addr), _clock(clock)
101 {
102 }
103
104 virtual ~Impl() = default;
105
106 inline uint8_t address() const
107 {
108 return _addr;
109 }
110 inline uint32_t clock() const
111 {
112 return _clock;
113 }
114 inline void setClock(const uint32_t clock)
115 {
116 _clock = clock;
117 }
118
119 virtual Impl* duplicate(const uint8_t addr)
120 {
121 return new Impl(addr, _clock);
122 }
123
124 virtual m5::hal::error::error_t readWithTransaction(uint8_t*, const size_t)
125 {
126 return m5::hal::error::error_t::UNKNOWN_ERROR;
127 }
128 virtual m5::hal::error::error_t writeWithTransaction(const uint8_t*, const size_t, const bool)
129 {
130 return m5::hal::error::error_t::UNKNOWN_ERROR;
131 }
132 virtual m5::hal::error::error_t writeWithTransaction(const uint8_t, const uint8_t*, const size_t, const bool)
133 {
134 return m5::hal::error::error_t::UNKNOWN_ERROR;
135 }
136 virtual m5::hal::error::error_t writeWithTransaction(const uint16_t, const uint8_t*, const size_t, const bool)
137 {
138 return m5::hal::error::error_t::UNKNOWN_ERROR;
139 }
140
141 virtual m5::hal::error::error_t generalCall(const uint8_t*, const size_t)
142 {
143 return m5::hal::error::error_t::UNKNOWN_ERROR;
144 }
145
146 protected:
147 uint8_t _addr{};
148 uint32_t _clock{100000};
149 };
151
152protected:
153 std::unique_ptr<Impl> _impl{};
154 // Adapter* _parent{};
155};
156
157} // namespace unit
158} // namespace m5
159#endif
Adapters to treat M5HAL and TwoWire in the same way.
Definition adapter.hpp:28
m5::hal::error::error_t writeWithTransaction(const uint8_t reg, const uint8_t *data, const size_t len, const bool stop=true)
Writeing data with transactions (reg8)*‍/.
Definition adapter.hpp:80
m5::hal::error::error_t writeWithTransaction(const uint8_t *data, const size_t len, const bool stop=true)
Writeing data with transactions *‍/.
Definition adapter.hpp:75
Adapter * duplicate(const uint8_t addr)
Dupicate adapter.
Definition adapter.cpp:300
m5::hal::error::error_t readWithTransaction(uint8_t *data, const size_t len)
Reading data with transactions.
Definition adapter.hpp:70
m5::hal::error::error_t generalCall(const uint8_t *data, const size_t len)
write to address zero (general call)
Definition adapter.cpp:314
m5::hal::error::error_t writeWithTransaction(const uint16_t reg, const uint8_t *data, const size_t len, const bool stop=true)
Writeing data with transactions (reg16) *‍/.
Definition adapter.hpp:86
Top level namespace of M5stack.
Definition test_helper.hpp:18
Unit-related namespace.