eschnou / arduino-px4flow-i2c

Arduino library wrapping the Wire I2C calls to a PX4Flow Optical Flow Tracker.
MIT License
63 stars 18 forks source link

No need to wait after Wire.requestFrom() #6

Open Koepel opened 7 years ago

Koepel commented 7 years ago

When the Wire.requestFrom() returns, the I2C transaction has completely finished and the received data is waiting in a buffer in the Wire library.

There is no need to wait for something after a call to Wire.requestFrom().

The function wait() in the file "arduino-px4flow-i2c/PX4Flow/PX4Flow.cpp" is not needed.

You can write your code like this:

  // request 22 bytes from the module
  Wire.requestFrom(PX4FLOW_ADDRESS, 22);    

  // read the data
  frame.frame_count = read16();
  ...

or something like this:

  // request 22 bytes from the module
  Wire.requestFrom(PX4FLOW_ADDRESS, 22);    

  if (wire.available() != 22) {
      return false;
  }

  // read the data
  frame.frame_count = read16();
  ...

Also draining the buffer (removing any remaining data) is not needed. The Wire library uses packets of data and starts with a cleared buffer before doing a I2C transaction.
There was a bug in the library for the SAMD processors that a buffer was not cleared, but that bug has been solved a year and a half ago.

eschnou commented 6 years ago

Thanks for your message. However I don't have access to a PX4Flow anymore and don't really maintain this project. If you have time to prepare a pull request with the changes, I'd be happy to merge it.