Adafruit Bus IO Library
Public Member Functions | List of all members
Adafruit_I2CDevice Class Reference

< The class which defines how we will talk to this device over I2C More...

#include <Adafruit_I2CDevice.h>

Public Member Functions

 Adafruit_I2CDevice (uint8_t addr, TwoWire *theWire=&Wire)
 Create an I2C device at a given address. More...
 
uint8_t address (void)
 Returns the 7-bit address of this device. More...
 
bool begin (bool addr_detect=true)
 Initializes and does basic address detection. More...
 
void end (void)
 De-initialize device, turn off the Wire interface.
 
bool detected (void)
 Scans I2C for the address - note will give a false-positive if there's no pullups on I2C. More...
 
bool read (uint8_t *buffer, size_t len, bool stop=true)
 Read from I2C into a buffer from the I2C device. Cannot be more than maxBufferSize() bytes. More...
 
bool write (const uint8_t *buffer, size_t len, bool stop=true, const uint8_t *prefix_buffer=nullptr, size_t prefix_len=0)
 Write a buffer or two to the I2C device. Cannot be more than maxBufferSize() bytes. More...
 
bool write_then_read (const uint8_t *write_buffer, size_t write_len, uint8_t *read_buffer, size_t read_len, bool stop=false)
 Write some data, then read some data from I2C into another buffer. Cannot be more than maxBufferSize() bytes. The buffers can point to same/overlapping locations. More...
 
bool setSpeed (uint32_t desiredclk)
 Change the I2C clock speed to desired (relies on underlying Wire support! More...
 
size_t maxBufferSize ()
 How many bytes we can read in a transaction. More...
 

Detailed Description

< The class which defines how we will talk to this device over I2C

Constructor & Destructor Documentation

◆ Adafruit_I2CDevice()

Adafruit_I2CDevice::Adafruit_I2CDevice ( uint8_t  addr,
TwoWire *  theWire = &Wire 
)

Create an I2C device at a given address.

Parameters
addrThe 7-bit I2C address for the device
theWireThe I2C bus to use, defaults to &Wire

Member Function Documentation

◆ address()

uint8_t Adafruit_I2CDevice::address ( void  )

Returns the 7-bit address of this device.

Returns
The 7-bit address of this device

◆ begin()

bool Adafruit_I2CDevice::begin ( bool  addr_detect = true)

Initializes and does basic address detection.

Parameters
addr_detectWhether we should attempt to detect the I2C address with a scan. 99% of sensors/devices don't mind, but once in a while they don't respond well to a scan!
Returns
True if I2C initialized and a device with the addr found

◆ detected()

bool Adafruit_I2CDevice::detected ( void  )

Scans I2C for the address - note will give a false-positive if there's no pullups on I2C.

Returns
True if I2C initialized and a device with the addr found

◆ read()

bool Adafruit_I2CDevice::read ( uint8_t *  buffer,
size_t  len,
bool  stop = true 
)

Read from I2C into a buffer from the I2C device. Cannot be more than maxBufferSize() bytes.

Parameters
bufferPointer to buffer of data to read into
lenNumber of bytes from buffer to read.
stopWhether to send an I2C STOP signal on read
Returns
True if read was successful, otherwise false.

◆ write()

bool Adafruit_I2CDevice::write ( const uint8_t *  buffer,
size_t  len,
bool  stop = true,
const uint8_t *  prefix_buffer = nullptr,
size_t  prefix_len = 0 
)

Write a buffer or two to the I2C device. Cannot be more than maxBufferSize() bytes.

Parameters
bufferPointer to buffer of data to write. This is const to ensure the content of this buffer doesn't change.
lenNumber of bytes from buffer to write
prefix_bufferPointer to optional array of data to write before buffer. Cannot be more than maxBufferSize() bytes. This is const to ensure the content of this buffer doesn't change.
prefix_lenNumber of bytes from prefix buffer to write
stopWhether to send an I2C STOP signal on write
Returns
True if write was successful, otherwise false.

◆ write_then_read()

bool Adafruit_I2CDevice::write_then_read ( const uint8_t *  write_buffer,
size_t  write_len,
uint8_t *  read_buffer,
size_t  read_len,
bool  stop = false 
)

Write some data, then read some data from I2C into another buffer. Cannot be more than maxBufferSize() bytes. The buffers can point to same/overlapping locations.

Parameters
write_bufferPointer to buffer of data to write from
write_lenNumber of bytes from buffer to write.
read_bufferPointer to buffer of data to read into.
read_lenNumber of bytes from buffer to read.
stopWhether to send an I2C STOP signal between the write and read
Returns
True if write & read was successful, otherwise false.

◆ setSpeed()

bool Adafruit_I2CDevice::setSpeed ( uint32_t  desiredclk)

Change the I2C clock speed to desired (relies on underlying Wire support!

Parameters
desiredclkThe desired I2C SCL frequency
Returns
True if this platform supports changing I2C speed. Not necessarily that the speed was achieved!

◆ maxBufferSize()

size_t Adafruit_I2CDevice::maxBufferSize ( )
inline

How many bytes we can read in a transaction.

Returns
The size of the Wire receive/transmit buffer

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