41 #error "F_CPU not defined for i2c.h" 45 #include <external/i2cmaster/i2cmaster.h> 104 i2c_start((address << 1) | I2C_WRITE);
116 if (m_bufferIndex == c_bufferSize)
119 m_buffer[m_bufferIndex++] = byte;
134 for (uint8_t index = 0; index < m_bufferIndex; ++index) {
135 if (i2c_write(m_buffer[index]) != 0)
156 uint8_t
requestFrom(uint8_t address, uint8_t quantity,
bool stop =
true)
162 if (i2c_start((address << 1) | I2C_READ) != 0)
166 while (--quantity > 0) {
167 m_buffer[m_bufferIndex++] = i2c_readAck();
169 m_buffer[m_bufferIndex++] = i2c_readNak();
170 m_bufferReadIndex = 0;
175 return m_bufferIndex;
186 if (m_bufferReadIndex == m_bufferIndex)
189 return m_buffer[m_bufferReadIndex++];
193 static I2c s_instance;
195 static const uint8_t c_bufferSize = 8;
196 uint8_t m_buffer[c_bufferSize];
197 uint8_t m_bufferIndex = 0;
198 uint8_t m_bufferReadIndex = 0;
206 I2c I2c::s_instance =
I2c();
uint8_t requestFrom(uint8_t address, uint8_t quantity, bool stop=true)
Request bytes from an I2C (TWI) slave device.
Definition: i2c.h:156
static I2c & instance()
Returns the I2c::I2c instance.
Definition: i2c.h:86
A C++ wrapper for accessing the built-in I2C (TWI) communication interfaces.
Definition: i2c.h:78
uint8_t read()
Reads a byte that was received from a slave device after requesting with requestFrom().
Definition: i2c.h:184
void beginTransmission(uint8_t address)
Starts transmission to I2C (TWI) slave device with given address.
Definition: i2c.h:100
void endTransmission(bool stop=true)
Ends transmission to slave device.
Definition: i2c.h:132
Namespace containing all symbols of the AVR C++ utilities library.
Definition: adc.h:48
T min(T a, T b)
Returns the minimum of the two values a and b.
Definition: utils.h:188
void write(uint8_t byte)
Queues data for transmission to a slave device.
Definition: i2c.h:114