uStepper
Public Member Functions | Private Member Functions | Private Attributes | List of all members
i2cMaster Class Reference

Prototype of class for accessing the TWI (I2C) interface of the AVR (master mode only). More...

#include <uStepper.h>

Public Member Functions

 i2cMaster (void)
 Constructor. More...
 
bool readByte (bool ack, uint8_t *data)
 Reads a byte from the I2C bus. More...
 
bool read (uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data)
 sets up I2C connection to device, reads a number of data bytes and closes the connection More...
 
bool start (uint8_t addr, bool RW)
 sets up connection between arduino and I2C device. More...
 
bool restart (uint8_t addr, bool RW)
 Restarts connection between arduino and I2C device. More...
 
bool writeByte (uint8_t data)
 Writes a byte to a device on the I2C bus. More...
 
bool write (uint8_t slaveAddr, uint8_t regAddr, uint8_t numOfBytes, uint8_t *data)
 sets up I2C connection to device, writes a number of data bytes and closes the connection More...
 
bool stop (void)
 Closes the I2C connection. More...
 
uint8_t getStatus (void)
 Get current I2C status. More...
 
void begin (void)
 Setup TWI (I2C) interface. More...
 

Private Member Functions

bool cmd (uint8_t cmd)
 Sends commands over the I2C bus. More...
 

Private Attributes

uint8_t status
 

Detailed Description

Prototype of class for accessing the TWI (I2C) interface of the AVR (master mode only).

This class enables the use of the hardware TWI (I2C) interface in the AVR (master mode only), which is used for interfacing with the encoder. This class is needed in this library, as arduino's build in "wire" library uses interrupts to access the TWI interface, and since the uStepper library needs to use the TWI interface within a timer interrupt, this library cannot be used. As a result of this, the "wire" library, cannot be used in sketches using the uStepper library, as this will screw with the setups, and make the sketch hang in the timer interrupt routine. Instead, if the programmer of the sketch needs to interface with external I2C devices, this class should be used. This library contains a predefined object called "I2C", which should be used for these purposes.

The functions "read()" and "write()", should be the only functions needed by most users of this library !

Definition at line 1210 of file uStepper.h.

Constructor & Destructor Documentation

◆ i2cMaster()

i2cMaster::i2cMaster ( void  )

Constructor.

This is the constructor, used to instantiate an I2C object. Under normal circumstances, this should not be needed by the programmer of the arduino sketch, since this library already has a global object instantiation of this class, called "I2C".

Definition at line 2113 of file uStepper.cpp.

Member Function Documentation

◆ begin()

void i2cMaster::begin ( void  )

Setup TWI (I2C) interface.

This function sets up the TWI interface, and is automatically called in the instantiation of the uStepper encoder object.

Definition at line 2105 of file uStepper.cpp.

◆ cmd()

bool i2cMaster::cmd ( uint8_t  cmd)
private

Sends commands over the I2C bus.

This function is used to send different commands over the I2C bus.

Parameters
cmd- Command to be send over the I2C bus.

Definition at line 1922 of file uStepper.cpp.

◆ getStatus()

uint8_t i2cMaster::getStatus ( void  )

Get current I2C status.

This function returns the status of the I2C bus.

Returns
Status of the I2C bus. Refer to defines for possible status

Definition at line 2100 of file uStepper.cpp.

◆ read()

bool i2cMaster::read ( uint8_t  slaveAddr,
uint8_t  regAddr,
uint8_t  numOfBytes,
uint8_t *  data 
)

sets up I2C connection to device, reads a number of data bytes and closes the connection

This function is used to perform a read transaction between the arduino and an I2C device. This function will perform everything from setting up the connection, reading the desired number of bytes and tear down the connection.

Parameters
slaveAddr- 7 bit address of the device to read from
regAddr- 8 bit address of the register to read from
numOfBytes- Number of bytes to read from the device
data- Address of the array/string to store the bytes read. Make sure enough space are allocated before calling this function !
Returns
1 - Currently always returns this value. In the future this value will be used to indicate successful transactions.

Definition at line 1943 of file uStepper.cpp.

◆ readByte()

bool i2cMaster::readByte ( bool  ack,
uint8_t *  data 
)

Reads a byte from the I2C bus.

This function requests a byte from the device addressed during the I2C transaction setup. The parameter "ack" is used to determine whether the device should keep sending data or not after the reception of the currently requested data byte.

Parameters
ack- should be set to "ACK" if more bytes is wanted, and "NACK" if no more bytes should be send (without the quotes)
data- Address of the variable to store the requested data byte
Returns
Always returns 1

Definition at line 2022 of file uStepper.cpp.

◆ restart()

bool i2cMaster::restart ( uint8_t  addr,
bool  RW 
)

Restarts connection between arduino and I2C device.

This function restarts the connection between the arduino and the I2C device desired to communicate with, by sending a start condition on the I2C bus, followed by the device address and a read/write bit.

Parameters
addr- Address of the device it is desired to communicate with
RW- Can be set to "READ" to setup a read transaction or "WRITE" for a write transaction (without the quotes)
Returns
1 - Connection properly set up
0 - Connection failed

Definition at line 2072 of file uStepper.cpp.

◆ start()

bool i2cMaster::start ( uint8_t  addr,
bool  RW 
)

sets up connection between arduino and I2C device.

This function sets up the connection between the arduino and the I2C device desired to communicate with, by sending a start condition on the I2C bus, followed by the device address and a read/write bit.

Parameters
addr- Address of the device it is desired to communicate with
RW- Can be set to "READ" to setup a read transaction or "WRITE" for a write transaction (without the quotes)
Returns
1 - Connection properly set up
0 - Connection failed

Definition at line 2046 of file uStepper.cpp.

◆ stop()

bool i2cMaster::stop ( void  )

Closes the I2C connection.

This function is used to close down the I2C connection, by sending a stop condition on the I2C bus.

Returns
1 - This is always returned Currently - in a later version, this should indicate that connection is successfully closed

Definition at line 2086 of file uStepper.cpp.

◆ write()

bool i2cMaster::write ( uint8_t  slaveAddr,
uint8_t  regAddr,
uint8_t  numOfBytes,
uint8_t *  data 
)

sets up I2C connection to device, writes a number of data bytes and closes the connection

This function is used to perform a write transaction between the arduino and an I2C device. This function will perform everything from setting up the connection, writing the desired number of bytes and tear down the connection.

Parameters
slaveAddr- 7 bit address of the device to write to
regAddr- 8 bit address of the register to write to
numOfBytes- Number of bytes to write to the device
data- Address of the array/string containing data to write.
Returns
1 - Currently always returns this value. In the future this value will be used to indicate successful transactions.

Definition at line 1989 of file uStepper.cpp.

◆ writeByte()

bool i2cMaster::writeByte ( uint8_t  data)

Writes a byte to a device on the I2C bus.

This function writes a byte to a device on the I2C bus.

Parameters
data- Byte to be written
Returns
1 - Byte written successfully
0 - transaction failed

Definition at line 2077 of file uStepper.cpp.

Member Data Documentation

◆ status

uint8_t i2cMaster::status
private

Contains the status of the I2C bus

Definition at line 1214 of file uStepper.h.


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