I built a shim library to translate some I2C function calls used in an vendor library that was meant for another system. All the library does is implement some basic types and translates the I2C function calls to Arduino Wire library calls. Seems pretty simple which is why I am wondering what went wrong and why I get a undefined reference error. Specifically the error is:
c:\Temp\build39237f3a2c84112751b4421a94afb949.tmp\libraries\MAX14661\MAX14661.cpp.o: In function `MAX14661::setAB(int, int)':
F:\Coding\Arduino\libraries\MAX14661/MAX14661.cpp:36: undefined reference to `I2C::write(int, char const*, int, bool)'
collect2.exe: error: ld returned 1 exit status
The library is broken into a few cpp and h files and has the following folder structure with everything in the libraries folder located at the same level as the sketch folders in the Arduino folder.
I know that this seems like overkill and that it would be easier to just rewrite the vendor library but there are many more that are just like those and I wanted to write a few shims to support a large number of those style libraries on Arduino without rewriting them all.
mbedI2C.cpp
#include "mbedI2C.h"
#include <Arduino.h>
#include <Wire.h>
/* Create a mbed style I2C interface
*
*/
I2C::I2C(PinName sda, PinName scl)
{
Wire.begin();
}
I2C::~I2C()
{
}
/* Read from an I2C slave
*
*/
int read(int address, char *data, int length, bool repeated)
{
int timeout = 0;
Wire.requestFrom(address, length);
while (length > 0 && timeout < 500) {
if (Wire.available()) {
*data = Wire.read();
data++;
length--;
timeout = 0;
}
delay(10);
timeout++;
}
return 0;
}
/* Write to an I2C slave
*
*/
int write(int address, const char *data, int length, bool repeated)
{
Wire.beginTransmission(address);
Wire.write(data, length);
Wire.endTransmission();
return 0;
}