M5UnitUnified 0.5.5 git rev:bf711f3
Loading...
Searching...
No Matches
m5::unit::Component Class Referenceabstract

Base class of unit component. More...

#include <M5UnitComponent.hpp>

Classes

struct  component_config_t
 Component basic settings for begin. More...
 

Public Member Functions

bool generalCall (const uint8_t *data, const size_t len)
 General call for I2C.
 
virtual std::string debugInfo () const
 Output information for debug.
 
Constructor
Warning
COPY PROHIBITED
 Component (const uint8_t addr=0x00)
 
 Component (const Component &)=delete
 
 Component (Component &&) noexcept=default
 
Assignment
Warning
COPY PROHIBITED
Componentoperator= (const Component &)=delete
 
Componentoperator= (Component &&) noexcept=default
 
Component settings
component_config_t component_config () const
 Gets the common configurations in each unit.
 
void component_config (const component_config_t &cfg)
 Set the common configurations in each unit.
 
Functions that must be inherited
virtual bool begin ()
 Begin unit.
 
virtual void update (const bool force=false)
 Update unit.
 
Properties
const char * deviceName () const
 Gets the device name.
 
types::uid_t identifier () const
 Gets the identifier.
 
types::attr_t attribute () const
 Gets the attributes.
 
types::category_t category () const
 Gets the category.
 
uint32_t order () const
 Gets the registered order (== 0 means not yet)
 
int16_t channel () const
 Gets the channel if connected to another unit.
 
bool isRegistered () const
 Is the unit registered with the manager?
 
uint8_t address () const
 Address used to I2C access the device.
 
Adapteradapter () const
 Gets the access adapter.
 
template<class T >
auto asAdapter (const Adapter::Type t) -> typename std::remove_cv< typename std::remove_pointer< T >::type >::type *
 Gets the access adapter cast to the specified type.
 
template<class T >
auto asAdapter (const Adapter::Type t) const -> const typename std::remove_cv< typename std::remove_pointer< T >::type >::type *
 Gets the access adapter cast to the specified type (const overload)
 
Attributes
bool canAccessI2C () const
 Can the unit access via I2C?
 
bool canAccessGPIO () const
 Can the unit access via GPIO?
 
bool canAccessUART () const
 Can the unit access via UART?
 
bool canAccessSPI () const
 Can the unit access via SPI?
 
Periodic measurement
bool inPeriodic () const
 In periodic measurement?
 
bool updated () const
 Periodic measurement data updated?
 
types::elapsed_time_t updatedMillis () const
 Time elapsed since start-up when the measurement data was updated in update()
 
types::elapsed_time_t interval () const
 Gets the periodic measurement interval.
 
Assign(I2C)
virtual bool assign (TwoWire &wire)
 Assign TwoWire as the communication bus.
 
virtual bool assign (i2c_master_bus_handle_t bus)
 Assign I2C master bus (ESP-IDF native driver)
 
virtual bool assign (const i2c_port_t port, const gpio_num_t sda, const gpio_num_t scl)
 Assign I2C (ESP-IDF legacy driver, pre-installed port)
 
virtual bool assign (m5::I2C_Class &i2c)
 Assign I2C_Class as the communication bus.
 
Assign(GPIO)
virtual bool assign (const int8_t rx_pin, const int8_t tx_pin)
 Assign GPIO pins as the communication bus.
 
Assign(UART)
virtual bool assign (HardwareSerial &serial)
 Assign HardwareSerial as the communication bus.
 
virtual bool assign (const uart_port_t uart_num)
 Assign UART (ESP-IDF native driver, pre-installed port)
 
Assign(SPI)
virtual bool assign (SPIClass &spi, const SPISettings &settings)
 Assign SPIClass as the communication bus.
 
virtual bool assign (spi_device_handle_t handle, const gpio_num_t cs=GPIO_NUM_NC)
 Assign SPI device handle (ESP-IDF native driver, borrowed)
 
Assign(M5HAL)
virtual bool assign (m5::hal::bus::Bus *bus)
 Assign m5::hal::bus as the communication bus.
 
Parent-children relationship
Note
For daisy-chaining units such as hubs
bool hasParent () const
 Has parent unit?
 
bool hasSiblings () const
 Are there any other devices connected to the same parent unit besides yourself?
 
bool hasChildren () const
 Are there other devices connected to me?
 
size_t childrenSize () const
 Number of units connected to me.
 
bool existsChild (const uint8_t ch) const
 Is there another unit connected to the specified channel?
 
Componentparent ()
 Gets the parent unit.
 
Componentchild (const uint8_t channel) const
 Gets the device connected to the specified channel.
 
bool add (Component &c, const int16_t channel)
 Connect the unit to the specified channel.
 
bool selectChannel (const uint8_t ch=8)
 Select valid channel if exists.
 
Read/Write
m5::hal::error::error_t readWithTransaction (uint8_t *data, const size_t len)
 Read any data with transaction.
 
template<typename Reg >
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.
 
template<typename Reg >
bool readRegister8 (const Reg reg, uint8_t &result, const uint32_t delayMillis, const bool stop=true)
 Read byte with transaction from register.
 
template<typename Reg >
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.
 
template<typename Reg >
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.
 
template<typename Reg >
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.
 
template<typename Reg >
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.
 
m5::hal::error::error_t writeWithTransaction (const uint8_t *data, const size_t len, const bool stop=true)
 Write any data with transaction.
 
template<typename Reg >
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.
 
template<typename Reg >
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.
 
template<typename Reg >
bool writeRegister8 (const Reg reg, const uint8_t value, const bool stop=true)
 Write byte with transaction to register.
 
template<typename Reg >
bool writeRegister16BE (const Reg reg, const uint16_t value, const bool stop=true)
 Write word in big-endian order with transaction to register.
 
template<typename Reg >
bool writeRegister16LE (const Reg reg, const uint16_t value, const bool stop=true)
 Write word in little-endian order with transaction to register.
 
template<typename Reg >
bool writeRegister32BE (const Reg reg, const uint32_t value, const bool stop=true)
 Write dword in big-endian order with transaction to register.
 
template<typename Reg >
bool writeRegister32LE (const Reg reg, const uint32_t value, const bool stop=true)
 Write dword in little-endian order with transaction to register.
 

Static Public Attributes

Fixed parameters for class
Warning
Define the same name and type in the derived class.
static const types::uid_t uid {}
 Unique identifier.
 
static const types::attr_t attr {}
 Attributes.
 
static const char name [] = ""
 Device name string.
 

Protected Member Functions

virtual const char * unit_device_name () const =0
 
virtual types::uid_t unit_identifier () const =0
 
virtual types::attr_t unit_attribute () const =0
 
virtual types::category_t unit_category () const
 
virtual bool in_periodic () const
 
virtual std::shared_ptr< Adapterensure_adapter (const uint8_t)
 
virtual m5::hal::error::error_t select_channel (const uint8_t)
 
size_t stored_size () const
 
bool add_child (Component *c)
 
bool changeAddress (const uint8_t addr)
 

Detailed Description

Base class of unit component.

Member Function Documentation

◆ adapter()

Adapter * m5::unit::Component::adapter ( ) const
inline

Gets the access adapter.

Returns
Pointer to the adapter; ownership is retained by the unit
Warning
Ownership is retained by the unit and should not be released

◆ add()

bool m5::unit::Component::add ( Component & c,
const int16_t channel )

Connect the unit to the specified channel.

Parameters
cChild component to connect
channelChannel number to connect on
Returns
True if successful

◆ address()

uint8_t m5::unit::Component::address ( ) const
inline

Address used to I2C access the device.

Returns
I2C address of the device

◆ asAdapter() [1/2]

template<class T >
auto m5::unit::Component::asAdapter ( const Adapter::Type t) -> typename std::remove_cv<typename std::remove_pointer<T>::type>::type*
inline

Gets the access adapter cast to the specified type.

Template Parameters
TAdapter-derived pointer type to cast to
Parameters
tExpected adapter type
Returns
Pointer to the adapter cast to T, or nullptr if type does not match

◆ asAdapter() [2/2]

template<class T >
auto m5::unit::Component::asAdapter ( const Adapter::Type t) const -> const typename std::remove_cv<typename std::remove_pointer<T>::type>::type*
inline

Gets the access adapter cast to the specified type (const overload)

Template Parameters
TAdapter-derived pointer type to cast to
Parameters
tExpected adapter type
Returns
Const pointer to the adapter cast to T, or nullptr if type does not match

◆ assign() [1/10]

virtual bool m5::unit::Component::assign ( const i2c_port_t port,
const gpio_num_t sda,
const gpio_num_t scl )
virtual

Assign I2C (ESP-IDF legacy driver, pre-installed port)

Parameters
portI2C port (the driver must be installed beforehand via i2c_param_config / i2c_driver_install)
sdaSDA GPIO
sclSCL GPIO
Returns
True if successful

◆ assign() [2/10]

bool m5::unit::Component::assign ( const int8_t rx_pin,
const int8_t tx_pin )
virtual

Assign GPIO pins as the communication bus.

Parameters
rx_pinPin number to be used for RX
tx_pinPin number to be used for TX
Returns
True if successful

◆ assign() [3/10]

virtual bool m5::unit::Component::assign ( const uart_port_t uart_num)
virtual

Assign UART (ESP-IDF native driver, pre-installed port)

Parameters
uart_numUART port number (the driver must be installed beforehand via uart_driver_install / uart_param_config / uart_set_pin)
Returns
True if successful

◆ assign() [4/10]

virtual bool m5::unit::Component::assign ( HardwareSerial & serial)
virtual

Assign HardwareSerial as the communication bus.

Parameters
serialHardwareSerial to be used
Returns
True if successful

◆ assign() [5/10]

virtual bool m5::unit::Component::assign ( i2c_master_bus_handle_t bus)
virtual

Assign I2C master bus (ESP-IDF native driver)

Parameters
busESP-IDF I2C master bus handle
Returns
True if successful

◆ assign() [6/10]

bool m5::unit::Component::assign ( m5::hal::bus::Bus * bus)
virtual

Assign m5::hal::bus as the communication bus.

Parameters
busBus to be used
Returns
True if successful

◆ assign() [7/10]

bool m5::unit::Component::assign ( m5::I2C_Class & i2c)
virtual

Assign I2C_Class as the communication bus.

Parameters
i2cI2C_Class to be used (e.g. M5.In_I2C)
Returns
True if successful

◆ assign() [8/10]

virtual bool m5::unit::Component::assign ( spi_device_handle_t handle,
const gpio_num_t cs = GPIO_NUM_NC )
virtual

Assign SPI device handle (ESP-IDF native driver, borrowed)

Parameters
handleESP-IDF SPI device handle (create with spics_io_num = -1; init bus with SPI_DMA_DISABLED)
csCS GPIO controlled manually by this library. If GPIO_NUM_NC (default), uses address() as the CS pin (same convention as Arduino SPI)
Returns
True if successful

◆ assign() [9/10]

virtual bool m5::unit::Component::assign ( SPIClass & spi,
const SPISettings & settings )
virtual

Assign SPIClass as the communication bus.

Parameters
spiSPIClass to be used
settingsSPI settings to be applied
Returns
True if successful

◆ assign() [10/10]

virtual bool m5::unit::Component::assign ( TwoWire & wire)
virtual

Assign TwoWire as the communication bus.

Parameters
wireTwoWire to be used
Returns
True if successful

◆ attribute()

types::attr_t m5::unit::Component::attribute ( ) const
inline

Gets the attributes.

Returns
Attribute flags of the unit

◆ begin()

virtual bool m5::unit::Component::begin ( )
inlinevirtual

Begin unit.

Initiate functions based on component config and unit config

◆ canAccessGPIO()

bool m5::unit::Component::canAccessGPIO ( ) const

Can the unit access via GPIO?

Returns
True if the current adapter supports GPIO access

◆ canAccessI2C()

bool m5::unit::Component::canAccessI2C ( ) const

Can the unit access via I2C?

Returns
True if the current adapter supports I2C access

◆ canAccessSPI()

bool m5::unit::Component::canAccessSPI ( ) const

Can the unit access via SPI?

Returns
True if the current adapter supports SPI access

◆ canAccessUART()

bool m5::unit::Component::canAccessUART ( ) const

Can the unit access via UART?

Returns
True if the current adapter supports UART access

◆ category()

types::category_t m5::unit::Component::category ( ) const
inline

Gets the category.

Returns
Category of the unit

◆ channel()

int16_t m5::unit::Component::channel ( ) const
inline

Gets the channel if connected to another unit.

Returns
Channel number; negative if not connected to a parent

◆ child()

Component * m5::unit::Component::child ( const uint8_t channel) const

Gets the device connected to the specified channel.

Parameters
channelChannel number to query
Returns
Pointer to the child unit on that channel, or nullptr if none

◆ childrenSize()

size_t m5::unit::Component::childrenSize ( ) const

Number of units connected to me.

Returns
Number of child units connected to this unit

◆ component_config() [1/2]

component_config_t m5::unit::Component::component_config ( ) const
inline

Gets the common configurations in each unit.

Returns
Current component configuration

◆ component_config() [2/2]

void m5::unit::Component::component_config ( const component_config_t & cfg)
inline

Set the common configurations in each unit.

Parameters
cfgConfiguration to apply

◆ debugInfo()

std::string m5::unit::Component::debugInfo ( ) const
virtual

Output information for debug.

Returns
String containing debug information about this unit

◆ deviceName()

const char * m5::unit::Component::deviceName ( ) const
inline

Gets the device name.

Returns
Pointer to the null-terminated device name string

◆ existsChild()

bool m5::unit::Component::existsChild ( const uint8_t ch) const

Is there another unit connected to the specified channel?

Parameters
chChannel number to check
Returns
True if a child unit is connected on the specified channel

◆ generalCall()

bool m5::unit::Component::generalCall ( const uint8_t * data,
const size_t len )

General call for I2C.

Parameters
dataPointer to data to send
lenLength of data
Returns
True if successful

◆ hasChildren()

bool m5::unit::Component::hasChildren ( ) const
inline

Are there other devices connected to me?

Returns
True if at least one child unit is connected

◆ hasParent()

bool m5::unit::Component::hasParent ( ) const
inline

Has parent unit?

Returns
True if this unit is connected to a parent unit

◆ hasSiblings()

bool m5::unit::Component::hasSiblings ( ) const
inline

Are there any other devices connected to the same parent unit besides yourself?

Returns
True if sibling units exist on the same parent

◆ identifier()

types::uid_t m5::unit::Component::identifier ( ) const
inline

Gets the identifier.

Returns
Unique identifier of the unit

◆ inPeriodic()

bool m5::unit::Component::inPeriodic ( ) const
inline

In periodic measurement?

Returns
True if the unit is currently in periodic measurement mode

◆ interval()

types::elapsed_time_t m5::unit::Component::interval ( ) const
inline

Gets the periodic measurement interval.

Returns
interval time (Unit: ms)

◆ isRegistered()

bool m5::unit::Component::isRegistered ( ) const
inline

Is the unit registered with the manager?

Returns
True if registered with a UnitUnified manager

◆ order()

uint32_t m5::unit::Component::order ( ) const
inline

Gets the registered order (== 0 means not yet)

Returns
Registration order; 0 if not yet registered

◆ parent()

Component * m5::unit::Component::parent ( )
inline

Gets the parent unit.

Returns
Pointer to the parent unit, or nullptr if there is no parent

◆ selectChannel()

bool m5::unit::Component::selectChannel ( const uint8_t ch = 8)

Select valid channel if exists.

Parameters
chChannel number to select
Returns
True if the channel was selected successfully

◆ update()

virtual void m5::unit::Component::update ( const bool force = false)
inlinevirtual

Update unit.

Parameters
forceForced communication for updates if true

◆ updated()

bool m5::unit::Component::updated ( ) const
inline

Periodic measurement data updated?

Returns
True if measurement data was updated in the last update() call

◆ updatedMillis()

types::elapsed_time_t m5::unit::Component::updatedMillis ( ) const
inline

Time elapsed since start-up when the measurement data was updated in update()

Returns
Updated time (Unit: ms)