I2Cwrapper v0.5.0
Generic framework for Arduino I2C target devices
I2Cwrapper Class Reference

A helper class for the AccelStepperI2C and related libraries. More...

Detailed Description

A helper class for the AccelStepperI2C and related libraries.

An object of type I2Cwrapper represents an I2C target and handles all communication with it. AccelStepperI2C, ServoI2C, and other modules use it for communicating with the target. To do so, an I2Cwrapper object is instantiated with the target's I2C address and then passed to its client object's (AccelStepperI2C etc.) constructor.

Command codes
New classes can use I2Cwrapper to easily add even more capabilities to the target, e.g. for driving DC motors, granted that the firmware is extended accordingly and the above list of uint8_t command codes is kept free from duplicates.

Public Member Functions

 I2Cwrapper (uint8_t i2c_address, uint8_t maxBuf=I2CmaxBuf)
 Constructor. More...
 
bool ping ()
 Test if target device is listening. More...
 
void reset (unsigned long resetDelay=defaultResetDelay)
 Tells the target device to reset to its default state. It is recommended to reset the target every time the controller is started or restarted to put the target in a defined state. Otherwise it could happen that the target still manages units (steppers, etc.) which the controller does not know about. More...
 
void changeI2Caddress (uint8_t newAddress)
 Permanently change the I2C address of the device. The new address is stored in EEPROM (AVR) or flash memory (ESPs) and will be active after the next reset/reboot. More...
 
void setInterruptPin (int8_t pin, bool activeHigh=true)
 Define a global interrupt pin which can be used by device units (steppers, servos...) to inform the controller that something important happend. Currently used by AccelStepperI2C to inform about end stop hits and target reached events, and ESP32sensorsI2C to inform a touched button event. More...
 
uint8_t clearInterrupt ()
 Acknowledge to target that interrupt has been received, so that the target can clear the interupt condition and return the reason for the interrupt. More...
 
unsigned long setI2Cdelay (unsigned long delay)
 Define a minimum duration of time that the controller keeps between I2C transmissions. This is to make sure that the target has finished its earlier task or has its answer to the controller's previous command ready (see Adjusting the I2C delay). The actual delay will take the time spent since the last I2C transmission into account, so that it won't wait at all if the given time has already passed. More...
 
unsigned long getI2Cdelay ()
 Returns currently set I2Cdelay. More...
 
uint8_t autoAdjustI2Cdelay (uint8_t maxLength=I2CmaxBuf - 3, uint8_t safetyMargin=2, uint8_t startWith=I2CdefaultDelay)
 Set I2C delay to the smallest value that still allows error free transmissions. To determine this value, a simulation test is run: A number of dummy transmissions are repeatedly exchanged with the target using the pingBack() function, while successively decreasing the I2C delay. The test will stop as soon as a transmission error occurs. The smallest error free value (or 0 ms), increased by the safety margin, will be set as the new I2C delay. More...
 
uint32_t getVersion ()
 Get semver compliant version of target firmware. More...
 
bool checkVersion (uint32_t controllerVersion)
 Get version of target firmware and compare it with library version. More...
 
uint16_t sentErrors ()
 Return and reset the number of failed commands sent since the last time this method was used. A command is sent each time a function call is transmitted to the target. More...
 
uint16_t resultErrors ()
 Return and reset the number of failed receive events since the last time this method was used. A receive event happens each time a function returns a value from the target. More...
 
uint16_t transmissionErrors ()
 Return and reset the sum of failed commands sent and failed receive events since the last time this method was used. Use this if you are only interested in the sum of all transmission errors, not in what direction the errors occurred. More...
 
void prepareCommand (uint8_t cmd, uint8_t unit=-1)
 
bool sendCommand ()
 
bool readResult (uint8_t numBytes)
 

Public Attributes

SimpleBuffer buf
 
bool sentOK = false
 True if previous function call was successfully transferred to target. More...
 
bool resultOK = false
 True if return value from previous function call was received successfully. More...
 

Constructor & Destructor Documentation

◆ I2Cwrapper()

I2Cwrapper::I2Cwrapper ( uint8_t  i2c_address,
uint8_t  maxBuf = I2CmaxBuf 
)

Constructor.

Parameters
i2c_addressAddress of the target device
maxBuf[not implemented yet in the firmware, don't use] Upper limit of send and receive buffer including 1 crc8 byte and, for transmissions from the controller to the target, 2 header bytes. Note that this is corrently only implemented for the controller, but not in the firmware, so that it is not usable yet.

Member Function Documentation

◆ autoAdjustI2Cdelay()

uint8_t I2Cwrapper::autoAdjustI2Cdelay ( uint8_t  maxLength = I2CmaxBuf - 3,
uint8_t  safetyMargin = 2,
uint8_t  startWith = I2CdefaultDelay 
)

Set I2C delay to the smallest value that still allows error free transmissions. To determine this value, a simulation test is run: A number of dummy transmissions are repeatedly exchanged with the target using the pingBack() function, while successively decreasing the I2C delay. The test will stop as soon as a transmission error occurs. The smallest error free value (or 0 ms), increased by the safety margin, will be set as the new I2C delay.

Parameters
maxLengthNumber of simulated test parameter bytes sent with each testing transmission, defaulting to the maximum bytes that are possible with the given I2C buffer size. This theoretical maximum (I2CmaxBuf minus three bytes for the message header) will not be fully used by most modules. So if you know what the maximum number of parameter bytes sent or receiced by any of the commands you will use in your project is, you can specify it here to get a more aggressive, shorter I2C delay. Leave it to the default to be on the safe side, in most cases it will not make a significant difference.
safetyMarginA number of microseconds that will be added to the empirically determined minimum I2C delay. As the test transmissions do nothing but send back the amount of specified simulated parameter bytes, you will want to specify some extra time to allow for the time the controller will need to process any given command on top of the transmission itself. Its optimal value fully depends on how fast the target's module(s) do their job. It should usually be at least 1 ms. For the included modules 2 ms are a safe bet, that's why it's used as the default.
startWithThe delay value in ms to start with, defaults to I2CdefaultDelay (20 ms). Mainly meant to be used if serial debugging is enabled in the target firmware. If there is heavy debugging output, the default I2CdefaultDelay may sometimes be too low.
Note
new in v0.3.0, experimental
Returns
The newly set I2C delay
See also
setI2Cdelay()

◆ changeI2Caddress()

void I2Cwrapper::changeI2Caddress ( uint8_t  newAddress)

Permanently change the I2C address of the device. The new address is stored in EEPROM (AVR) or flash memory (ESPs) and will be active after the next reset/reboot.

Note
(since v0.3.0) This command needs a target with the _addressFromFlash_firmware.h feature module activated. Without it,the target will just ignore this command.
See also
reset()

◆ checkVersion()

bool I2Cwrapper::checkVersion ( uint32_t  controllerVersion)

Get version of target firmware and compare it with library version.

Returns
true if both versions match, i.a. are compatible.

◆ clearInterrupt()

uint8_t I2Cwrapper::clearInterrupt ( )

Acknowledge to target that interrupt has been received, so that the target can clear the interupt condition and return the reason for the interrupt.

Returns
Reason for the interrupt as 8bit BCD with triggering unit in lower 4 bits and trigger reason in the upper 4 bits. 0xff in case of error.
See also
List of possible reasons an interrupt was triggered.

◆ getI2Cdelay()

unsigned long I2Cwrapper::getI2Cdelay ( )

Returns currently set I2Cdelay.

◆ getVersion()

uint32_t I2Cwrapper::getVersion ( )

Get semver compliant version of target firmware.

Returns
major version in bits 0-7, minor version in bits 8-15; patch version in bits 16-23; 0xFFFFFFFF on error.

◆ ping()

bool I2Cwrapper::ping ( )

Test if target device is listening.

Returns
true if target could be found under the given address.

◆ prepareCommand()

void I2Cwrapper::prepareCommand ( uint8_t  cmd,
uint8_t  unit = -1 
)

◆ readResult()

bool I2Cwrapper::readResult ( uint8_t  numBytes)

◆ reset()

void I2Cwrapper::reset ( unsigned long  resetDelay = defaultResetDelay)

Tells the target device to reset to its default state. It is recommended to reset the target every time the controller is started or restarted to put the target in a defined state. Otherwise it could happen that the target still manages units (steppers, etc.) which the controller does not know about.

Parameters
resetDelay(new in v0.3.0, optional) delay in ms that the controller waits after sending the reset command to give the target enough time to reinitialize the firmware core and the activated modules. Defaults to defaultResetDelay (100 ms), 10 ms would probably be more than enough for all current modules.

◆ resultErrors()

uint16_t I2Cwrapper::resultErrors ( )

Return and reset the number of failed receive events since the last time this method was used. A receive event happens each time a function returns a value from the target.

See also
sentErrors(), transmissionErrors()

◆ sendCommand()

bool I2Cwrapper::sendCommand ( )

◆ sentErrors()

uint16_t I2Cwrapper::sentErrors ( )

Return and reset the number of failed commands sent since the last time this method was used. A command is sent each time a function call is transmitted to the target.

See also
resultErrors(), transmissionErrors()

◆ setI2Cdelay()

unsigned long I2Cwrapper::setI2Cdelay ( unsigned long  delay)

Define a minimum duration of time that the controller keeps between I2C transmissions. This is to make sure that the target has finished its earlier task or has its answer to the controller's previous command ready (see Adjusting the I2C delay). The actual delay will take the time spent since the last I2C transmission into account, so that it won't wait at all if the given time has already passed.

Parameters
delayMinimum time in between I2C transmissions in milliseconds. The default I2CdefaultDelay is a bit conservative at 20 ms to allow for serial debugging output to slow things down. 4 to 6 ms is usually more than enough.
Returns
Returns the previously set delay.
See also
autoAdjustI2Cdelay()

◆ setInterruptPin()

void I2Cwrapper::setInterruptPin ( int8_t  pin,
bool  activeHigh = true 
)

Define a global interrupt pin which can be used by device units (steppers, servos...) to inform the controller that something important happend. Currently used by AccelStepperI2C to inform about end stop hits and target reached events, and ESP32sensorsI2C to inform a touched button event.

Parameters
pinPin the target will use to send out interrupts.
activeHighIf true, HIGH will signal an interrupt.
See also
AccelStepperI2C::enableInterrupts()

◆ transmissionErrors()

uint16_t I2Cwrapper::transmissionErrors ( )

Return and reset the sum of failed commands sent and failed receive events since the last time this method was used. Use this if you are only interested in the sum of all transmission errors, not in what direction the errors occurred.

Returns
Sum of sentErrors() and resultErrors()
See also
sentErrors(), resultErrors()

Member Data Documentation

◆ buf

SimpleBuffer I2Cwrapper::buf

◆ resultOK

bool I2Cwrapper::resultOK = false

True if return value from previous function call was received successfully.

◆ sentOK

bool I2Cwrapper::sentOK = false

True if previous function call was successfully transferred to target.


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