Hi everybody,
if you ever worked with I2C devices with Arduino using Wire you probably have written something like:
Wire.beginTransmission(DEVICE_ADDRESS);
Wire.send(REG_ADDRESS);
Wire.send(VAL_TO_WRITE);
Wire.endTransmission();
and
Wire.beginTransmission(DEVICE_ADDRESS);
Wire.send(REG_ADDRESS);
Wire.endTransmission();
Wire.beginTransmission(DEVICE_ADDRESS);
Wire.requestFrom(DEVICE_ADDRESS,NUM_BYTES_TO_READ);
int i = 0;
while(Wire.available()) { //ACC may send less than requested (abnormal)
buff[i] = Wire.receive(); // receive a byte
i++; }
Wire.endTransmission();
That's something very common on most of the device specific I2C Arduino libraries out there.
Unfortunately, when you use the above code (or similar) in more than two libraries for many devices you are actually wasting program memory as you have the above similar code blocks in all of your I2C libraries..
So, I'd like to push something like the above into Wire, the methods would be something as similar as:
TwoWire::writeTo(int dev_address, int reg_address, int data)
and
TwoWire::readFrom(int dev_address, int reg_address, int *buff, int quantity)
So, instead of using custom code to access those features, you would simply rely on the above methods.
Results will be a decrease in total program memory size (when using more than one I2C device) and a more readable and easier code.
I opened a bug report with a patch for this:
http://code.google.com/p/arduino/issues/detail?id=466Interested in hearing your opinions and suggestions,
Thanks,
Fabio Varesano