M5UnitUnified 0.0.2 git rev:a353e8c
Loading...
Searching...
No Matches
M5UnitComponent.hpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024 M5Stack Technology CO LTD
3 *
4 * SPDX-License-Identifier: MIT
5 */
10#ifndef M5_UNIT_COMPONENT_HPP
11#define M5_UNIT_COMPONENT_HPP
12
15#include <cstdint>
16#include <vector>
17#include <algorithm>
18#include <iterator>
19#include <type_traits>
20#include <memory>
21
22class TwoWire;
23
24namespace m5 {
25namespace unit {
26class UnitUnified;
27class Adapter;
28} // namespace unit
29} // namespace m5
30
31namespace m5 {
32namespace unit {
33
38class Component {
39public:
46 uint32_t clock{100000};
48 uint32_t stored_size{1};
50 bool self_update{false};
52 uint8_t max_children{0};
53 };
54
58 static const types::uid_t uid;
59 static const types::attr_t attr;
60 static const char name[];
62
66 explicit Component(const uint8_t addr = 0x00);
67
68 Component(const Component&) = delete;
69
70 Component(Component&&) noexcept = default;
72
76 Component& operator=(const Component&) = delete;
77
78 Component& operator=(Component&&) noexcept = default;
80
81 virtual ~Component() = default;
82
85
87 {
88 return _component_cfg;
89 }
91 inline void component_config(const component_config_t& cfg)
92 {
93 _component_cfg = cfg;
94 }
96
99
103 virtual bool begin()
104 {
105 return true;
106 }
111 virtual void update(const bool force = false)
112 {
113 (void)force;
114 }
116
119
120 inline const char* deviceName() const
121 {
122 return unit_device_name();
123 }
126 {
127 return unit_identifier();
128 }
131 {
132 return unit_attribute();
133 }
135 inline uint32_t order() const
136 {
137 return _order;
138 }
140 inline int16_t channel() const
141 {
142 return _channel;
143 }
145 inline bool isRegistered() const
146 {
147 return _manager != nullptr;
148 }
150 inline uint8_t address() const
151 {
152 return _addr;
153 }
158 inline Adapter* adapter() const
159 {
160 return _adapter.get();
161 }
163
166
167 inline bool inPeriodic() const
168 {
169 return in_periodic();
170 }
172 inline bool updated() const
173 {
174 return _updated;
175 }
181 {
182 return _latest;
183 }
189 {
190 return _interval;
191 }
193
196
197 virtual bool assign(m5::hal::bus::Bus* bus);
199 virtual bool assign(TwoWire& wire);
201
205
206 inline bool hasParent() const
207 {
208 return _parent;
209 }
211 inline bool hasSiblings() const
212 {
213 return _prev || _next;
214 }
216 inline bool hasChildren() const
217 {
218 return _child;
219 }
221 size_t childrenSize() const;
223 bool existsChild(const uint8_t ch) const;
225 Component* child(const uint8_t chhanle) const;
227 bool add(Component& c, const int16_t channel);
229 bool selectChannel(const uint8_t ch = 8);
231
233 template <typename T>
234 class iterator {
235 public:
236 using iterator_category = std::forward_iterator_tag;
237 using difference_type = std::ptrdiff_t;
238 using value_type = T;
239 using pointer = T*;
240 using reference = T&;
241
242 explicit iterator(Component* c = nullptr) : _ptr(c)
243 {
244 }
245
246 reference operator*() const
247 {
248 return *_ptr;
249 }
250 pointer operator->() const
251 {
252 return _ptr;
253 }
254 iterator& operator++()
255 {
256 _ptr = _ptr ? _ptr->_next : nullptr;
257 return *this;
258 }
259 iterator operator++(int)
260 {
261 auto tmp = *this;
262 ++(*this);
263 return tmp;
264 }
265 friend bool operator==(const iterator& a, const iterator& b)
266 {
267 return a._ptr == b._ptr;
268 }
269 friend bool operator!=(const iterator& a, const iterator& b)
270 {
271 return a._ptr != b._ptr;
272 }
273
274 private:
275 Component* _ptr;
276 };
278
281 using child_iterator = iterator<Component>;
282 using const_child_iterator = iterator<const Component>;
283 inline child_iterator childBegin() noexcept
284 {
285 return child_iterator(_child);
286 }
287 inline child_iterator childEnd() noexcept
288 {
289 return child_iterator();
290 }
291 inline const_child_iterator childBegin() const noexcept
292 {
293 return const_child_iterator(_child);
294 }
295 inline const_child_iterator childEnd() const noexcept
296 {
297 return const_child_iterator();
298 }
300
302 bool generalCall(const uint8_t* data, const size_t len);
303
305 virtual std::string debugInfo() const;
306
309 m5::hal::error::error_t readWithTransaction(uint8_t* data, const size_t len);
310 template <typename Reg,
311 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value && sizeof(Reg) <= 2,
312 std::nullptr_t>::type = nullptr>
313 bool readRegister(const Reg reg, uint8_t* rbuf, const size_t len, const uint32_t delayMillis,
314 const bool stop = true);
315 template <typename Reg,
316 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value && sizeof(Reg) <= 2,
317 std::nullptr_t>::type = nullptr>
318 bool readRegister8(const Reg reg, uint8_t& result, const uint32_t delayMillis, const bool stop = true);
319 template <typename Reg,
320 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value && sizeof(Reg) <= 2,
321 std::nullptr_t>::type = nullptr>
322 bool readRegister16(const Reg reg, uint16_t& result, const uint32_t delayMillis, const bool stop = true);
323 m5::hal::error::error_t writeWithTransaction(const uint8_t* data, const size_t len, const bool stop = true);
324 template <typename Reg,
325 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value && sizeof(Reg) <= 2,
326 std::nullptr_t>::type = nullptr>
327 m5::hal::error::error_t writeWithTransaction(const Reg reg, const uint8_t* data, const size_t len,
328 const bool stop = true);
329 template <typename Reg,
330 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value && sizeof(Reg) <= 2,
331 std::nullptr_t>::type = nullptr>
332 bool writeRegister(const Reg reg, const uint8_t* buf = nullptr, const size_t len = 0U, const bool stop = true);
333 template <typename Reg,
334 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value && sizeof(Reg) <= 2,
335 std::nullptr_t>::type = nullptr>
336 bool writeRegister8(const Reg reg, const uint8_t value, const bool stop = true);
337 template <typename Reg,
338 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value && sizeof(Reg) <= 2,
339 std::nullptr_t>::type = nullptr>
340 bool writeRegister16(const Reg reg, const uint16_t value, const bool stop = true);
342
343protected:
344 // Proper implementation in derived classes is required
345 virtual const char* unit_device_name() const = 0;
346 virtual types::uid_t unit_identifier() const = 0;
347 virtual types::attr_t unit_attribute() const = 0;
348 inline virtual bool in_periodic() const
349 {
350 return _periodic;
351 }
352
353 // Duplicate the adapter for children
354 // Note that ownership of the return pointer is delegated to the destination
355 inline virtual Adapter* duplicate_adapter(const uint8_t /*ch*/)
356 {
357 return nullptr;
358 }
359 // Select valid channel if exists(Hub etc...)
360 inline virtual m5::hal::error::error_t select_channel(const uint8_t)
361 {
362 return m5::hal::error::error_t::OK;
363 }
364
365 inline size_t stored_size() const
366 {
367 return _component_cfg.stored_size;
368 }
369 bool add_child(Component* c);
370 bool changeAddress(const uint8_t addr); // Functions for dynamically addressable devices
371
372protected:
373 // For periodic measurement
374 types::elapsed_time_t _latest{}, _interval{};
375 bool _periodic{}; // During periodic measurement?
376 bool _updated{};
377
378private:
379 UnitUnified* _manager{};
380 std::unique_ptr<m5::unit::Adapter> _adapter{};
381
382 uint32_t _order{};
383 component_config_t _component_cfg{};
384 int16_t _channel{-1}; // valid [0...]
385 uint8_t _addr{};
386 bool _begun{};
387
388 // for chain
389 Component* _parent{};
390 Component* _next{};
391 Component* _prev{};
392 Component* _child{};
393
394 friend class UnitUnified;
395};
396
414template <class Derived, typename MD>
416public:
419
425 template <typename... Args>
426 inline bool startPeriodicMeasurement(Args&&... args)
427 {
428 // Prepare for future common initiation preprocessing needs
429 return static_cast<Derived*>(this)->start_periodic_measurement(std::forward<Args>(args)...);
430 }
437 template <typename... Args>
438 inline bool stopPeriodicMeasurement(Args&&... args)
439 {
440 // Prepare for future common stopping preprocessing needs
441 return static_cast<Derived*>(this)->stop_periodic_measurement(std::forward<Args>(args)...);
442 }
444
448 inline size_t available() const
449 {
450 return available_periodic_measurement_data();
451 }
453 inline bool empty() const
454 {
455 return empty_periodic_measurement_data();
456 }
458 inline bool full() const
459 {
460 return full_periodic_measurement_data();
461 }
463 inline MD oldest() const
464 {
465 return static_cast<const Derived*>(this)->oldest_periodic_data();
466 }
468 inline MD latest() const
469 {
470 return static_cast<const Derived*>(this)->latest_periodic_data();
471 }
473 inline void discard()
474 {
475 discard_periodic_measurement_data();
476 }
478 inline void flush()
479 {
480 flush_periodic_measurement_data();
481 }
483
484protected:
488 virtual size_t available_periodic_measurement_data() const = 0;
489 virtual bool empty_periodic_measurement_data() const = 0;
490 virtual bool full_periodic_measurement_data() const = 0;
491 virtual void discard_periodic_measurement_data() = 0;
492 virtual void flush_periodic_measurement_data() = 0;
494};
495
496} // namespace unit
497} // namespace m5
498
499// Helper for creating derived classes from Component
501#define M5_UNIT_COMPONENT_HPP_BUILDER(cls, reg) \
502public: \
503 constexpr static uint8_t DEFAULT_ADDRESS{(reg)}; \
504 static const types::uid_t uid; \
505 static const types::attr_t attr; \
506 static const char name[]; \
507 \
508 cls(const cls&) = delete; \
509 \
510 cls& operator=(const cls&) = delete; \
511 \
512 cls(cls&&) noexcept = default; \
513 \
514 cls& operator=(cls&&) noexcept = default; \
515 \
516protected: \
517 inline virtual const char* unit_device_name() const override \
518 { \
519 return name; \
520 } \
521 inline virtual types::uid_t unit_identifier() const override \
522 { \
523 return uid; \
524 } \
525 inline virtual types::attr_t unit_attribute() const override \
526 { \
527 return attr; \
528 }
529
530// Helper for creating derived class from PeriodicMeasurementAdapter
531#define M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(cls, md) \
532protected: \
533 friend class PeriodicMeasurementAdapter<cls, md>; \
534 \
535 inline md oldest_periodic_data() const \
536 { \
537 return !_data->empty() ? _data->front().value() : md{}; \
538 } \
539 inline md latest_periodic_data() const \
540 { \
541 return !_data->empty() ? _data->back().value() : md{}; \
542 } \
543 inline virtual size_t available_periodic_measurement_data() const override \
544 { \
545 return _data->size(); \
546 } \
547 inline virtual bool empty_periodic_measurement_data() const override \
548 { \
549 return _data->empty(); \
550 } \
551 inline virtual bool full_periodic_measurement_data() const override \
552 { \
553 return _data->full(); \
554 } \
555 inline virtual void discard_periodic_measurement_data() override \
556 { \
557 _data->pop_front(); \
558 } \
559 inline virtual void flush_periodic_measurement_data() override \
560 { \
561 _data->clear(); \
562 }
563
565#endif
Adapters to treat M5HAL and TwoWire in the same way.
Adapters to treat M5HAL and TwoWire in the same way.
Definition adapter.hpp:28
Base class of unit component.
Definition M5UnitComponent.hpp:38
virtual void update(const bool force=false)
Update unit.
Definition M5UnitComponent.hpp:111
types::attr_t attribute() const
Gets the attributes.
Definition M5UnitComponent.hpp:130
bool existsChild(const uint8_t ch) const
Is there an other unit connected to the specified channel?
Definition M5UnitComponent.cpp:38
void component_config(const component_config_t &cfg)
Set the common configurations in each unit.
Definition M5UnitComponent.hpp:91
static const types::uid_t uid
Unique identifier.
Definition M5UnitComponent.hpp:58
bool selectChannel(const uint8_t ch=8)
Select valid channel if exists.
Definition M5UnitComponent.cpp:127
virtual std::string debugInfo() const
Output information for debug.
component_config_t component_config()
Gets the common configurations in each unit.
Definition M5UnitComponent.hpp:86
bool hasChildren() const
Are there other devices connected to me?
Definition M5UnitComponent.hpp:216
bool isRegistered() const
Is the unit registered with the manager?
Definition M5UnitComponent.hpp:145
static const char name[]
Device name string.
Definition M5UnitComponent.hpp:60
bool inPeriodic() const
In periodic measurement?
Definition M5UnitComponent.hpp:167
Component * child(const uint8_t chhanle) const
Gets the deviceconnected to the specified channel.
Definition M5UnitComponent.cpp:97
Adapter * adapter() const
Gets the access adapter.
Definition M5UnitComponent.hpp:158
bool hasParent() const
Has parent unit?
Definition M5UnitComponent.hpp:206
bool hasSiblings() const
Are there any other devices connected to the same parent unit besides yourself?
Definition M5UnitComponent.hpp:211
size_t childrenSize() const
Number of units connected to me.
Definition M5UnitComponent.cpp:27
types::uid_t identifier() const
Gets the identifier.
Definition M5UnitComponent.hpp:125
const char * deviceName() const
Gets the device name.
Definition M5UnitComponent.hpp:120
virtual bool assign(m5::hal::bus::Bus *bus)
Assgin m5::hal::bus.
Definition M5UnitComponent.cpp:109
bool updated() const
Periodic measurement data updated?
Definition M5UnitComponent.hpp:172
types::elapsed_time_t updatedMillis() const
Time elapsed since start-up when the measurement data was updated in update()
Definition M5UnitComponent.hpp:180
static const types::attr_t attr
Attributes.
Definition M5UnitComponent.hpp:59
uint8_t address() const
Address used to access the device.
Definition M5UnitComponent.hpp:150
virtual bool begin()
Begin unit.
Definition M5UnitComponent.hpp:103
int16_t channel() const
Gets the channel if connected to another unit.
Definition M5UnitComponent.hpp:140
types::elapsed_time_t interval() const
Gets the periodic measurement interval.
Definition M5UnitComponent.hpp:188
bool generalCall(const uint8_t *data, const size_t len)
General call for I2C.
bool add(Component &c, const int16_t channel)
Connect the unit to the specified channel.
Definition M5UnitComponent.cpp:46
uint32_t order() const
Gets the registered order (== 0 means not yet)
Definition M5UnitComponent.hpp:135
Interface class for periodic measurement (CRTP)
Definition M5UnitComponent.hpp:415
MD oldest() const
Retrieve oldest stored data.
Definition M5UnitComponent.hpp:463
size_t available() const
Gets the number of stored data.
Definition M5UnitComponent.hpp:448
bool startPeriodicMeasurement(Args &&... args)
Start periodic measurement.
Definition M5UnitComponent.hpp:426
void discard()
Discard the oldest data accumulated.
Definition M5UnitComponent.hpp:473
MD latest() const
Retrieve latest stored data.
Definition M5UnitComponent.hpp:468
bool stopPeriodicMeasurement(Args &&... args)
Stop periodic measurement.
Definition M5UnitComponent.hpp:438
bool empty() const
Is empty stored data?
Definition M5UnitComponent.hpp:453
bool full() const
Is stored data full?
Definition M5UnitComponent.hpp:458
void flush()
Discard all data.
Definition M5UnitComponent.hpp:478
Top level namespace of M5stack.
Definition test_helper.hpp:18
Unit-related namespace.
Component basic settings for begin.
Definition M5UnitComponent.hpp:44
uint32_t clock
Clock for communication (default as 100000)
Definition M5UnitComponent.hpp:46
uint32_t stored_size
Maximum number of periodic measurement data to be stored.
Definition M5UnitComponent.hpp:48
uint8_t max_children
Maximum number of units that can be connected (default as 0)
Definition M5UnitComponent.hpp:52
bool self_update
Does the user call Unit's update? (default as false)
Definition M5UnitComponent.hpp:50
Type and enumerator definitions.
unsigned long elapsed_time_t
Elapsed time unit (ms)
Definition types.hpp:25
uint32_t attr_t
Component attribute bits.
Definition types.hpp:24
uint32_t uid_t
Component unique identifier.
Definition types.hpp:23