atMETEO
An ATmega based weather station
Files | Classes

Wrapper for accessing built-in I2C (TWI) communication interfaces. More...

Collaboration diagram for I2C (TWI) module:

Files

file  i2c.h
 Wrapper for accessing built-in I2C (TWI) communication interfaces.
 

Classes

class  Avr::I2c
 A C++ wrapper for accessing the built-in I2C (TWI) communication interfaces. More...
 

Detailed Description

Wrapper for accessing built-in I2C (TWI) communication interfaces.

Avr::I2c is a class implementing a C++ wrapper for accessing the built-in I2C (TWI) communication interfaces.


Class Documentation

◆ Avr::I2c

class Avr::I2c

A C++ wrapper for accessing the built-in I2C (TWI) communication interfaces.

Usage:

auto i2c = Avr::I2c;
i2c.write(10);
i2c.endTransmission(false); // repeated start
uint8_t bytesRead = i2c.requestFrom(0xAB, 3);
for (int i = 0; i < bytesRead; i++) {
uint8_t byte i2c.read();
// ...
}
Note
Avr::I2c is currently limited to I2C (TWI) master functionality.
The implementation is based on i2cmaster (http://homepage.hispeed.ch/peterfleury/avr-software.html).

Public Member Functions

void beginTransmission (uint8_t address)
 Starts transmission to I2C (TWI) slave device with given address. More...
 
void write (uint8_t byte)
 Queues data for transmission to a slave device. More...
 
void endTransmission (bool stop=true)
 Ends transmission to slave device. More...
 
uint8_t requestFrom (uint8_t address, uint8_t quantity, bool stop=true)
 Request bytes from an I2C (TWI) slave device. More...
 
uint8_t read ()
 Reads a byte that was received from a slave device after requesting with requestFrom(). More...
 

Static Public Member Functions

static I2cinstance ()
 Returns the I2c::I2c instance. More...
 

Member Function Documentation

◆ instance()

static I2c& Avr::I2c::instance ( )
inlinestatic

Returns the I2c::I2c instance.

Returns
The I2c::I2c instance.

◆ beginTransmission()

void Avr::I2c::beginTransmission ( uint8_t  address)
inline

Starts transmission to I2C (TWI) slave device with given address.

Bytes are queued for transmission with write() and sent when calling endTransmission().

Parameters
addressThe address of the slave device.

◆ write()

void Avr::I2c::write ( uint8_t  byte)
inline

Queues data for transmission to a slave device.

Has to be called in-between beginTransmission() and endTransmission().

Parameters
byteThe value to be queued for transmission.

◆ endTransmission()

void Avr::I2c::endTransmission ( bool  stop = true)
inline

Ends transmission to slave device.

Has to be called after beginTransmission() and write().

Parameters
stopSend stop message after transmission. If set to true a stop message is sent to the slave device and the bus is released. If set to false no stop message is sent so that the master can continue the transmission (repeated start).

◆ requestFrom()

uint8_t Avr::I2c::requestFrom ( uint8_t  address,
uint8_t  quantity,
bool  stop = true 
)
inline

Request bytes from an I2C (TWI) slave device.

The bytes can be retrieved using read().

Parameters
addressThe address of the slave device.
quantityThe number of bytes to request from the slave device.
stopSend stop message after transmission. If set to true a stop message is sent to the slave device and the bus is released. If set to false no stop message is sent so that the master can continue the transmission (repeated start).
Returns
The number of bytes received from the slave device.

◆ read()

uint8_t Avr::I2c::read ( )
inline

Reads a byte that was received from a slave device after requesting with requestFrom().

Returns
The next available byte.