mraa  2.0.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
Public Member Functions
I2c Class Reference

Detailed Description

An I2c object represents an i2c master and can talk multiple i2c slaves by selecting the correct address It is considered best practice to make sure the address is correct before doing any calls on i2c, in case another application or even thread changed the addres on that bus. Multiple instances of the same bus can exist.

mraa::I2c i2c(I2C_BUS);
i2c.address(HMC5883L_I2C_ADDR);
rx_tx_buf[0] = HMC5883L_CONF_REG_B;
rx_tx_buf[1] = GA_1_3_REG;
i2c.write(rx_tx_buf, 2);
i2c.address(HMC5883L_I2C_ADDR);
rx_tx_buf[0] = HMC5883L_MODE_REG;
rx_tx_buf[1] = HMC5883L_CONT_MODE;
while (flag) {
i2c.address(HMC5883L_I2C_ADDR);
i2c.writeByte(HMC5883L_DATA_REG);
i2c.address(HMC5883L_I2C_ADDR);
i2c.read(rx_tx_buf, DATA_REG_SIZE);
x = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8) | rx_tx_buf[HMC5883L_X_LSB_REG];
z = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8) | rx_tx_buf[HMC5883L_Z_LSB_REG];
y = (rx_tx_buf[HMC5883L_Y_MSB_REG] << 8) | rx_tx_buf[HMC5883L_Y_LSB_REG];
// scale and calculate direction
direction = atan2(y * SCALE_0_92_MG, x * SCALE_0_92_MG);
// check if the signs are reversed
if (direction < 0)
direction += 2 * M_PI;
std::cout << "Compass scaled data x : %f, y : %f, z : %f" << x * SCALE_0_92_MG
<< y * SCALE_0_92_MG << z * SCALE_0_92_MG << std::endl;
std::cout << "Heading : %f" << direction * 180 / M_PI << std::endl;
sleep(1);
}

Public Member Functions

 I2c (int bus, bool raw=false)
 
 I2c (void *i2c_context)
 
 ~I2c ()
 
Result frequency (I2cMode mode)
 
Result address (uint8_t address)
 
uint8_t readByte ()
 
int read (uint8_t *data, int length)
 
uint8_t readReg (uint8_t reg)
 
uint16_t readWordReg (uint8_t reg)
 
int readBytesReg (uint8_t reg, uint8_t *data, int length)
 
Result writeByte (uint8_t data)
 
Result write (const uint8_t *data, int length)
 
Result writeReg (uint8_t reg, uint8_t data)
 
Result writeWordReg (uint8_t reg, uint16_t data)
 

Constructor & Destructor Documentation

I2c ( int  bus,
bool  raw = false 
)
inline

Instantiates an i2c bus. Multiple instances of the same bus can exist and the bus is not guaranteed to be on the correct address before read/write.

Parameters
busThe i2c bus to use
rawWhether to disable pinmapper for your board

Here is the call graph for this function:

I2c ( void *  i2c_context)
inline

I2C constructor, takes a pointer to a I2C context and initialises the I2C class

Parameters
i2c_contextvoid * to an I2C context
~I2c ( )
inline

Closes the I2c Bus used. This does not guarantee the bus will not be usable by anyone else or communicates this disconnect to any slaves.

Here is the call graph for this function:

Member Function Documentation

Result frequency ( I2cMode  mode)
inline

Sets the i2c Frequency for communication. Your board may not support the set frequency. Anyone can change this at any time and this will affect every slave on the bus

Parameters
modeFrequency to set the bus to
Returns
Result of operation

Here is the call graph for this function:

Result address ( uint8_t  address)
inline

Set the slave to talk to, typically called before every read/write operation

Parameters
addressCommunicate to the i2c slave on this address
Returns
Result of operation

Here is the call graph for this function:

uint8_t readByte ( )
inline

Read exactly one byte from the bus

Exceptions
std::invalid_argumentin case of error
Returns
char read from the bus

Here is the call graph for this function:

int read ( uint8_t *  data,
int  length 
)
inline

Read length bytes from the bus into *data pointer

Parameters
dataData to read into
lengthSize of read in bytes to make
Returns
length of read, should match length

Here is the call graph for this function:

uint8_t readReg ( uint8_t  reg)
inline

Read byte from an i2c register

Parameters
regRegister to read from
Exceptions
std::invalid_argumentin case of error
Returns
char read from register

Here is the call graph for this function:

uint16_t readWordReg ( uint8_t  reg)
inline

Read word from an i2c register

Parameters
regRegister to read from
Exceptions
std::invalid_argumentin case of error
Returns
char read from register

Here is the call graph for this function:

int readBytesReg ( uint8_t  reg,
uint8_t *  data,
int  length 
)
inline

Read length bytes from the bus into *data pointer starting from an i2c register

Parameters
regRegister to read from
datapointer to the byte array to read data in to
lengthmax number of bytes to read
Returns
length passed to the function or -1

Here is the call graph for this function:

Result writeByte ( uint8_t  data)
inline

Write a byte on the bus

Parameters
dataThe byte to send on the bus
Returns
Result of operation

Here is the call graph for this function:

Result write ( const uint8_t *  data,
int  length 
)
inline

Write length bytes to the bus, the first byte in the array is the command/register to write

Parameters
dataBuffer to send on the bus, first byte is i2c command
lengthSize of buffer to send
Returns
Result of operation

Here is the call graph for this function:

Result writeReg ( uint8_t  reg,
uint8_t  data 
)
inline

Write a byte to an i2c register

Parameters
regRegister to write to
dataValue to write to register
Returns
Result of operation

Here is the call graph for this function:

Result writeWordReg ( uint8_t  reg,
uint16_t  data 
)
inline

Write a word to an i2c register

Parameters
regRegister to write to
dataValue to write to register
Returns
Result of operation

Here is the call graph for this function:


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