Go Down

Topic: New and growing well-documented, feature-complete I2C device library (Read 21 times) previous topic - next topic

retrolefty

Thanks Jeff;

Unfortunately the results appear to remain the same:

Quote

Initializing I2C devices...
Testing device connections...
I2C (0x48) reading 1 words from 0x0.... Done (0 read).
ADS1115 connection failed
I2C (0x48) reading 1 words from 0x1.... Done (0 read).
0
I2C (0x48) reading 2 bytes from 0x1...85 83 . Done (2 read).
2



Lefty

Jeff Rowberg


Thanks Jeff; Unfortunately the results appear to remain the same.


Drat. But I have good news! The results of successful readBytes() and unsuccessful readWords(), now that I've thought about the info staring me in the face, changed my assumption about always requesting the number of bytes needed, even when working with 16-bit registers. The readWords() method was requesting one byte (because I thought "one" on that hardware would actually mean one word), and therefore failing because it was only ever receiving one byte. I've adjusted that call to request length * 2 bytes and pushed the changes up. Try it now.

(Incidentally, if this doesn't work, I won't be able to try another fix until tomorrow...almost 2am here, heh.)

    Jeff

retrolefty

Quote
(Incidentally, if this doesn't work, I won't be able to try another fix until tomorrow...almost 2am here, heh.)

Don't knock yourself out, I'm sure you have a day job to deal with. Don't try to keep up with the hours us old retired guys can keep. ;)

The latest changes results in compiler errors:

Quote


C:\Documents and Settings\Primary Windows User\My Documents\Arduino\libraries\ADS1115\I2Cdev.cpp: In static member function 'static int8_t I2Cdev::readWords(uint8_t, uint8_t, uint8_t, uint16_t*, uint16_t)':
C:\Documents and Settings\Primary Windows User\My Documents\Arduino\libraries\ADS1115\I2Cdev.cpp:225: error: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
C:\Documents and Settings\Primary Windows User\My Documents\My Programs\Arduino\arduino-0022\libraries\Wire/Wire.h:53: note: candidate 1: uint8_t TwoWire::requestFrom(int, int)
C:\Documents and Settings\Primary Windows User\My Documents\My Programs\Arduino\arduino-0022\libraries\Wire/Wire.h:52: note: candidate 2: uint8_t TwoWire::requestFrom(uint8_t, uint8_t)



Thanks once again, I wish I was more competent with C++ classes and such.;
Lefty

retrolefty

Well progress it seems. I didn't really know what I was doing but your last change (the lenght * 2) did seem to be the source of the compiler errors, so I changed your latest change to this:

Code: [Select]

    Wire.beginTransmission(devAddr);
    //Wire.requestFrom(devAddr, length * 2); // length=words, this wants bytes, but length * 2 bombs 
                                                              // compiler with strange geek errors
    length = length * 2;                               // so put it on it's own line
    Wire.requestFrom(devAddr,length); // length=words, this wants bytes, and changed back to this, seems 
                                                     //to work!



And now when run it displays this:

Quote
Initializing I2C devices...
Testing device connections...
I2C (0x48) reading 1 words from 0x0...0 . Done (1 read).
ADS1115 connection successful
I2C (0x48) reading 1 words from 0x1...8583 . Done (1 read).
1
I2C (0x48) reading 2 bytes from 0x1...85 83 . Done (2 read).
2


So I'll try and use the higher order functions tomorrow and see if I can actually do some A/D conversions.

Thanks a million;

Lefty

Jeff Rowberg


Well progress it seems. I didn't really know what I was doing but your last change (the lenght * 2) did seem to be the source of the compiler errors...


Eureka! Good call on that fix, and I'm glad you have something to work with now. I did compile it in my avr-gcc-based project, which currently has an incomplete TwoWire implementation hacked together. It worked fine there, but I didn't test it in the Arduino sketch until this morning, at which point the error you mentioned showed up. The problem was that the TwoWire::requestFrom() method has at least two unique parameter lists (a.k.a. it's overloaded), which are:

Code: [Select]

TwoWire::requestFrom(uint8_t, uint8_t);
TwoWire::requestFrom(int, int);


I believe that second (int, int) one is equivalent to (int16_t, int16_t) using C99 types. Basically, one uses unsigned 8-bit integer, and one uses signed 16-bit integers. The call I have in the I2Cdev::readWords() method uses a specifically declared uint8_t length variable, which was fine up until I added that inline "length * 2" as an argument. If you take an 8-bit integer and multiply it by two, you might end up with a 9-bit value, which would need to be stored in a 16-bit integer container (since the only viable options are 8-bit and 16-bit, and it obviously won't fit in an 8-bit one). Once this potential data type conversion takes place, the function is called like this:

Code: [Select]

TwoWire::requestFrom(uint8_t, int);


...which is not one of the candidates, and requires another conversion of the first parameter to make it work. It could go in either direction, trying to fit (and potentially clip) a 9-bit unsigned integer into an 8-bit unsigned container (uint8_t, uint8_t), or it could expand the unsigned 8-bit first parameter into a larger signed 16-bit container (int, int). Apparently ISO C++ doesn't like to make such a choice arbitrarily.

So, because the length variable is really supposed to contain the number of words that you want, and your fix changes it permanently to the number of bytes, I left it the way I had it and typecast it back into an unsigned 8-bit value:

Code: [Select]

Wire.requestFrom(devAddr, (uint8_t)(length * 2)); // length=words, this wants bytes


Problem solved, and pushed to the repository. The only caveat here is that you can't request more than 127 words at a time. I have no idea whether this is a legitimate concern, but with the devices in the library so far, it isn't even remotely close to being a problem.

    Jeff

Go Up