A compilation of some hardware sensors and their shared programming interfaces.

Embed: (wiki syntax)

« Back to documentation index

I2CSensor Class Reference

Base class from which digital sensors using I2C should be derived to simplify low-level communications with the device. More...

#include <I2CSensor.h>

Inherited by INA219, MAG3110, MAX17043, and MMA8451Q.

Public Member Functions

 I2CSensor (PinName sda, PinName scl, int i2c_addr)
PinName getSDAPin ()
PinName getSCLPin ()
uint8_t getDeviceI2CAddress ()
uint8_t getRegister (const uint8_t reg_addr)
 Read an 8-bit register.
uint16_t getRegister16b (const uint8_t reg_addr)
 Read a 16-bit register.
void setRegister (const uint8_t reg_addr, const uint8_t data)
 Set an 8-bit register.
void setRegister16b (const uint8_t reg_addr, const uint16_t data)
 Set a 16-bit register.

Protected Member Functions

int __readReg (const uint8_t reg_addr, uint8_t *data, int len)
int __writeReg (const uint8_t *data, int total_len)

Detailed Description

Base class from which digital sensors using I2C should be derived to simplify low-level communications with the device.

Definition at line 15 of file I2CSensor.h.


Constructor & Destructor Documentation

I2CSensor ( PinName  sda,
PinName  scl,
int  i2c_addr 
)
Parameters:
sdaI2C SDA pin ID
sclI2C SCL pin ID
i2c_addrI2C 8-bit address (LSB is actually don't care)

Definition at line 14 of file I2CSensor.cpp.


Member Function Documentation

int __readReg ( const uint8_t  reg_addr,
uint8_t *  data,
int  len 
) [protected]
Parameters:
reg_addr8-bit register address inside the device
data8-bit data that will be read from the register. This pointer MUST be valid. This array MUST be at least as long as len.
lentotal number of bytes to read. len must be >= 1.
Returns:
0 on success, otherwise error code from I2C

Definition at line 57 of file I2CSensor.cpp.

int __writeReg ( const uint8_t *  data,
int  total_len 
) [protected]
Parameters:
data8-bit data that will be written to the register. This pointer MUST be valid. This array MUST be at least as long as total_len. data[0] should be set to the 8-bit register address. The data payload should start at index 1.
total_lentotal length of the data array, which is the length of the payload + 1 for the register address. total_len must be >= 2.
Returns:
0 on success, otherwise error code from I2C

Definition at line 72 of file I2CSensor.cpp.

uint8_t getDeviceI2CAddress (  )
Returns:
Device I2C address (LSB always 0 in this case, it is don't care)

Definition at line 28 of file I2CSensor.cpp.

uint8_t getRegister ( const uint8_t  reg_addr )

Read an 8-bit register.

Parameters:
reg_addrthe register in the device
Returns:
The raw value from the register specified by reg_addr.

Definition at line 30 of file I2CSensor.cpp.

uint16_t getRegister16b ( const uint8_t  reg_addr )

Read a 16-bit register.

Parameters:
reg_addrthe register in the device
Returns:
The raw value from the register specified by reg_addr.

Definition at line 36 of file I2CSensor.cpp.

PinName getSCLPin (  )
Returns:
I2C SCL pin ID

Definition at line 26 of file I2CSensor.cpp.

PinName getSDAPin (  )
Returns:
I2C SDA pin ID

Definition at line 24 of file I2CSensor.cpp.

void setRegister ( const uint8_t  reg_addr,
const uint8_t  data 
)

Set an 8-bit register.

Parameters:
reg_addrthe register in the device
datathe byte to write to the register

Definition at line 43 of file I2CSensor.cpp.

void setRegister16b ( const uint8_t  reg_addr,
const uint16_t  data 
)

Set a 16-bit register.

Parameters:
reg_addrthe register in the device
datathe byte to write to the register

Definition at line 48 of file I2CSensor.cpp.