Skip to content

Commit

Permalink
add sample, add other properties and active/standby mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
ntavish committed Jul 8, 2019
1 parent 29ddcad commit e40bc49
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 88 deletions.
7 changes: 1 addition & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ python:
cache:
pip: true

# TODO: if deployment to PyPi is desired, change 'DEPLOY_PYPI' to "true",
# or remove the env block entirely and remove the condition in the
# deploy block.
env:
- DEPLOY_PYPI="false"

Expand All @@ -26,12 +23,10 @@ deploy:
overwrite: true
on:
tags: true
# TODO: Use 'travis encrypt --com -r adafruit/<repo slug>' to generate
# the encrypted password for adafruit-travis. Paste result below.
- provider: pypi
user: adafruit-travis
password:
secure: #-- PASTE ENCRYPTED PASSWORD HERE --#
secure: "uSEQHUsvjBuLSevEjGO0Su2mkxCw9HupzXkT4zEfyHSnIcegOMiZh+rAVWO9GSV+mRA3GmDxrb61Y8V6xHX8kc4oOoym6Zt3q/2WGUolHHJ8s8FdzjA6z6nVU2NPsFXda+NsUBz5X5wEpblZ82XF7agA4BSxy//CEZ4jhxlOHpaZfGO2ZeywDAfN4bPYB7T0JxCZ9qkLzS7DqB6PwFy2wE4b5jLIR1fi+Cengn12KBa1pASuRv0rrrBgTdhQ5D/acm3T57ZBLKCjE1S05kqGVjpkwWWjh8/znCeiYEwqIu8bkqJ6goz3pKkWzU8rKZTooUg/OFZ63TAMtpxRPdWgs560HccdI4zoSLTovnmrvUDk7Vkmc8QNUkYWSyRyWYJpO5je/LpsIUajACBjMp6yQ+iwbED54Uq4+uYVA2z+bn27EEr668+X39Lz1q/TJTIkgaA1Qt+IWYDmaDEY7hFZAOAfDp8ew45WZ3JQa5TAwgCIAxMT3JR87eyj4QitYFNQrlX0QMMLNhqZaPY+pNbRvsXO+zyevF43oDqtOh/VB+Dttaw1PFYQnmIbgDKQCx7Lc5Nbzta5TqOR7r9y2lyxy4W4P8Mp4Z2+2f8mBjZmzhgrP87ehdsFPEDwoPhbnoJQI19nW9b9l1YYoS4AZU25imrclAGOtG8f4d66lP/XlOY="
on:
tags: true
condition: $DEPLOY_PYPI = "true"
Expand Down
49 changes: 17 additions & 32 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
Introduction
============

.. image:: https://readthedocs.org/projects/electronutlabs-circuitpython-ltr329als01/badge/?version=latest
:target: https://circuitpython.readthedocs.io/projects/ltr329als01/en/latest/
.. image:: https://readthedocs.org/projects/circuitpython-ltr329als01-library/badge/?version=latest
:target: https://circuitpython-ltr329als01-library.readthedocs.io/en/latest/
:alt: Documentation Status

.. image:: https://img.shields.io/discord/327254708534116352.svg
:target: https://discord.gg/nBQh6qu
:alt: Discord

.. image:: https://travis-ci.com/electronut/Electronutlabs_CircuitPython_LTR329ALS01.svg?branch=master
:target: https://travis-ci.com/electronut/Electronutlabs_CircuitPython_LTR329ALS01
.. image:: https://api.travis-ci.org/electronut/Electronutlabs_CircuitPython_LTR329ALS01.svg?branch=master
:target: https://travis-ci.org/electronut/Electronutlabs_CircuitPython_LTR329ALS01/
:alt: Build Status

Circuitpython library for reading data from light sensor LTR329ALS01.
Expand All @@ -27,40 +27,25 @@ Please ensure all dependencies are available on the CircuitPython filesystem.
This is easily achieved by downloading
`the Adafruit library and driver bundle <https://github.com/adafruit/Adafruit_CircuitPython_Bundle>`_.

Installing from PyPI
=====================
.. note:: This library is not available on PyPI yet. Install documentation is included
as a standard element. Stay tuned for PyPI availability!

.. todo:: Remove the above note if PyPI version is/will be available at time of release.
If the library is not planned for PyPI, remove the entire 'Installing from PyPI' section.

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
PyPI <https://pypi.org/project/adafruit-circuitpython-ltr329als01/>`_. To install for current user:

.. code-block:: shell
pip3 install adafruit-circuitpython-ltr329als01
To install system-wide (this may be required in some cases):

.. code-block:: shell
Usage Example
=============

sudo pip3 install adafruit-circuitpython-ltr329als01
.. code-block:: python
To install in a virtual environment in your current project:
import time
import board
import busio
import electronutlabs_ltr329als01
.. code-block:: shell
i2c = busio.I2C(board.SCL, board.SDA)
ltr = electronutlabs_ltr329als01.LTR329ALS01(i2c)
mkdir project-name && cd project-name
python3 -m venv .env
source .env/bin/activate
pip3 install adafruit-circuitpython-ltr329als01
ltr.gain = electronutlabs_ltr329als01.CONST_GAIN_4X
Usage Example
=============
while True:
print("%.2f Lux" % (ltr.get_lux()))
time.sleep(1)
.. todo:: Add a quick, simple example. It and other examples should live in the examples folder and be included in docs/examples.rst.
Contributing
============
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# Uncomment the below if you use native CircuitPython modules such as
# digitalio, micropython and busio. List the modules you use. Without it, the
# autodoc module docs will fail to generate with a warning.
# autodoc_mock_imports = ["digitalio", "busio"]
autodoc_mock_imports = ["micropython", "busio"]


intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'BusDevice': ('https://circuitpython.readthedocs.io/projects/busdevice/en/latest/', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)}
Expand Down
7 changes: 2 additions & 5 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ Table of Contents
.. toctree::
:caption: Tutorials

.. todo:: Add any Learn guide links here. If there are none, then simply delete this todo and leave
the toctree above for use later.

.. toctree::
:caption: Related Products

.. todo:: Add any product links here. If there are none, then simply delete this todo and leave
the toctree above for use later.
* `Lite-On Digital ambient light sensor <https://www.mouser.com/ds/2/239/Lite-On_LTR-329ALS-01%20DS_ver1.1-348647.pdf>`_
* `Electronut Labs Blip <https://docs.electronut.in/blip/>`_

.. toctree::
:caption: Other Links
Expand Down
190 changes: 146 additions & 44 deletions electronutlabs_ltr329als01.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,17 @@
**Hardware:**
.. todo:: Add links to any specific hardware product page(s), or category page(s). Use unordered list & hyperlink rST
inline format: "* `Link Text <url>`_"
* `Lite-On Digital ambient light sensor \
<https://www.mouser.com/ds/2/239/Lite-On_LTR-329ALS-01%20DS_ver1.1-348647.pdf>`_
* `Electronut Labs Blip <https://docs.electronut.in/blip/>`_
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies based on the library's use of either.
# * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
# * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
"""

# imports
Expand Down Expand Up @@ -92,71 +91,171 @@
class LTR329ALS01:
"""Driver for the LTR329ALS01 ambient light sensor."""

def __init__(self, i2c, gain = CONST_GAIN_1X, integration = CONST_INT_100, rate = CONST_RATE_500):
def __init__(self, i2c, gain=CONST_GAIN_1X, integration=CONST_INT_100, rate=CONST_RATE_500):
import adafruit_bus_device.i2c_device as i2c_device
self._i2c = i2c_device.I2CDevice(i2c, LTR_ADDRESS)
self._buffer = bytearray(2)
self._gain = gain
self._integration = integration
self._rate = rate
self._active = 0x01

# just in case it we are just starting, choip needs 10ms after boot
time.sleep(0.1)

self._write_register_byte(REG_ALS_CONTR, self._get_contr(gain))
self._write_register_byte(REG_ALS_MEAS_RATE, self._get_meas_rate(integration, rate))
self._write_register_byte(
REG_ALS_MEAS_RATE, self._get_meas_rate(integration, rate))
self._write_register_byte(
REG_ALS_CONTR, self._get_contr(gain) | self._active)

time.sleep(0.01)

def _get_gain(self, gain):
if gain == CONST_GAIN_1X: return 1
if gain == CONST_GAIN_2X: return 2
if gain == CONST_GAIN_4X: return 4
if gain == CONST_GAIN_8X: return 8
if gain == CONST_GAIN_48X: return 48
if gain == CONST_GAIN_96X: return 96

def _get_integration(self, integration):
if integration == CONST_INT_50: return 50
if integration == CONST_INT_100: return 100
if integration == CONST_INT_150: return 150
if integration == CONST_INT_200: return 200
if integration == CONST_INT_250: return 250
if integration == CONST_INT_300: return 300
if integration == CONST_INT_350: return 350
if integration == CONST_INT_400: return 400

def _get_rate(self, rate):
if rate == CONST_RATE_50: return 50
if rate == CONST_RATE_100: return 100
if rate == CONST_RATE_200: return 200
if rate == CONST_RATE_500: return 500
if rate == CONST_RATE_1000: return 1000
if rate == CONST_RATE_2000: return 2000

def _get_contr(self, gain):
return ((gain & 0x07) << 2) + 0x01

def _get_meas_rate(self, integration, rate):
@property
def gain(self):
"""Gain of the sensor, possible values are CONST_RATE_50,
CONST_RATE_100, CONST_RATE_200, CONST_RATE_500, CONST_RATE_1000,
CONST_RATE_2000.
"""
return self._gain

@gain.setter
def gain(self, new_gain):
self._gain = new_gain
self._write_register_byte(
REG_ALS_CONTR, self._get_contr(new_gain) | self._active)

@property
def integration(self):
"""Integration time of the sensor, possible values are CONST_INT_50,
CONST_INT_100, CONST_INT_150, CONST_INT_200, CONST_INT_250,
CONST_INT_300, CONST_INT_350, CONST_INT_400.
"""
return self._integration

@integration.setter
def integration(self, new_integration):
self._integration = new_integration
self._write_register_byte(
REG_ALS_MEAS_RATE, self._get_meas_rate(self._integration, self._rate))

@property
def rate(self):
"""Measurement rate of sensor, possible values are CONST_RATE_50,
CONST_RATE_100, CONST_RATE_200, CONST_RATE_500, CONST_RATE_1000,
CONST_RATE_2000.
"""
return self._rate

@rate.setter
def rate(self, new_rate):
self._rate = new_rate
self._write_register_byte(
REG_ALS_MEAS_RATE, self._get_meas_rate(self._integration, self._rate))

def set_active(self):
"""Set sensor to active mode.
"""
self._active = 0x01
self._write_register_byte(
REG_ALS_CONTR, self._get_contr(self._gain) | self._active)

def set_standby(self):
"""Set sensor to standby mode.
"""
self._active = 0x00
self._write_register_byte(
REG_ALS_CONTR, self._get_contr(self._gain) | self._active)

@staticmethod
def _get_gain(gain):
retval = 0
if gain == CONST_GAIN_1X:
retval = 1
if gain == CONST_GAIN_2X:
retval = 2
if gain == CONST_GAIN_4X:
retval = 4
if gain == CONST_GAIN_8X:
retval = 8
if gain == CONST_GAIN_48X:
retval = 48
if gain == CONST_GAIN_96X:
retval = 96
return retval

@staticmethod
def _get_integration(integration):
retval = 0
if integration == CONST_INT_50:
retval = 50
if integration == CONST_INT_100:
retval = 100
if integration == CONST_INT_150:
retval = 150
if integration == CONST_INT_200:
retval = 200
if integration == CONST_INT_250:
retval = 250
if integration == CONST_INT_300:
retval = 300
if integration == CONST_INT_350:
retval = 350
if integration == CONST_INT_400:
retval = 400
return retval

@staticmethod
def _get_rate(rate):
retval = 0
if rate == CONST_RATE_50:
retval = 50
if rate == CONST_RATE_100:
retval = 100
if rate == CONST_RATE_200:
retval = 200
if rate == CONST_RATE_500:
retval = 500
if rate == CONST_RATE_1000:
retval = 1000
if rate == CONST_RATE_2000:
retval = 2000
return retval

@staticmethod
def _get_contr(gain):
return (gain & 0x07) << 2

@staticmethod
def _get_meas_rate(integration, rate):
return ((integration & 0x07) << 3) + (rate & 0x07)

def _read_register(self, register):
self._buffer[0] = register & 0xFF
with self._i2c as i2c:
i2c.write(self._buffer, start=0, end=1)
i2c.readinto(self._buffer, start=0, end=1)
return self._buffer[1]
return self._buffer[0]

def _write_register_byte(self, register, value):
self._buffer[0] = register & 0xFF
self._buffer[1] = value & 0xFF
with self._i2c as i2c:
i2c.write(self._buffer, start=0, end=2)

def _combine_word(self, high, low):

@staticmethod
def _combine_word(high, low):
return ((high & 0xFF) << 8) + (low & 0xFF)

def _valid_data(self):
status = self._read_register(REG_ALS_STATUS)
return bool(status & 0x04)

def get_lux(self):
"""Get luminance value in Lux.
"""
while not self._valid_data():
time.sleep(0.1)

ch1low = self._read_register(REG_ALS_DATA_CH1_0)
ch1high = self._read_register(REG_ALS_DATA_CH1_1)
data1 = int(self._combine_word(ch1high, ch1low))
Expand All @@ -173,12 +272,15 @@ def get_lux(self):
ratio = f_data1 / (f_data1 + f_data0)

if ratio < 0.45:
als_lux = (1.7743 * f_data0 + (1.1059 * f_data1)) / (als_gain * als_int)
als_lux = (1.7743 * f_data0 + (1.1059 * f_data1)) / \
(als_gain * als_int)
elif ratio < 0.64 and ratio >= 0.45:
als_lux = (4.2785 * f_data0 - (1.9548 * f_data1)) / (als_gain * als_int)
als_lux = (4.2785 * f_data0 - (1.9548 * f_data1)) / \
(als_gain * als_int)
elif ratio < 0.85 and ratio >= 0.64:
als_lux = (0.5926 * f_data0 + (0.1185 * f_data1)) / (als_gain * als_int)
als_lux = (0.5926 * f_data0 + (0.1185 * f_data1)) / \
(als_gain * als_int)
else:
als_lux = 0.0

return als_lux
13 changes: 13 additions & 0 deletions examples/ltr329als01_simpletest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import time
import board
import busio
import electronutlabs_ltr329als01

i2c = busio.I2C(board.SCL, board.SDA)
ltr = electronutlabs_ltr329als01.LTR329ALS01(i2c)

ltr.gain = electronutlabs_ltr329als01.CONST_GAIN_4X

while True:
print("%.2f Lux" % (ltr.get_lux()))
time.sleep(1)

0 comments on commit e40bc49

Please sign in to comment.