NetBurner 3.5.7
PDF Version
TwoWireObject Class Reference

Convenience wrapper binding a TwoWire instance and device address. More...

#include <Wire.h>

Public Member Functions

 TwoWireObject (TwoWire &w, uint8_t addr)
 Construct a TwoWireObject with a specific TwoWire instance.
 
 TwoWireObject (uint8_t addr)
 Construct a TwoWireObject using the global Wire instance.
 
void reset ()
 Reset the I2C system and unhang the bus.
 
bool ping ()
 Check to see if the bound device address exists on this bus.
 
int writeRegN (uint32_t reg, const uint8_t *buf, uint32_t blen)
 Write a number of 8-bit values to the specified register address.
 
int readRegN (uint32_t reg, uint8_t *buf, uint32_t blen)
 Read a number of 8-bit values from the specified register address.
 
int writeReg8 (uint32_t reg, uint8_t data)
 Write an 8-bit value to a target device register.
 
int readReg8 (uint32_t reg, uint8_t &data)
 Read an 8-bit value from a target device register.
 

Detailed Description

Convenience wrapper binding a TwoWire instance and device address.

TwoWireObject stores a reference to a TwoWire instance and a fixed 7-bit I2C device address, providing a cleaner API for register-based access to a single device. All methods delegate to the underlying TwoWire, automatically supplying the stored device address.


Expand for Example Usage

TwoWireObject Example

Example: Using TwoWireObject for Register Access
#include <wire.h>
TwoWireObject Sensor(Wire, 0x68); // Bind to device at address 0x68
void UserMain(void *pd)
{
if (Sensor.ping())
{
uint8_t id;
Sensor.readReg8(0x75, id); // Read WHO_AM_I register
iprintf("Device ID: 0x%02X\r\n", id);
Sensor.writeReg8(0x6B, 0x00); // Wake up device
}
}
Arduino Wire-compatible I2C controller/target interface.
Definition Wire.h:255
void begin(uint8_t slave_addr=0)
Initialize the I2C interface.
Convenience wrapper binding a TwoWire instance and device address.
Definition Wire.h:839
class TwoWire Wire
Pre-declared global TwoWire instance using platform default pins.
Definition I2C/Wire/PAC193X/src/main.cpp:15

See also
TwoWire

Constructor & Destructor Documentation

◆ TwoWireObject() [1/2]

TwoWireObject::TwoWireObject ( TwoWire & w,
uint8_t addr )
inline

Construct a TwoWireObject with a specific TwoWire instance.

Parameters
wReference to the TwoWire instance to use
addr7-bit I2C device address

◆ TwoWireObject() [2/2]

TwoWireObject::TwoWireObject ( uint8_t addr)
inline

Construct a TwoWireObject using the global Wire instance.

Parameters
addr7-bit I2C device address

Member Function Documentation

◆ ping()

bool TwoWireObject::ping ( )
inline

Check to see if the bound device address exists on this bus.

Return values
trueif the address exists

◆ readReg8()

int TwoWireObject::readReg8 ( uint32_t reg,
uint8_t & data )
inline

Read an 8-bit value from a target device register.

Parameters
regRegister address to read
dataReference to variable in which to store the register data
Returns
Wire Result Codes

◆ readRegN()

int TwoWireObject::readRegN ( uint32_t reg,
uint8_t * buf,
uint32_t blen )
inline

Read a number of 8-bit values from the specified register address.

Parameters
regRegister address to read
*bufPointer to buffer to store received data
blenNumber of bytes to read
Returns
Wire Result Codes

◆ writeReg8()

int TwoWireObject::writeReg8 ( uint32_t reg,
uint8_t data )
inline

Write an 8-bit value to a target device register.

Parameters
regRegister address to write
dataData to write to register
Returns
Wire Result Codes

◆ writeRegN()

int TwoWireObject::writeRegN ( uint32_t reg,
const uint8_t * buf,
uint32_t blen )
inline

Write a number of 8-bit values to the specified register address.

Executing this function will send the following: a start bit, the target device address and read/write bit, the 8-bit data bytes, and finally a stop bit. Note that the address is not incremented with each 8-bit data value.

Parameters
regRegister address to write
*bufPointer to buffer containing data to write
blenNumber of bytes to write
Returns
Wire Result Codes

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