RobTillaart / ADS1X15

Arduino library for ADS1015 = I2C 12 bit ADC and ADS1115 = I2C 16 bit ADC
MIT License
155 stars 29 forks source link

Refactor API proposal #84

Open RobTillaart opened 3 weeks ago

RobTillaart commented 3 weeks ago

in #82 a proposal is made to improve the API

  bool readADC(int &value)
Which returns false in case of errors and true as well as value when all is
Ok.

Users can then do
   if(readADC(value))
      handle value
   else
      handle error

An alternative could be

  int readADC(int &value)

return 0 if OK or the error code, so it does not need to be fetched with getError()
deKees687 commented 3 weeks ago

Perhaps most users are not interested in error handling. They just want results in the easiest way possible. For those users it is handy to keep readADC() that returns the adc value. Changing this function will break API and cause annoyance to existing users so I would keep that interface.

The longer timeout is no problem because that will never result in extra delays as long as all is OK. And it does not need any changes in user code anyway.

The alternative readADC(&Value) return 0 if OK, errorcode when error: This would make user code a little more complicated. It is no longer possible to put the call in an if() statement as you would want to preserve the error code. And you still need to call getError() if you want to clear the internal error code. So you get code like:

int Value;
int Rc = readADC(Value);
if(Rc == ADS1X15_OK )
   Handle value
else 
   Handle Error

So at the end, I would keep the interface as-is. Don't break user code. And update the examples to show how to use the API in the easy way and how to use it in a proper error-handling way.