AccelStepperI2C  v 0.1
I2C wrapper (and a bit more) for the AccelStepper Arduino library
I2Cwrapper.h
Go to the documentation of this file.
1 
17 #ifndef I2Cwrapper_h
18 #define I2Cwrapper_h
19 
20 #define DEBUG // uncomment for debug output to Serial (which has to be begun() in the main sketch)
21 
22 #include <SimpleBuffer.h>
23 #include "version.h"
24 
25 #ifdef DEBUG
26 #define log(...) Serial.print(__VA_ARGS__)
27 #else
28 #define log(...)
29 #endif // DEBUG
30 
31 
32 const uint16_t maxBufDefault = 20; // includes 1 byte for CRC8
33 
34 // ms to wait between I2C communication, can be changed by setI2Cdelay()
35 const uint16_t I2CdefaultDelay = 50;
36 
37 // I2C commands used by the wrapper
38 const uint8_t resetCmd = 241;
39 const uint8_t changeI2CaddressCmd = 242;
40 const uint8_t setInterruptPinCmd = 243;
41 const uint8_t clearInterruptCmd = 244; const uint8_t clearInterruptResult = 1; // 1 uint8_t
42 const uint8_t getVersionCmd = 245; const uint8_t getVersionResult = 4; // 1 uint32_t
43 
53 const uint8_t interruptReason_none = 0;
54 
55 /*****************************************************************************/
56 /*****************************************************************************/
57 
80 {
81 public:
82 
88  I2Cwrapper(uint8_t i2c_address, uint8_t maxBuf = maxBufDefault);
89 
94  bool ping();
95 
102  void reset();
103 
109  void changeI2Caddress(uint8_t newAddress);
110 
120  void setInterruptPin(int8_t pin, bool activeHigh = true);
121 
129  uint8_t clearInterrupt();
130 
131 
145  int16_t setI2Cdelay(int16_t delay);
146 
151  uint32_t getSlaveVersion();
152 
153 
158  bool checkVersion(uint32_t masterVersion);
159 
166  uint16_t sentErrors();
167 
174  uint16_t resultErrors();
175 
184  uint16_t transmissionErrors();
185 
186  void prepareCommand(uint8_t cmd, uint8_t unit = -1);
187  bool sendCommand();
188  bool readResult(uint8_t numBytes);
189 
191  bool sentOK = true;
192  bool resultOK = true;
193 
194 private:
195  void doDelay();
196  uint8_t address;
197  // ms to wait between I2C communication, can be changed by setI2Cdelay()
198  int16_t I2Cdelay = I2CdefaultDelay;
199  uint32_t lastI2Ctransmission = 0; // used to adjust I2Cdelay in doDelay()
200  uint16_t sentErrorsCount = 0; // Number of transmission errors. Will be reset to 0 by sentErrors().
201  uint16_t resultErrorsCount = 0; // Number of receiving errors. Will be reset to 0 by resultErrors().
202 };
203 
204 
205 
206 #endif
I2Cwrapper
A helper class for the AccelStepperI2C (and ServoI2C) library.
Definition: I2Cwrapper.h:79
I2Cwrapper::resultOK
bool resultOK
True if return value from previous function call was received successfully.
Definition: I2Cwrapper.h:192
I2Cwrapper::prepareCommand
void prepareCommand(uint8_t cmd, uint8_t unit=-1)
Definition: I2Cwrapper.cpp:34
I2Cwrapper::sentOK
bool sentOK
True if previous function call was successfully transferred to slave.
Definition: I2Cwrapper.h:191
I2Cwrapper::I2Cwrapper
I2Cwrapper(uint8_t i2c_address, uint8_t maxBuf=maxBufDefault)
Constructor.
Definition: I2Cwrapper.cpp:18
SimpleBuffer
Definition: SimpleBuffer.h:30
I2Cwrapper::clearInterrupt
uint8_t clearInterrupt()
Acknowledge a received interrupt to slave so that it can clear the interupt condition and return the ...
Definition: I2Cwrapper.cpp:137
I2Cwrapper::resultErrors
uint16_t resultErrors()
Return and reset the number of failed receive events since the last time this method was used....
Definition: I2Cwrapper.cpp:169
I2Cwrapper::buf
SimpleBuffer buf
Definition: I2Cwrapper.h:190
version.h
changeI2CaddressCmd
const uint8_t changeI2CaddressCmd
Definition: I2Cwrapper.h:39
I2Cwrapper::transmissionErrors
uint16_t transmissionErrors()
Return and reset the sum of failed commands sent and failed receive events since the last time this m...
Definition: I2Cwrapper.cpp:176
setInterruptPinCmd
const uint8_t setInterruptPinCmd
Definition: I2Cwrapper.h:40
I2Cwrapper::readResult
bool readResult(uint8_t numBytes)
Definition: I2Cwrapper.cpp:70
resetCmd
const uint8_t resetCmd
Definition: I2Cwrapper.h:38
I2Cwrapper::sentErrors
uint16_t sentErrors()
Return and reset the number of failed commands sent since the last time this method was used....
Definition: I2Cwrapper.cpp:162
I2Cwrapper::ping
bool ping()
Test if slave is listening.
Definition: I2Cwrapper.cpp:102
I2CdefaultDelay
const uint16_t I2CdefaultDelay
Definition: I2Cwrapper.h:35
SimpleBuffer.h
Simple and ugly serialization buffer for any data type. Template technique and CRC8 adapted from Nick...
interruptReason_none
const uint8_t interruptReason_none
You should not encounter this, as you don't want to be interrupted without a reason....
Definition: I2Cwrapper.h:53
I2Cwrapper::getSlaveVersion
uint32_t getSlaveVersion()
Get semver compliant version of slave firmware.
Definition: I2Cwrapper.cpp:147
maxBufDefault
const uint16_t maxBufDefault
Definition: I2Cwrapper.h:32
I2Cwrapper::changeI2Caddress
void changeI2Caddress(uint8_t newAddress)
Permanently change the I2C address of the device. New address is stored in EEPROM (AVR) or flash memo...
Definition: I2Cwrapper.cpp:114
newAddress
const uint8_t newAddress
Definition: Change_address.ino:17
getVersionResult
const uint8_t getVersionResult
Definition: I2Cwrapper.h:42
getVersionCmd
const uint8_t getVersionCmd
Definition: I2Cwrapper.h:42
clearInterruptResult
const uint8_t clearInterruptResult
Definition: I2Cwrapper.h:41
I2Cwrapper::setI2Cdelay
int16_t setI2Cdelay(int16_t delay)
Define a minimum time that the master keeps between I2C transmissions. This is to make sure that the ...
Definition: I2Cwrapper.cpp:121
clearInterruptCmd
const uint8_t clearInterruptCmd
Definition: I2Cwrapper.h:41
I2Cwrapper::reset
void reset()
Tells the slave to reset to it's default state. It is recommended to reset the slave every time the m...
Definition: I2Cwrapper.cpp:108
I2Cwrapper::checkVersion
bool checkVersion(uint32_t masterVersion)
Get version of slave firmware and compare it with library version.
Definition: I2Cwrapper.cpp:157
I2Cwrapper::sendCommand
bool sendCommand()
Definition: I2Cwrapper.cpp:46
I2Cwrapper::setInterruptPin
void setInterruptPin(int8_t pin, bool activeHigh=true)
Define a global interrupt pin which can be used by device units (steppers, servos....
Definition: I2Cwrapper.cpp:128