Pages: [1]   Go Down
Author Topic: Help us push writeTo and readFrom into Wire  (Read 1642 times)
0 Members and 1 Guest are viewing this topic.
Torino, Italy
Offline Offline
Sr. Member
****
Karma: 2
Posts: 309
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everybody,

if you ever worked with I2C devices with Arduino using Wire you probably have written something like:

Code:
Wire.beginTransmission(DEVICE_ADDRESS);
  Wire.send(REG_ADDRESS);
  Wire.send(VAL_TO_WRITE);
  Wire.endTransmission();

and

Code:
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:
Code:
TwoWire::writeTo(int dev_address, int reg_address, int data)
and
Code:
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=466

Interested in hearing your opinions and suggestions,


Thanks,

Fabio Varesano
Logged

France
Offline Offline
God Member
*****
Karma: 11
Posts: 637
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

+1  smiley-wink
Logged


nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8472
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Sounds reasonable, you can wrap in your own function but this seems a good thing to build into the class.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Worcester, MA
Offline Offline
God Member
*****
Karma: 3
Posts: 623
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

+1
If we're doing a Wire rewrite can someone sneak in the function for Repeated Starts so it's compatible with the rest of the I2C devices out there?  smiley-cry
Logged

I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

Worcester, MA
Offline Offline
God Member
*****
Karma: 3
Posts: 623
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.

You know what the funny thing is...this is exactly how it's done in the TWI library which is called by the Wire library! Kind of makes you wonder why they wrote the Wire library in the first place.  I wonder if you can attach the Wire library and just send the functions directly to TWI...I'll have to try that later.
Logged

I2C GPS Shield

Checkout my Open Source GPS Tracker on Kickstarter

Torino, Italy
Offline Offline
Sr. Member
****
Karma: 2
Posts: 309
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You are right.. so probably I should use twi_readFrom() and twi_writeTo() as backend for TwoWire::readFrom() and TwoWire::writeTo() ..
I'll give it a try if I find some time.
Logged

Torino, Italy
Offline Offline
Sr. Member
****
Karma: 2
Posts: 309
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, guys.. thanks everybody for your comment and suggestions. We finally got attention from David and he agreed to push this into Arduino 023.

Now, please if you have ideas or suggestions have a look at http://code.google.com/p/arduino/issues/detail?id=466

Thank you,

Fabio
Logged

Pages: [1]   Go Up
Jump to: