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

1-Wire bus controller with thread-safe access. More...

Public Member Functions

 bus (gpio pin)
 Creates a new 1-Wire bus controller.
 
 ~bus ()=default
 
 bus (const bus &)=delete
 
busoperator= (const bus &)=delete
 
 bus (bus &&other) noexcept=default
 
busoperator= (bus &&other) noexcept=default
 
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.
 
gpio pin () const noexcept
 Returns the GPIO pin.
 
bool reset ()
 Performs a 1-Wire reset cycle.
 
void select (address addr)
 Selects a specific device by ROM address.
 
void skip_rom ()
 Selects all devices on the bus.
 
result< voidtry_select (address addr)
 Selects a specific device by ROM address.
 
result< voidtry_skip_rom ()
 Selects all devices on the bus.
 
void write (uint8_t value)
 Writes a single byte to the bus.
 
void write (std::span< const uint8_t > data)
 Writes multiple bytes to the bus.
 
result< voidtry_write (uint8_t value)
 Writes a single byte to the bus.
 
result< voidtry_write (std::span< const uint8_t > data)
 Writes multiple bytes to the bus.
 
uint8_t read ()
 Reads a single byte from the bus.
 
void read (std::span< uint8_t > buf)
 Reads multiple bytes from the bus into a buffer.
 
result< uint8_ttry_read ()
 Reads a single byte from the bus.
 
result< voidtry_read (std::span< uint8_t > buf)
 Reads multiple bytes from the bus into a buffer.
 
void power ()
 Actively drives the bus high for parasitic power.
 
result< voidtry_power ()
 Actively drives the bus high for parasitic power.
 
void depower () noexcept
 Stops driving power onto the bus.
 
std::vector< addresssearch (size_t max_devices=8)
 Searches for all devices on the bus.
 
std::vector< addresssearch (uint8_t family_code, size_t max_devices=8)
 Searches for devices with a specific family code.
 
result< std::vector< address > > try_search (size_t max_devices=8)
 Searches for all devices on the bus.
 
result< std::vector< address > > try_search (uint8_t family_code, size_t max_devices=8)
 Searches for devices with a specific family code.
 

Static Public Member Functions

static result< busmake (gpio pin)
 Creates a new 1-Wire bus controller.
 

Detailed Description

1-Wire bus controller with thread-safe access.

Provides low-level 1-Wire bus operations: reset, ROM commands, data transfer, parasitic power control, and device search. Satisfies the Lockable named requirement for use with std::lock_guard and std::unique_lock.

This type is non-copyable and move-only. Result-returning methods on a moved-from object return errc::invalid_state. Simple accessors return default/null values. The Lockable methods (lock/try_lock/unlock) silently no-op on a moved-from object.

if (bus.reset()) {
bus.write(0x44); // Convert T command
}
1-Wire bus controller with thread-safe access.
Definition onewire.hpp:170
bool reset()
Performs a 1-Wire reset cycle.
void write(uint8_t value)
Writes a single byte to the bus.
Definition onewire.hpp:296
void skip_rom()
Selects all devices on the bus.
Definition onewire.hpp:262
constexpr gpio gpio_4
Definition gpio.hpp:812

Definition at line 170 of file onewire.hpp.

Constructor & Destructor Documentation

◆ bus() [1/3]

idfxx::onewire::bus::bus ( gpio  pin)
explicit

Creates a new 1-Wire bus controller.

Parameters
pinGPIO pin connected to the 1-Wire bus.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_errorif the pin is not connected.

◆ ~bus()

idfxx::onewire::bus::~bus ( )
default

◆ bus() [2/3]

idfxx::onewire::bus::bus ( const bus )
delete

◆ bus() [3/3]

idfxx::onewire::bus::bus ( bus &&  other)
defaultnoexcept

Member Function Documentation

◆ depower()

void idfxx::onewire::bus::depower ( )
noexcept

Stops driving power onto the bus.

Only needed after a previous call to power(). Note that reset() will also automatically depower the bus.

◆ lock()

void idfxx::onewire::bus::lock ( ) const
inline

Acquires exclusive access to the bus.

Definition at line 200 of file onewire.hpp.

◆ make()

static result< bus > idfxx::onewire::bus::make ( gpio  pin)
static

Creates a new 1-Wire bus controller.

Parameters
pinGPIO pin connected to the 1-Wire bus.
Returns
The new bus, or an error if the pin is not connected.
Return values
invalid_stateIf the pin is not connected.

◆ operator=() [1/2]

bus & idfxx::onewire::bus::operator= ( bus &&  other)
defaultnoexcept

◆ operator=() [2/2]

bus & idfxx::onewire::bus::operator= ( const bus )
delete

◆ pin()

gpio idfxx::onewire::bus::pin ( ) const
inlinenoexcept

Returns the GPIO pin.

Definition at line 224 of file onewire.hpp.

◆ power()

void idfxx::onewire::bus::power ( )
inline

Actively drives the bus high for parasitic power.

Some devices need more power than the pull-up resistor can provide during certain operations (e.g., temperature conversion).

Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron failure (e.g., bus is being held low).

Definition at line 373 of file onewire.hpp.

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

◆ read() [1/2]

uint8_t idfxx::onewire::bus::read ( )
inline

Reads a single byte from the bus.

Returns
The byte read.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron bus communication failure.

Definition at line 334 of file onewire.hpp.

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

◆ read() [2/2]

void idfxx::onewire::bus::read ( std::span< uint8_t buf)
inline

Reads multiple bytes from the bus into a buffer.

Parameters
bufThe buffer to read into.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron bus communication failure.

Definition at line 343 of file onewire.hpp.

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

◆ reset()

bool idfxx::onewire::bus::reset ( )

Performs a 1-Wire reset cycle.

Must be called before issuing ROM commands (select/skip_rom).

Returns
true if at least one device responds with a presence pulse, false if no devices were detected (or the bus is shorted).

◆ search() [1/2]

std::vector< address > idfxx::onewire::bus::search ( size_t  max_devices = 8)
inline

Searches for all devices on the bus.

Parameters
max_devicesMaximum number of devices to discover.
Returns
A vector of discovered device addresses.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron bus communication failure.

Definition at line 405 of file onewire.hpp.

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

◆ search() [2/2]

std::vector< address > idfxx::onewire::bus::search ( uint8_t  family_code,
size_t  max_devices = 8 
)
inline

Searches for devices with a specific family code.

Parameters
family_codeThe family code to filter by (e.g., 0x28 for DS18B20).
max_devicesMaximum number of devices to discover.
Returns
A vector of discovered device addresses matching the family code.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron bus communication failure.

Definition at line 416 of file onewire.hpp.

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

◆ select()

void idfxx::onewire::bus::select ( address  addr)
inline

Selects a specific device by ROM address.

Issues a "ROM select" command on the bus. A reset() must be performed before calling this method.

Parameters
addrThe ROM address of the device to select.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron bus communication failure.

Definition at line 251 of file onewire.hpp.

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

◆ skip_rom()

void idfxx::onewire::bus::skip_rom ( )
inline

Selects all devices on the bus.

Issues a "skip ROM" command. A reset() must be performed before calling this method.

Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron bus communication failure.

Definition at line 262 of file onewire.hpp.

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

◆ try_lock()

bool idfxx::onewire::bus::try_lock ( ) const
inlinenoexcept

Tries to acquire exclusive access without blocking.

Definition at line 208 of file onewire.hpp.

◆ try_power()

result< void > idfxx::onewire::bus::try_power ( )

Actively drives the bus high for parasitic power.

Some devices need more power than the pull-up resistor can provide during certain operations (e.g., temperature conversion).

Returns
Success, or an error on failure.

Referenced by power().

◆ try_read() [1/2]

result< uint8_t > idfxx::onewire::bus::try_read ( )

Reads a single byte from the bus.

Returns
The byte read, or an error on bus communication failure.

Referenced by read(), and read().

◆ try_read() [2/2]

result< void > idfxx::onewire::bus::try_read ( std::span< uint8_t buf)

Reads multiple bytes from the bus into a buffer.

Parameters
bufThe buffer to read into.
Returns
Success, or an error on bus communication failure.

◆ try_search() [1/2]

result< std::vector< address > > idfxx::onewire::bus::try_search ( size_t  max_devices = 8)

Searches for all devices on the bus.

Parameters
max_devicesMaximum number of devices to discover.
Returns
A vector of discovered device addresses, or an error on bus communication failure.

Referenced by search(), and search().

◆ try_search() [2/2]

result< std::vector< address > > idfxx::onewire::bus::try_search ( uint8_t  family_code,
size_t  max_devices = 8 
)

Searches for devices with a specific family code.

Parameters
family_codeThe family code to filter by (e.g., 0x28 for DS18B20).
max_devicesMaximum number of devices to discover.
Returns
A vector of discovered device addresses matching the family code, or an error on bus communication failure.

◆ try_select()

result< void > idfxx::onewire::bus::try_select ( address  addr)

Selects a specific device by ROM address.

Issues a "ROM select" command on the bus. A reset() must be performed before calling this method.

Parameters
addrThe ROM address of the device to select.
Returns
Success, or an error on bus communication failure.

Referenced by select().

◆ try_skip_rom()

result< void > idfxx::onewire::bus::try_skip_rom ( )

Selects all devices on the bus.

Issues a "skip ROM" command. A reset() must be performed before calling this method.

Returns
Success, or an error on bus communication failure.

Referenced by skip_rom().

◆ try_write() [1/2]

result< void > idfxx::onewire::bus::try_write ( std::span< const uint8_t data)

Writes multiple bytes to the bus.

Parameters
dataThe bytes to write.
Returns
Success, or an error on bus communication failure.

◆ try_write() [2/2]

result< void > idfxx::onewire::bus::try_write ( uint8_t  value)

Writes a single byte to the bus.

Parameters
valueThe byte to write.
Returns
Success, or an error on bus communication failure.

Referenced by write(), and write().

◆ unlock()

void idfxx::onewire::bus::unlock ( ) const
inline

Releases exclusive access to the bus.

Definition at line 216 of file onewire.hpp.

◆ write() [1/2]

void idfxx::onewire::bus::write ( std::span< const uint8_t data)
inline

Writes multiple bytes to the bus.

Parameters
dataThe bytes to write.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron bus communication failure.

Definition at line 305 of file onewire.hpp.

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

◆ write() [2/2]

void idfxx::onewire::bus::write ( uint8_t  value)
inline

Writes a single byte to the bus.

Parameters
valueThe byte to write.
Note
Only available when CONFIG_COMPILER_CXX_EXCEPTIONS is enabled in menuconfig.
Exceptions
std::system_erroron bus communication failure.

Definition at line 296 of file onewire.hpp.

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


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