Base Peripheral

The espp::BasePeripheral class is a base class for all peripherals. It provides a common interface for all peripherals and is used to access the peripheral’s registers. It is primarily designed to be used as a base class for peripheral classes that communicate using I2C (address-based) or SPI (CS-based) protocols.

The base class provides an interface for specifying different communications functions that the peripheral may use, as well as providing some base implementations for common functionality such as reading / writing u8 and u16 values from / to a register.

API Reference

Header File

Classes

template<std::integral RegisterAddressType = std::uint8_t, bool UseAddress = true>
class BasePeripheral : public espp::BaseComponent

Base class for all peripherals This class provides a common interface for all peripherals

It provides a way to probe the peripheral, write data to the peripheral, read data from the peripheral, and write then read data from the peripheral.

The peripheral is protected by a mutex to ensure that only one operation can be performed at a time.

Subclassed by espp::Ads1x15, espp::Ads7138, espp::As5600, espp::Aw9523, espp::Bm8563, espp::Chsc6x, espp::Drv2605, espp::Ft5x06, espp::Kts1622, espp::Max1704x, espp::Mcp23x17, espp::QwiicNes, espp::TKeyboard, espp::Tla2528, espp::Tt21100

Public Types

typedef std::function<bool(uint8_t)> probe_fn

Function to probe the peripheral

Param address

The address to probe

Return

True if the peripheral is found at the given address

Public Functions

inline bool probe (std::error_code &ec) requires(UseAddress)

Probe the peripheral

Note

This function is thread safe

Note

If the probe function is not set, this function will return false and set the error code to operation_not_supported

Note

This function is only available if UseAddress is true

Parameters

ec – The error code to set if there is an error

Returns

True if the peripheral is found

inline void set_address (uint8_t address) requires(UseAddress)

Set the address of the peripheral

Note

This function is thread safe

Note

This function is only available if UseAddress is true

Parameters

address – The address of the peripheral

inline void set_probe (const probe_fn &probe) requires(UseAddress)

Set the probe function

Note

This function is thread safe

Note

This should rarely be used, as the probe function is usually set in the constructor. If you need to change the probe function, consider using the set_config function instead.

Note

This function is only available if UseAddress is true

Parameters

probe – The probe function

inline void set_write(const write_fn &write)

Set the write function

Note

This function is thread safe

Note

This should rarely be used, as the write function is usually set in the constructor. If you need to change the write function, consider using the set_config function instead.

Parameters

write – The write function

inline void set_read(const read_fn &read)

Set the read function

Note

This function is thread safe

Note

This should rarely be used, as the read function is usually set in the constructor. If you need to change the read function, consider using the set_config function instead.

Parameters

read – The read function

inline void set_read_register(const read_register_fn &read_register)

Set the read register function

Note

This function is thread safe

Note

This should rarely be used, as the read register function is usually set in the constructor. If you need to change the read register function, consider using the set_config function instead.

Parameters

read_register – The read register function

inline void set_write_then_read(const write_then_read_fn &write_then_read)

Set the write then read function

Note

This function is thread safe

Note

This should rarely be used, as the write then read function is usually set in the constructor. If you need to change the write then

Parameters

write_then_read – The write then read function

inline void set_separate_write_then_read_delay(const std::chrono::milliseconds &delay)

Set the delay between the write and read operations in write_then_read

Note

This function is thread safe

Note

This should rarely be used, as the delay is usually set in the constructor. If you need to change the delay, consider using the set_config function instead.

Note

This delay is only used if the write_then_read function is not set to a custom function and the write and read functions are separate functions.

Parameters

delay – The delay between the write and read operations in write_then_read

inline void set_config(const Config &config)

Set the configuration for the peripheral

Note

This function is thread safe

Note

The configuration should normally be set in the constructor, but this function can be used to change the configuration after the peripheral has been created - for instance if the peripheral could be found on different communications buses.

Parameters

config – The configuration for the peripheral

inline void set_config(Config &&config)

Set the configuration for the peripheral

Note

This function is thread safe

Note

The configuration should normally be set in the constructor, but this function can be used to change the configuration after the peripheral has been created - for instance if the peripheral could be found on different communications buses.

Parameters

config – The configuration for the peripheral

inline const std::string &get_name() const

Get the name of the component

Note

This is the tag of the logger

Returns

A const reference to the name of the component

inline void set_log_tag(const std::string_view &tag)

Set the tag for the logger

Parameters

tag – The tag to use for the logger

inline espp::Logger::Verbosity get_log_level() const

Get the log level for the logger

Returns

The verbosity level of the logger

inline void set_log_level(espp::Logger::Verbosity level)

Set the log level for the logger

Parameters

level – The verbosity level to use for the logger

inline void set_log_verbosity(espp::Logger::Verbosity level)

Set the log verbosity for the logger

See also

set_log_level

Note

This is a convenience method that calls set_log_level

Parameters

level – The verbosity level to use for the logger

inline espp::Logger::Verbosity get_log_verbosity() const

Get the log verbosity for the logger

See also

get_log_level

Note

This is a convenience method that calls get_log_level

Returns

The verbosity level of the logger

inline void set_log_rate_limit(std::chrono::duration<float> rate_limit)

Set the rate limit for the logger

Note

Only calls to the logger that have _rate_limit suffix will be rate limited

Parameters

rate_limit – The rate limit to use for the logger

struct Config

Configuration for the peripheral.

Public Members

uint8_t address{0}

The address of the peripheral. Note that this is only used if UseAddress is true.

probe_fn probe = {nullptr}

Function to probe the peripheral. Note that this is only used if UseAddress is true

write_fn write = {nullptr}

Function to write data to the peripheral.

read_fn read = {nullptr}

Function to read data from the peripheral.

read_register_fn read_register{nullptr}

Function to read data at a specific address from the peripheral.

write_then_read_fn write_then_read{nullptr}

Function to write then read data from the peripheral.

std::chrono::milliseconds separate_write_then_read_delay{0}

The delay between the write and read operations in write_then_read in milliseconds if the write_then_read function is not set to a custom function and the write and read functions are separate