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;
281 using child_iterator = iterator<Component>;
282 using const_child_iterator = iterator<const Component>;
283 inline child_iterator childBegin() noexcept
285 return child_iterator(_child);
287 inline child_iterator childEnd() noexcept
289 return child_iterator();
291 inline const_child_iterator childBegin() const noexcept
293 return const_child_iterator(_child);
295 inline const_child_iterator childEnd() const noexcept
297 return const_child_iterator();
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);
345 virtual const char* unit_device_name()
const = 0;
348 inline virtual bool in_periodic()
const
355 inline virtual Adapter* duplicate_adapter(
const uint8_t )
360 inline virtual m5::hal::error::error_t select_channel(
const uint8_t)
362 return m5::hal::error::error_t::OK;
365 inline size_t stored_size()
const
367 return _component_cfg.stored_size;
369 bool add_child(Component* c);
370 bool changeAddress(
const uint8_t addr);
374 types::elapsed_time_t _latest{}, _interval{};
379 UnitUnified* _manager{};
380 std::unique_ptr<m5::unit::Adapter> _adapter{};
383 component_config_t _component_cfg{};
384 int16_t _channel{-1};
389 Component* _parent{};
394 friend class UnitUnified;
414template <
class Derived,
typename MD>
425 template <
typename... Args>
429 return static_cast<Derived*
>(
this)->start_periodic_measurement(std::forward<Args>(args)...);
437 template <
typename... Args>
441 return static_cast<Derived*
>(
this)->stop_periodic_measurement(std::forward<Args>(args)...);
450 return available_periodic_measurement_data();
455 return empty_periodic_measurement_data();
460 return full_periodic_measurement_data();
465 return static_cast<const Derived*
>(
this)->oldest_periodic_data();
470 return static_cast<const Derived*
>(
this)->latest_periodic_data();
475 discard_periodic_measurement_data();
480 flush_periodic_measurement_data();
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;
501#define M5_UNIT_COMPONENT_HPP_BUILDER(cls, reg) \
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[]; \
508 cls(const cls&) = delete; \
510 cls& operator=(const cls&) = delete; \
512 cls(cls&&) noexcept = default; \
514 cls& operator=(cls&&) noexcept = default; \
517 inline virtual const char* unit_device_name() const override \
521 inline virtual types::uid_t unit_identifier() const override \
525 inline virtual types::attr_t unit_attribute() const override \
531#define M5_UNIT_COMPONENT_PERIODIC_MEASUREMENT_ADAPTER_HPP_BUILDER(cls, md) \
533 friend class PeriodicMeasurementAdapter<cls, md>; \
535 inline md oldest_periodic_data() const \
537 return !_data->empty() ? _data->front().value() : md{}; \
539 inline md latest_periodic_data() const \
541 return !_data->empty() ? _data->back().value() : md{}; \
543 inline virtual size_t available_periodic_measurement_data() const override \
545 return _data->size(); \
547 inline virtual bool empty_periodic_measurement_data() const override \
549 return _data->empty(); \
551 inline virtual bool full_periodic_measurement_data() const override \
553 return _data->full(); \
555 inline virtual void discard_periodic_measurement_data() override \
557 _data->pop_front(); \
559 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
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
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