pololu / zumo-shield-arduino-library

Arduino libraries for the Pololu Zumo Shield and Zumo robot kit for Arduino.
https://www.pololu.com/product/2508
MIT License
33 stars 17 forks source link

No need to wait after Wire.requestFrom() and no Wire.endTransmission() should be called after it. #1

Open Koepel opened 6 years ago

Koepel commented 6 years ago

In the files "LSM303.cpp" and "L3G.cpp" there is sometimes a Wire.endTransmission() after the Wire.requestFrom(). That Wire.endTransmission() can be removed, it should only be used when writing data.

The waiting or a timeout after a Wire.requestFrom() can be removed as well. When the Wire.requestFrom() returns, the I2C transaction on the bus has already completely finished and the received data is waiting in a buffer in the Wire library. If something did go wrong on the bus, the number of received bytes might be not the same as the number of requested bytes.

This:

  Wire.requestFrom(address, (byte)6);

  unsigned int millis_start = millis();
  while (Wire.available() < 6)
  {
    if (io_timeout > 0 && ((unsigned int)millis() - millis_start) > io_timeout)
    {
      did_timeout = true;
      return;
    }
  }

could be replaced with this:

  Wire.requestFrom(address, (byte)6);

  if (Wire.available() != 6)
  {
    set some kind of error
    return;
  }
ryantm commented 6 years ago

Hi, Koepel.

Thank you for your reminders about this issue with our libraries.

The files you mention are copied versions of our libraries. You raised this issue with the source libraries:

https://github.com/pololu/l3g-arduino/issues/12 https://github.com/pololu/lsm303-arduino/issues/10

I'll leave this open until we fix the issue in those libraries and copy them to this one.

Sincerely, Ryan