Class I2cDevice

Class Documentation

class I2cDevice

Low-level I2C device communication wrapper.

This class provides a clean interface for I2C communication using Linux I2C API. It manages the I2C device file descriptor and provides methods for device detection, register reading/writing, and proper error handling.

Public Functions

I2cDevice(const std::string &bus_path, uint8_t device_address)

Construct a new I2cDevice object.

Opens the I2C bus and configures the slave address. The object is fully initialized upon successful construction following RAII principles.

Parameters:
const std::string &bus_path

Path to the I2C bus device file (e.g., “/dev/i2c-7”)

uint8_t device_address

7-bit I2C device address

Throws:

std::runtime_error – if I2C bus cannot be opened or configured

bool is_connected() const

Check if the device is connected and responsive.

Returns:

true if device is connected, false otherwise

std::optional<uint8_t> read_register(uint8_t reg_address)

Read a single byte from a register.

Note

Uses hardware I2C timeout (configured at bus level, typically 1-2 seconds)

Parameters:
uint8_t reg_address

Register address to read from

Returns:

Register value if successful, std::nullopt otherwise

bool write_register(uint8_t reg_address, uint8_t value)

Write a single byte to a register.

Note

Uses hardware I2C timeout (configured at bus level, typically 1-2 seconds)

Parameters:
uint8_t reg_address

Register address to write to

uint8_t value

Value to write

Returns:

true if write succeeded, false otherwise

bool read_registers(uint8_t reg_address, uint8_t *buffer, size_t length)

Read multiple bytes from consecutive registers.

Performs an I2C combined transaction: write register address, then read data.

Note

Uses hardware I2C timeout (configured at bus level, typically 1-2 seconds) @threadsafe NO - Must not be called from multiple threads concurrently

Parameters:
uint8_t reg_address

Starting register address

uint8_t *buffer

Output buffer (must be at least length bytes)

size_t length

Number of bytes to read (maximum: 255)

Returns:

true if read succeeded, false if:

  • Device not connected

  • Buffer is null

  • Length is 0 or > 255

  • I2C transaction failed

Pre:

buffer != nullptr

Pre:

length > 0 && length <= 255

Pre:

buffer has at least length bytes allocated

bool write_registers(uint8_t reg_address, const uint8_t *buffer, size_t length)

Write multiple bytes to consecutive registers.

Note

Uses hardware I2C timeout (configured at bus level, typically 1-2 seconds) @threadsafe NO - Must not be called from multiple threads concurrently

Parameters:
uint8_t reg_address

Starting register address

const uint8_t *buffer

Buffer containing data to write (must be at least length bytes)

size_t length

Number of bytes to write (maximum: 255)

Returns:

true if write succeeded, false otherwise

Pre:

buffer != nullptr

Pre:

length > 0 && length <= 255

inline uint8_t get_device_address() const

Get the device address.

Returns:

uint8_t Device address

inline const std::string &get_bus_path() const

Get the bus path.

Returns:

const std::string& Bus path