M5UnitUnified 0.0.5 git rev:3b942a7
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 */
11#ifndef M5_UNIT_COMPONENT_ADAPTER_HPP
12#define M5_UNIT_COMPONENT_ADAPTER_HPP
13
14#include <cstdint>
15#include <cstddef>
16#include <memory>
17#include <M5HAL.hpp>
18#include "pin.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
54 inline uint32_t clock() const
55 {
56 return _impl->clock();
57 }
58 inline void setClock(const uint32_t clock)
59 {
60 return _impl->setClock(clock);
61 }
62
63 int16_t scl() const
64 {
65 return _impl->scl();
66 }
67 int16_t sda() const
68 {
69 return _impl->sda();
70 }
71
73 Adapter* duplicate(const uint8_t addr);
74
76 m5::hal::error::error_t generalCall(const uint8_t* data, const size_t len);
77
80
81 inline m5::hal::error::error_t readWithTransaction(uint8_t* data, const size_t len)
82 {
83 return _impl->readWithTransaction(data, len);
84 }
86 inline m5::hal::error::error_t writeWithTransaction(const uint8_t* data, const size_t len, const bool stop = true)
87 {
88 return _impl->writeWithTransaction(data, len, stop);
89 }
91 inline m5::hal::error::error_t writeWithTransaction(const uint8_t reg, const uint8_t* data, const size_t len,
92 const bool stop = true)
93 {
94 return _impl->writeWithTransaction(reg, data, len, stop);
95 }
97 inline m5::hal::error::error_t writeWithTransaction(const uint16_t reg, const uint8_t* data, const size_t len,
98 const bool stop = true)
99 {
100 return _impl->writeWithTransaction(reg, data, len, stop);
101 }
103
108 bool begin()
109 {
110 return _impl->begin();
111 }
112 bool end()
113 {
114 return _impl->end();
115 }
116 void pinMode(uint8_t pin, uint8_t mode)
117 {
118 _impl->pinMode(pin, mode);
119 }
120 void digitalWrite(uint8_t pin, uint8_t val)
121 {
122 _impl->digitalWrite(pin, val);
123 }
124 bool pushPin();
125 bool popPin();
127
129 class Impl {
130 public:
131 Impl() = default;
132 explicit Impl(const uint8_t addr) : _addr(addr)
133 {
134 }
135 Impl(const uint8_t addr, const uint32_t clock) : _addr(addr), _clock(clock)
136 {
137 }
138
139 virtual ~Impl() = default;
140
141 inline uint8_t address() const
142 {
143 return _addr;
144 }
145 inline uint32_t clock() const
146 {
147 return _clock;
148 }
149 inline void setClock(const uint32_t clock)
150 {
151 _clock = clock;
152 }
153 virtual int16_t scl() const
154 {
155 return -1;
156 }
157 virtual int16_t sda() const
158 {
159 return -1;
160 }
161
162 virtual bool begin()
163 {
164 return false;
165 }
166 virtual bool end()
167 {
168 return false;
169 }
170 virtual void pinMode(uint8_t pin, uint8_t mode)
171 {
172 }
173 virtual void digitalWrite(uint8_t pin, uint8_t val)
174 {
175 }
176
177 virtual Impl* duplicate(const uint8_t addr)
178 {
179 return new Impl(addr, _clock);
180 }
181
182 virtual m5::hal::error::error_t readWithTransaction(uint8_t*, const size_t)
183 {
184 return m5::hal::error::error_t::UNKNOWN_ERROR;
185 }
186 virtual m5::hal::error::error_t writeWithTransaction(const uint8_t*, const size_t, const bool)
187 {
188 return m5::hal::error::error_t::UNKNOWN_ERROR;
189 }
190 virtual m5::hal::error::error_t writeWithTransaction(const uint8_t, const uint8_t*, const size_t, const bool)
191 {
192 return m5::hal::error::error_t::UNKNOWN_ERROR;
193 }
194 virtual m5::hal::error::error_t writeWithTransaction(const uint16_t, const uint8_t*, const size_t, const bool)
195 {
196 return m5::hal::error::error_t::UNKNOWN_ERROR;
197 }
198
199 virtual m5::hal::error::error_t generalCall(const uint8_t*, const size_t)
200 {
201 return m5::hal::error::error_t::UNKNOWN_ERROR;
202 }
203
204 protected:
205 uint8_t _addr{};
206 uint32_t _clock{100000};
207 };
209
210protected:
211 std::unique_ptr<Impl> _impl{};
212 gpio::pin_backup_t _backupSCL{-1}, _backupSDA{-1};
213
214 // Adapter* _parent{};
215};
216
217} // namespace unit
218} // namespace m5
219#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:91
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:86
Adapter * duplicate(const uint8_t addr)
Dupicate adapter.
Definition adapter.cpp:363
m5::hal::error::error_t readWithTransaction(uint8_t *data, const size_t len)
Reading data with transactions.
Definition adapter.hpp:81
m5::hal::error::error_t generalCall(const uint8_t *data, const size_t len)
write to address zero (general call)
Definition adapter.cpp:377
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:97
Top level namespace of M5stack.
Definition test_helper.hpp:18
Unit-related namespace.
PIN settings save/restore.