AceWire  0.4.1
Unified interface for selecting different I2C implementations on Arduino platforms
Public Member Functions | List of all members
ace_wire::SimpleWireInterface Class Reference

A software I2C implementation for sending LED segment patterns over I2C. More...

#include <SimpleWireInterface.h>

Public Member Functions

 SimpleWireInterface (uint8_t dataPin, uint8_t clockPin, uint8_t delayMicros)
 Constructor. More...
 
void begin () const
 Initialize the clock and data pins. More...
 
void end () const
 Set clock and data pins to INPUT mode.
 
uint8_t beginTransmission (uint8_t addr) const
 Send the I2C START condition. More...
 
uint8_t write (uint8_t data) const
 Send the data byte on the data bus, with MSB first as specified by I2C. More...
 
uint8_t endTransmission (bool sendStop=true) const
 Send the I2C STOP condition. More...
 
uint8_t requestFrom (uint8_t addr, uint8_t quantity, bool sendStop=true) const
 Prepare to read bytes by sending I2C START condition. More...
 
uint8_t read () const
 Read byte. More...
 
 SimpleWireInterface (const SimpleWireInterface &)=default
 
SimpleWireInterfaceoperator= (const SimpleWireInterface &)=default
 

Detailed Description

A software I2C implementation for sending LED segment patterns over I2C.

This has the same API has TwoWireInterface so it can be a drop-in replacement.

The implementation is very similar to the SimpleTmiInterface class (in the https://github.com/bxparks/AceTMI project) because the TM1637 protocol is very similar to I2C. To keep everything simple, all write and read methods are synchronous (i.e. blocking) because interrupts are not used. This means that we can eliminate the TX and RX buffers, which saves both flash and static memory.

During writing, beginTransmission() sends the START condition and the I2C address (along with the 'write' bit 0x00) right away. Then each write() call sends each byte immediately. The endTransmission() sends the STOP condition. There is no buffering of the data byte passed into the write() method.

During reading, the requestFrom() sends the START condition and the I2C address (along with the 'read' bit 0x01) right away. Then each call to read() returns the data byte from the slave. Before returning from read(), the master sends a NACK if total number of bytes read is not 'quantity', or an ACK if 'quantity' has just been read. A STOP condition is also sent after the last byte, if the 'sendStop' flag of requestFrom() was set to be true (default).

Definition at line 57 of file SimpleWireInterface.h.

Constructor & Destructor Documentation

◆ SimpleWireInterface()

ace_wire::SimpleWireInterface::SimpleWireInterface ( uint8_t  dataPin,
uint8_t  clockPin,
uint8_t  delayMicros 
)
inlineexplicit

Constructor.

The delayMicroseconds() may not be accurate for small values on some processors (e.g. AVR) . The actual minimum value of delayMicros will depend on the capacitance and resistance on the DATA and CLOCK lines, and the accuracy of the delayMicroseconds() function.

Parameters
dataPinSDA pin
clockPinSCL pin
delayMicrosdelay after each bit transition of SDA or SCL

Definition at line 72 of file SimpleWireInterface.h.

Member Function Documentation

◆ begin()

void ace_wire::SimpleWireInterface::begin ( ) const
inline

Initialize the clock and data pins.

These are open-drain lines, with pull-up resistors. We must not drive them HIGH actively since that could damage the transitor at the other end of the line pulling LOW. Instead, we go into INPUT mode to let the line to HIGH through the pullup resistor, then go to OUTPUT mode only to pull down.

Definition at line 88 of file SimpleWireInterface.h.

◆ beginTransmission()

uint8_t ace_wire::SimpleWireInterface::beginTransmission ( uint8_t  addr) const
inline

Send the I2C START condition.

Parameters
addrI2C address of slave device
Returns
0 if ACK, 1 if NACK

Definition at line 109 of file SimpleWireInterface.h.

◆ endTransmission()

uint8_t ace_wire::SimpleWireInterface::endTransmission ( bool  sendStop = true) const
inline

Send the I2C STOP condition.

Returns
always returns 0 to indicate success

Definition at line 158 of file SimpleWireInterface.h.

◆ read()

uint8_t ace_wire::SimpleWireInterface::read ( ) const
inline

Read byte.

After reading 8 bits, an ACK or NACK will be sent by the master to the slave. ACK means the slave will be asked to send more bytes so can hold control of the data line. NACK means no more bytes will be read from the slave and the slave should release the data line.

If requestFrom() was called with sendStop = true, a STOP condition will be sent after reading the final byte.

If called when the number of remaining bytes is 0 (which should not happen if the calling program is correctly implemented), this method returns immediately with a 0xff.

Definition at line 207 of file SimpleWireInterface.h.

◆ requestFrom()

uint8_t ace_wire::SimpleWireInterface::requestFrom ( uint8_t  addr,
uint8_t  quantity,
bool  sendStop = true 
) const
inline

Prepare to read bytes by sending I2C START condition.

If sendStop is true, then a STOP condition will be sent by read() after the last byte.

Returns
'quantity' if addr was written successfully and the device responded with ACK, 0 if device responded with NACK

Definition at line 176 of file SimpleWireInterface.h.

◆ write()

uint8_t ace_wire::SimpleWireInterface::write ( uint8_t  data) const
inline

Send the data byte on the data bus, with MSB first as specified by I2C.

This loop generates slightly asymmetric logic signals because clockLow() lasts for 2*bitDelay(), but clockHigh() lasts for only 1*bitDelay(). This does not seem to cause any problems with the LED modules that I have tested.

Returns
1 if successful with ACK, 0 for NACK.

Definition at line 132 of file SimpleWireInterface.h.


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