10#ifndef M5_UNIT_COMPONENT_HPP
11#define M5_UNIT_COMPONENT_HPP
66 explicit Component(
const uint8_t addr = 0x00);
88 return _component_cfg;
111 virtual void update(
const bool force =
false)
122 return unit_device_name();
127 return unit_identifier();
132 return unit_attribute();
147 return _manager !=
nullptr;
160 return _adapter.get();
169 return in_periodic();
197 virtual bool assign(m5::hal::bus::Bus* bus);
199 virtual bool assign(TwoWire& wire);
213 return _prev || _next;
233 template <
typename T>
236 using iterator_category = std::forward_iterator_tag;
237 using difference_type = std::ptrdiff_t;
238 using value_type = T;
240 using reference = T&;
242 explicit iterator(
Component* c =
nullptr) : _ptr(c)
246 reference operator*()
const
250 pointer operator->()
const
254 iterator& operator++()
256 _ptr = _ptr ? _ptr->_next :
nullptr;
259 iterator operator++(
int)
265 friend bool operator==(
const iterator& a,
const iterator& b)
267 return a._ptr == b._ptr;
269 friend bool operator!=(
const iterator& a,
const iterator& b)
271 return a._ptr != b._ptr;
278 using child_iterator = iterator<Component>;
279 using const_child_iterator = iterator<const Component>;
280 inline child_iterator childBegin() noexcept
282 return child_iterator(_child);
284 inline child_iterator childEnd() noexcept
286 return child_iterator();
288 inline const_child_iterator childBegin() const noexcept
290 return const_child_iterator(_child);
292 inline const_child_iterator childEnd() const noexcept
294 return const_child_iterator();
307 template <
typename Reg,
308 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
309 std::nullptr_t>::type =
nullptr>
310 bool readRegister(
const Reg reg, uint8_t* rbuf,
const size_t len,
const uint32_t delayMillis,
311 const bool stop =
true);
312 template <
typename Reg,
313 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
314 std::nullptr_t>::type =
nullptr>
315 bool readRegister8(
const Reg reg, uint8_t& result,
const uint32_t delayMillis,
const bool stop =
true);
317 template <
typename Reg,
318 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
319 std::nullptr_t>::type =
nullptr>
320 inline bool readRegister16BE(
const Reg reg, uint16_t& result,
const uint32_t delayMillis,
const bool stop =
true)
322 return read_register16E(reg, result, delayMillis, stop,
true);
325 template <
typename Reg,
326 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
327 std::nullptr_t>::type =
nullptr>
328 inline bool readRegister16LE(
const Reg reg, uint16_t& result,
const uint32_t delayMillis,
const bool stop =
true)
330 return read_register16E(reg, result, delayMillis, stop,
false);
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 inline bool readRegister32BE(
const Reg reg, uint32_t& result,
const uint32_t delayMillis,
const bool stop =
true)
338 return read_register32E(reg, result, delayMillis, stop,
true);
341 template <
typename Reg,
342 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
343 std::nullptr_t>::type =
nullptr>
344 inline bool readRegister32LE(
const Reg reg, uint32_t& result,
const uint32_t delayMillis,
const bool stop =
true)
346 return read_register32E(reg, result, delayMillis, stop,
false);
349 m5::hal::error::error_t
writeWithTransaction(
const uint8_t* data,
const size_t len,
const bool stop =
true);
351 template <
typename Reg,
352 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
353 std::nullptr_t>::type =
nullptr>
354 m5::hal::error::error_t
writeWithTransaction(
const Reg reg,
const uint8_t* data,
const size_t len,
355 const bool stop =
true);
357 template <
typename Reg,
358 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
359 std::nullptr_t>::type =
nullptr>
360 bool writeRegister(
const Reg reg,
const uint8_t* buf =
nullptr,
const size_t len = 0U,
const bool stop =
true);
362 template <
typename Reg,
363 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
364 std::nullptr_t>::type =
nullptr>
365 bool writeRegister8(
const Reg reg,
const uint8_t value,
const bool stop =
true);
367 template <
typename Reg,
368 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
369 std::nullptr_t>::type =
nullptr>
370 inline bool writeRegister16BE(
const Reg reg,
const uint16_t value,
const bool stop =
true)
372 return write_register16E(reg, value, stop,
true);
375 template <
typename Reg,
376 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
377 std::nullptr_t>::type =
nullptr>
378 inline bool writeRegister16LE(
const Reg reg,
const uint16_t value,
const bool stop =
true)
380 return write_register16E(reg, value, stop,
false);
383 template <
typename Reg,
384 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
385 std::nullptr_t>::type =
nullptr>
386 inline bool writeRegister32BE(
const Reg reg,
const uint32_t value,
const bool stop =
true)
388 return write_register32E(reg, value, stop,
true);
391 template <
typename Reg,
392 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
393 std::nullptr_t>::type =
nullptr>
394 inline bool writeRegister32LE(
const Reg reg,
const uint32_t value,
const bool stop =
true)
396 return write_register32E(reg, value, stop,
false);
400 template <
typename Reg,
401 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
402 std::nullptr_t>::type =
nullptr>
403 [[deprecated(
"Use readRegister16BE() or readRegister16LE(). To be removed in the next minor version increase")]]
404 inline bool readRegister16(
const Reg reg, uint16_t& result,
const uint32_t delayMillis,
const bool stop =
true)
406 return read_register16E(reg, result, delayMillis, stop,
true);
409 template <
typename Reg,
410 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
411 std::nullptr_t>::type =
nullptr>
412 [[deprecated(
"Use writeRegister16BE() or writeRegister16LE(). To be removed in the next minor version increase")]]
413 inline bool writeRegister16(
const Reg reg,
const uint16_t value,
const bool stop =
true)
415 return write_register16E(reg, value, stop,
true);
420#if defined(DOXYGEN_PROCESS)
428 template <
typename Reg>
429 bool readRegister(
const Reg reg, uint8_t* rbuf,
const size_t len,
const uint32_t delayMillis,
430 const bool stop =
true);
432 template <
typename Reg>
433 bool readRegister8(
const Reg reg, uint8_t& result,
const uint32_t delayMillis,
const bool stop =
true);
435 template <
typename Reg>
436 bool readRegister16BE(
const Reg reg, uint16_t& result,
const uint32_t delayMillis,
const bool stop =
true);
438 template <
typename Reg>
439 bool readRegister16LE(
const Reg reg, uint16_t& result,
const uint32_t delayMillis,
const bool stop =
true);
441 template <
typename Reg>
442 bool readRegister32BE(
const Reg reg, uint32_t& result,
const uint32_t delayMillis,
const bool stop =
true);
444 template <
typename Reg>
445 bool readRegister32LE(
const Reg reg, uint32_t& result,
const uint32_t delayMillis,
const bool stop =
true);
448 m5::hal::error::error_t
writeWithTransaction(
const uint8_t* data,
const size_t len,
const bool stop =
true);
450 template <
typename Reg>
452 const bool stop =
true);
454 template <
typename Reg>
455 bool writeRegister(
const Reg reg,
const uint8_t* buf =
nullptr,
const size_t len = 0U,
const bool stop =
true);
457 template <
typename Reg>
460 template <
typename Reg>
463 template <
typename Reg>
466 template <
typename Reg>
469 template <
typename Reg>
476 virtual const char* unit_device_name()
const = 0;
479 inline virtual bool in_periodic()
const
486 inline virtual Adapter* duplicate_adapter(
const uint8_t )
491 inline virtual m5::hal::error::error_t select_channel(
const uint8_t)
493 return m5::hal::error::error_t::OK;
496 inline size_t stored_size()
const
498 return _component_cfg.stored_size;
500 bool add_child(Component* c);
501 bool changeAddress(
const uint8_t addr);
503 template <
typename Reg,
504 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
505 std::nullptr_t>::type =
nullptr>
506 bool read_register16E(
const Reg reg, uint16_t& result,
const uint32_t delayMillis,
const bool stop,
508 template <
typename Reg,
509 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
510 std::nullptr_t>::type =
nullptr>
511 bool write_register16E(
const Reg reg,
const uint16_t value,
const bool stop,
const bool endifan);
512 template <
typename Reg,
513 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
514 std::nullptr_t>::type =
nullptr>
515 bool read_register32E(
const Reg reg, uint32_t& result,
const uint32_t delayMillis,
const bool stop,
517 template <
typename Reg,
518 typename std::enable_if<std::is_integral<Reg>::value && std::is_unsigned<Reg>::value &&
sizeof(Reg) <= 2,
519 std::nullptr_t>::type =
nullptr>
520 bool write_register32E(
const Reg reg,
const uint32_t value,
const bool stop,
const bool endifan);
524 types::elapsed_time_t _latest{}, _interval{};
529 UnitUnified* _manager{};
530 std::unique_ptr<m5::unit::Adapter> _adapter{};
533 component_config_t _component_cfg{};
534 int16_t _channel{-1};
539 Component* _parent{};
544 friend class UnitUnified;
564template <
class Derived,
typename MD>
575 template <
typename... Args>
579 return static_cast<Derived*
>(
this)->start_periodic_measurement(std::forward<Args>(args)...);
587 template <
typename... Args>
591 return static_cast<Derived*
>(
this)->stop_periodic_measurement(std::forward<Args>(args)...);
600 return available_periodic_measurement_data();
605 return empty_periodic_measurement_data();
610 return full_periodic_measurement_data();
615 return static_cast<const Derived*
>(
this)->oldest_periodic_data();
620 return static_cast<const Derived*
>(
this)->latest_periodic_data();
625 discard_periodic_measurement_data();
630 flush_periodic_measurement_data();
638 virtual size_t available_periodic_measurement_data()
const = 0;
639 virtual bool empty_periodic_measurement_data()
const = 0;
640 virtual bool full_periodic_measurement_data()
const = 0;
641 virtual void discard_periodic_measurement_data() = 0;
642 virtual void flush_periodic_measurement_data() = 0;
651#define M5_UNIT_COMPONENT_HPP_BUILDER(cls, reg) \
653 constexpr static uint8_t DEFAULT_ADDRESS{(reg)}; \
654 static const types::uid_t uid; \
655 static const types::attr_t attr; \
656 static const char name[]; \
658 cls(const cls&) = delete; \
660 cls& operator=(const cls&) = delete; \
662 cls(cls&&) noexcept = default; \
664 cls& operator=(cls&&) noexcept = default; \
667 inline virtual const char* unit_device_name() const override \
671 inline virtual types::uid_t unit_identifier() const override \
675 inline virtual types::attr_t unit_attribute() const override \
681#define M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(cls, md) \
683 friend class PeriodicMeasurementAdapter<cls, md>; \
685 inline md oldest_periodic_data() const \
687 return !_data->empty() ? _data->front().value() : md{}; \
689 inline md latest_periodic_data() const \
691 return !_data->empty() ? _data->back().value() : md{}; \
693 inline virtual size_t available_periodic_measurement_data() const override \
695 return _data->size(); \
697 inline virtual bool empty_periodic_measurement_data() const override \
699 return _data->empty(); \
701 inline virtual bool full_periodic_measurement_data() const override \
703 return _data->full(); \
705 inline virtual void discard_periodic_measurement_data() override \
707 _data->pop_front(); \
709 inline virtual void flush_periodic_measurement_data() override \
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
bool readRegister16BE(const Reg reg, uint16_t &result, const uint32_t delayMillis, const bool stop=true)
Read word in big-endian order with transaction from register.
void component_config(const component_config_t &cfg)
Set the common configurations in each unit.
Definition M5UnitComponent.hpp:91
bool readRegister8(const Reg reg, uint8_t &result, const uint32_t delayMillis, const bool stop=true)
Read byte with transaction from register.
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
bool writeRegister8(const Reg reg, const uint8_t value, const bool stop=true)
Write byte with transaction to register.
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 writeRegister32BE(const Reg reg, const uint32_t value, const bool stop=true)
Write dword in big-endian order with transaction from register.
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 writeRegister32LE(const Reg reg, const uint32_t value, const bool stop=true)
Write dword in little-endian order with transaction from register.
bool inPeriodic() const
In periodic measurement?
Definition M5UnitComponent.hpp:167
bool writeRegister16BE(const Reg reg, const uint16_t value, const bool stop=true)
Write word in big-endian order with transaction from register.
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
m5::hal::error::error_t writeWithTransaction(const Reg reg, const uint8_t *data, const size_t len, const bool stop=true)
Write any data with transaction to register.
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
bool readRegister(const Reg reg, uint8_t *rbuf, const size_t len, const uint32_t delayMillis, const bool stop=true)
Read any data with transaction from register.
bool writeRegister(const Reg reg, const uint8_t *buf=nullptr, const size_t len=0U, const bool stop=true)
Write any data with transaction to register.
bool readRegister16LE(const Reg reg, uint16_t &result, const uint32_t delayMillis, const bool stop=true)
Read word in little-endian order with transaction from register.
bool readRegister32BE(const Reg reg, uint32_t &result, const uint32_t delayMillis, const bool stop=true)
Read dword in big-endian order with transaction from register.
m5::hal::error::error_t readWithTransaction(uint8_t *data, const size_t len)
Read any data with transaction.
Definition M5UnitComponent.cpp:139
bool writeRegister16LE(const Reg reg, const uint16_t value, const bool stop=true)
Write word in little-endian order with transaction from register.
m5::hal::error::error_t writeWithTransaction(const uint8_t *data, const size_t len, const bool stop=true)
Write any data with transaction.
Definition M5UnitComponent.cpp:146
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
bool readRegister32LE(const Reg reg, uint32_t &result, const uint32_t delayMillis, const bool stop=true)
Read dword in little-endian order with transaction from register.
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:565
MD oldest() const
Retrieve oldest stored data.
Definition M5UnitComponent.hpp:613
size_t available() const
Gets the number of stored data.
Definition M5UnitComponent.hpp:598
bool startPeriodicMeasurement(Args &&... args)
Start periodic measurement.
Definition M5UnitComponent.hpp:576
void discard()
Discard the oldest data accumulated.
Definition M5UnitComponent.hpp:623
MD latest() const
Retrieve latest stored data.
Definition M5UnitComponent.hpp:618
bool stopPeriodicMeasurement(Args &&... args)
Stop periodic measurement.
Definition M5UnitComponent.hpp:588
bool empty() const
Is empty stored data?
Definition M5UnitComponent.hpp:603
bool full() const
Is stored data full?
Definition M5UnitComponent.hpp:608
void flush()
Discard all data.
Definition M5UnitComponent.hpp:628
Top level namespace of M5stack.
Definition test_helper.hpp:18
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