lux_sensor.py 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. # Copyright 2016 by MPI-SWS and Data-Ken Research.
  2. # Licensed under the Apache 2.0 License.
  3. """
  4. This is a sensor for the tsl2591 lux (light level) sensor breakout board
  5. from Adafruit. It is a thin layer on top of python-tsl2591.
  6. To install the tsl2591 library:
  7. sudo apt-get install build-essential libi2c-dev i2c-tools python-dev libffi-dev
  8. sudo /usr/bin/pip install cffi
  9. git clone https://github.com/maxlklaxl/python-tsl2591.git
  10. """
  11. import tsl2591
  12. class LuxSensor:
  13. tsl = None
  14. def __init__(self, sensor_id=1):
  15. self.sensor_id = sensor_id
  16. if LuxSensor.tsl is None:
  17. LuxSensor.tsl = tsl2591.Tsl2591() # initialize the sensor context
  18. def sample(self):
  19. """Read the tsl2591 lux sensor and dispatch the luminosity value.
  20. """
  21. full, ir = LuxSensor.tsl.get_full_luminosity()
  22. lux = LuxSensor.tsl.calculate_lux(full, ir)
  23. # we convert the lux value to an integer - fractions are not
  24. # significant.
  25. return int(round(lux, 0))
  26. def __str__(self):
  27. return 'LuxSensor(%s)' % self.sensor_id