pololu / lsm303-arduino

Arduino library for Pololu LSM303 boards
http://www.pololu.com/catalog/product/2124
MIT License
138 stars 137 forks source link

After Wire.requestFrom() there is no need for a timeout and no endTransmission. #10

Open Koepel opened 7 years ago

Koepel commented 7 years ago

In the functions readAccReg() and readMagReg(), there is a Wire.endTransmission() after the Wire.requestFrom(). That should not be there. The Wire.endTransmission() is only used when writing data. It is not a big problem, but the Wire.endTransmission() will sent the sensor address to the I2C bus and the sensor will aknowledge. It does no harm, but it is unnecessary.

In the functions readAcc() and readMag(), there is a timeout after Wire.requestFrom(). That timeout is not needed. The Wire.requestFrom() waits until the I2C transmission has completely finished. After that, the received data is in a buffer in the Wire library. The Wire.available() shows how many bytes there are in that buffer. The timeout is not needed, but in case there was a I2C bus error, it does detect that not all bytes are received and it does return with an error.

The return value of Wire.requestFrom() or the Wire.available() could be be used once to check if the correct number of bytes was received or if a bus error did occur.

Koepel commented 6 years ago

I just noticed this pull-request: https://github.com/pololu/lsm303-arduino/pull/2 A while-loop to wait for something after a Wire.requestFrom() is wrong and that was replaced with nonsense code with millis() and a timeout. Explanation: Common-mistakes#1