idfxx 1.0.0
Modern C++23 components for ESP-IDF
Loading...
Searching...
No Matches
idfxx::i2c::master_bus Class Reference

I2C master bus controller with thread-safe device access. More...

Public Member Functions

 master_bus (enum port port, gpio sda, gpio scl, freq::hertz frequency)
 Creates a new I2C master bus.
 
 ~master_bus ()
 
 master_bus (const master_bus &)=delete
 
master_busoperator= (const master_bus &)=delete
 
 master_bus (master_bus &&)=delete
 
master_busoperator= (master_bus &&)=delete
 
void lock () const
 Acquires exclusive access to the bus.
 
bool try_lock () const noexcept
 Tries to acquire exclusive access without blocking.
 
void unlock () const
 Releases exclusive access to the bus.
 
i2c_master_bus_handle_t handle () const
 Returns the underlying ESP-IDF bus handle.
 
enum port port () const
 Returns the I2C port.
 
freq::hertz frequency () const
 Returns the bus clock frequency in Hz.
 
std::vector< uint8_t > scan_devices () const
 Scans for devices on the bus.
 
template<typename Rep , typename Period >
std::vector< uint8_t > scan_devices (const std::chrono::duration< Rep, Period > &timeout) const
 Scans for devices on the bus.
 
void probe (uint8_t address) const
 Probes for a device at the specified address.
 
template<typename Rep , typename Period >
void probe (uint8_t address, const std::chrono::duration< Rep, Period > &timeout) const
 Probes for a device at the specified address.
 
result< void > try_probe (uint8_t address) const
 Probes for a device at the specified address.
 
template<typename Rep , typename Period >
result< void > try_probe (uint8_t address, const std::chrono::duration< Rep, Period > &timeout) const
 Probes for a device at the specified address.
 

Static Public Member Functions

static result< std::unique_ptr< master_bus > > make (enum port port, gpio sda, gpio scl, freq::hertz frequency)
 Creates a new I2C master bus.
 

Detailed Description

I2C master bus controller with thread-safe device access.

Represents an I2C bus operating in master mode. Satisfies the Lockable named requirement for use with std::lock_guard and std::unique_lock.

Definition at line 64 of file master.hpp.

Constructor & Destructor Documentation

◆ master_bus() [1/3]

idfxx::i2c::master_bus::master_bus ( enum port  port,
gpio  sda,
gpio  scl,
freq::hertz  frequency 
)
explicit

Creates a new I2C master bus.

Parameters
portI2C port number.
sdaGPIO pin for the SDA line.
sclGPIO pin for the SCL line.
frequencyClock frequency in Hz.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure.

◆ ~master_bus()

idfxx::i2c::master_bus::~master_bus ( )

◆ master_bus() [2/3]

idfxx::i2c::master_bus::master_bus ( const master_bus )
delete

◆ master_bus() [3/3]

idfxx::i2c::master_bus::master_bus ( master_bus &&  )
delete

Member Function Documentation

◆ frequency()

freq::hertz idfxx::i2c::master_bus::frequency ( ) const
inline

Returns the bus clock frequency in Hz.

Definition at line 117 of file master.hpp.

◆ handle()

i2c_master_bus_handle_t idfxx::i2c::master_bus::handle ( ) const
inline

Returns the underlying ESP-IDF bus handle.

Definition at line 111 of file master.hpp.

◆ lock()

void idfxx::i2c::master_bus::lock ( ) const
inline

Acquires exclusive access to the bus.

Definition at line 102 of file master.hpp.

◆ make()

static result< std::unique_ptr< master_bus > > idfxx::i2c::master_bus::make ( enum port  port,
gpio  sda,
gpio  scl,
freq::hertz  frequency 
)
static

Creates a new I2C master bus.

Parameters
portI2C port number.
sdaGPIO pin for the SDA line.
sclGPIO pin for the SCL line.
frequencyClock frequency in Hz.
Returns
The new master_bus, or an error.

◆ operator=() [1/2]

master_bus & idfxx::i2c::master_bus::operator= ( const master_bus )
delete

◆ operator=() [2/2]

master_bus & idfxx::i2c::master_bus::operator= ( master_bus &&  )
delete

◆ port()

enum port idfxx::i2c::master_bus::port ( ) const
inline

Returns the I2C port.

Definition at line 114 of file master.hpp.

◆ probe() [1/2]

void idfxx::i2c::master_bus::probe ( uint8_t  address) const
inline

Probes for a device at the specified address.

Parameters
address7-bit device address.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_errorif the device does not acknowledge or on error.

Definition at line 151 of file master.hpp.

References idfxx::i2c::DEFAULT_TIMEOUT, and probe().

Referenced by probe().

◆ probe() [2/2]

template<typename Rep , typename Period >
void idfxx::i2c::master_bus::probe ( uint8_t  address,
const std::chrono::duration< Rep, Period > &  timeout 
) const
inline

Probes for a device at the specified address.

Parameters
address7-bit device address.
timeoutMaximum time to wait for response.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_errorif the device does not acknowledge or on error.

Definition at line 163 of file master.hpp.

References idfxx::timeout, and idfxx::unwrap().

◆ scan_devices() [1/2]

std::vector< uint8_t > idfxx::i2c::master_bus::scan_devices ( ) const
inline

Scans for devices on the bus.

Probes addresses 0x08-0x77 and returns those that acknowledge.

Returns
Addresses of responding devices.

Definition at line 126 of file master.hpp.

References idfxx::i2c::DEFAULT_TIMEOUT, and scan_devices().

Referenced by scan_devices().

◆ scan_devices() [2/2]

template<typename Rep , typename Period >
std::vector< uint8_t > idfxx::i2c::master_bus::scan_devices ( const std::chrono::duration< Rep, Period > &  timeout) const
inline

Scans for devices on the bus.

Probes addresses 0x08-0x77 and returns those that acknowledge.

Parameters
timeoutMaximum time to wait for each probe.
Returns
Addresses of responding devices.

Definition at line 138 of file master.hpp.

References idfxx::timeout.

◆ try_lock()

bool idfxx::i2c::master_bus::try_lock ( ) const
inlinenoexcept

Tries to acquire exclusive access without blocking.

Definition at line 105 of file master.hpp.

◆ try_probe() [1/2]

result< void > idfxx::i2c::master_bus::try_probe ( uint8_t  address) const
inline

Probes for a device at the specified address.

Parameters
address7-bit device address.
Returns
Success if the device acknowledges, or an error.

Definition at line 175 of file master.hpp.

References idfxx::i2c::DEFAULT_TIMEOUT, and try_probe().

Referenced by try_probe().

◆ try_probe() [2/2]

template<typename Rep , typename Period >
result< void > idfxx::i2c::master_bus::try_probe ( uint8_t  address,
const std::chrono::duration< Rep, Period > &  timeout 
) const
inline

Probes for a device at the specified address.

Parameters
address7-bit device address.
timeoutMaximum time to wait for response.
Returns
Success if the device acknowledges, or an error.

Definition at line 186 of file master.hpp.

References idfxx::timeout.

◆ unlock()

void idfxx::i2c::master_bus::unlock ( ) const
inline

Releases exclusive access to the bus.

Definition at line 108 of file master.hpp.


The documentation for this class was generated from the following file: