Re: TSL2561 and Raspberry Pi
by csatt on Fri Dec 11, 2015 9:54 pm
Here's my comparison of the two proposed Adafruit_TSL2561 modules.
APIs:
- The IainColledge module has an API that maps directly to the API
for the Arduino C++ code
- The IainColledge module provides class methods to set modes,
configure the device, access measurements, etc., after object
creation
- The asciiphil module allows more configuration at object creation
time (i.e. its constructor method has more parameters)
- The asciiphil module provides class properties (as opposed to
methods) to set modes, configure the device, access measurements,
etc., at runtime
==> I think there's some benefit in having an API that is similar
to the Arduino code since there may be cases where code is
ported from one platform to the other.
Math:
- The IainColledge module uses the integer math approximation from
the data sheet for the lux calculation ("Simplified Lux
Calculation")
- The asciiphil module uses the floating point math equations from
the data sheet for the lux calculation
==> The floating point math should be more accurate, BUT the
difference is in the noise compared to the +-40% accuracy of
the sensor itself (see last entry in table on data sheet p.4),
so this is really a wash
Coding style:
- The IainColledge module is not very object-oriented (only class is
the Adafruit_TSL2651 class itself)
- The IainColledge module is not very "pythonic"; it is pretty
obvious that it is translated C++ code
- The asciiphil module is much more sophisticated python code. It is
truly object-oriented. It uses properties, decorators, docstrings,
dictionaries, indexed lookups, exceptions, etc. It has better error
checking.
==> The advantage of the IainColledge style is that it is easy to
understand even for a novice programmer, and that describes the
majority of this community. If the goal is to have code that is
easy for this community to understand, debug, and
modify/enhance then the IainColledge style is preferable. But
if the goal is to have code that this community might actually
learn to be better coders from, then the asciiphil style is
better.
Features:
- The IainColledge module can be run standalone
==> Not necessary, but nice. Easy to add.
- The IainColledge module has an auto-gain feature
==> The auto-gain feature is very useful, so I think it should be
added to the asciiphil implementation
- The asciiphil module has a "continuous mode" feature (which is
actually the default)
==> Not sure how important this is to most people and it uses more
power
- The IainColledge module returns a lux value of 0 when the sensor
saturates
==> This matches the Arduino code, but is undesirable (IMO) since
0 is a valid value. I'd prefer that it returned a negative
number.
- The asciiphil module throws an exception when the sensor saturates
==> This crashes the caller unless they know how to properly catch
and handle exceptions. This is ok, but there should be example
code for novice users
- The IainColledge module has an average lux calculation feature (not
in Arduino code)
==> Nice, but not necessary. Easy to add externally.
- The asciiphil module automatically determines the package type from
the ID register
==> Nice enhancement if anyone uses this code for anything other
than the Adafruit TSL2561 breakout
Conclusions:
- With the bug fixes both of these implementations work fine
- I do think adding automatic gain selection to the asciiphil
implementation is important
- Each (but especially the asciiphil version) could benefit by
including a module of example code showing how to:
- Instantiate the object
- Set modes
- Get the lux value
- Handle saturation cases
- Personally I would like to see both of them committed
(e.g. Adafruit_TSL2561.py and Adafruit_TSL2561_alt.py)
- Forced to choose, I would go with the IainColledge implementation
because I think having a direct map of the Arduino code could be
important for some people