Skip to content

Commit cc4666a

Browse files
committed
Stay in power on mode, add light level reads to example.
1 parent 00d294a commit cc4666a

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

adafruit_tsl2591.py

+6-24
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
2929
* Author(s): Tony DiCola
3030
"""
31-
import time
32-
3331
from micropython import const
3432

3533
import adafruit_bus_device.i2c_device as i2c_device
@@ -96,9 +94,8 @@ def __init__(self, i2c, address=_TSL2591_ADDR):
9694
# Set default gain and integration times.
9795
self.gain = GAIN_MED
9896
self.integration_time = INTEGRATIONTIME_100MS
99-
# Put the device in a disabled state after initialization.
100-
# Future calls will enable and disable as necessary.
101-
self.disable()
97+
# Put the device in a powered on state after initialization.
98+
self.enable()
10299

103100
def _read_u8(self, address):
104101
# Read an 8-bit unsigned value from the specified 8-bit address.
@@ -149,24 +146,19 @@ def gain(self):
149146
- GAIN_HIGH (428x)
150147
- GAIN_MAX (9876x)
151148
"""
152-
self.enable()
153149
control = self._read_u8(_TSL2591_REGISTER_CONTROL)
154-
self.disable()
155150
return control & 0b00110000
156151

157152
@gain.setter
158153
def gain(self, val):
159154
assert val in (GAIN_LOW, GAIN_MED, GAIN_HIGH, GAIN_MAX)
160-
# Enable chip, set appropriate gain value.
161-
self.enable()
155+
# Set appropriate gain value.
162156
control = self._read_u8(_TSL2591_REGISTER_CONTROL)
163157
control &= 0b11001111
164158
control |= val
165159
self._write_u8(_TSL2591_REGISTER_CONTROL, control)
166160
# Keep track of gain for future lux calculations.
167161
self._gain = val
168-
# Go back to low power mode.
169-
self.disable()
170162

171163
@property
172164
def integration_time(self):
@@ -178,24 +170,19 @@ def integration_time(self):
178170
- INTEGRATIONTIME_500MS (500 millis)
179171
- INTEGRATIONTIME_600MS (600 millis)
180172
"""
181-
self.enable()
182173
control = self._read_u8(_TSL2591_REGISTER_CONTROL)
183-
self.disable()
184174
return control & 0b00000111
185175

186176
@integration_time.setter
187177
def integration_time(self, val):
188178
assert 0 <= val <= 5
189-
# Enable chip, set control bits appropriately.
190-
self.enable()
179+
# Set control bits appropriately.
191180
control = self._read_u8(_TSL2591_REGISTER_CONTROL)
192181
control &= 0b11111000
193182
control |= val
194183
self._write_u8(_TSL2591_REGISTER_CONTROL, control)
195184
# Keep track of integration time for future reading delay times.
196185
self._integration_time = val
197-
# Go back to low power mode.
198-
self.disable()
199186

200187
@property
201188
def raw_luminosity(self):
@@ -204,14 +191,9 @@ def raw_luminosity(self):
204191
is IR + visible luminosity (channel 0) and the second is the IR only
205192
(channel 1). Both values are 16-bit unsigned numbers (0-65535).
206193
"""
207-
# Enable then wait for the integration time to pass.
208-
self.enable()
209-
time.sleep(120.0 * self._integration_time / 1000.0)
210-
# Now read both the luminosity channels.
194+
# Read both the luminosity channels.
211195
channel_0 = self._read_u16LE(_TSL2591_REGISTER_CHAN0_LOW)
212196
channel_1 = self._read_u16LE(_TSL2591_REGISTER_CHAN1_LOW)
213-
# Go back to low power mode and return result.
214-
self.disable()
215197
return (channel_0, channel_1)
216198

217199
@property
@@ -231,7 +213,7 @@ def infrared(self):
231213

232214
@property
233215
def visible(self):
234-
"""Read the visible light and return its value as a 16-bit unsigned number.
216+
"""Read the visible light and return its value as a 32-bit unsigned number.
235217
"""
236218
channel_0, channel_1 = self.raw_luminosity
237219
full = (channel_1 << 16) | channel_0

examples/simpletest.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,21 @@
2525
#sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_500MS (500ms)
2626
#sensor.integration_time = adafruit_tsl2591.INTEGRATIONTIME_600MS (600ms)
2727

28-
# Read the lux and print it every second.
28+
# Read the total lux, IR, and visible light levels and print it every second.
2929
while True:
30+
# Read and calculate the light level in lux.
3031
lux = sensor.lux
31-
print('Light: {0}lux'.format(lux))
32+
print('Total light: {0}lux'.format(lux))
33+
# You can also read the raw infrared and visible light levels.
34+
# These are unsigned, the higher the number the more light of that type.
35+
# There are no units like lux.
36+
# Infrared levels range from 0-65535 (16-bit)
37+
infrared = sensor.infrared
38+
print('Infrared light: {0}'.format(infrared))
39+
# Visible-only levels range from 0-2147483647 (32-bit)
40+
visible = sensor.visible
41+
print('Visible light: {0}'.format(visible))
42+
# Full spectrum (visible + IR) also range from 0-2147483647 (32-bit)
43+
full_spectrum = sensor.full_spectrum
44+
print('Full spectrum (IR + visible) light: {0}'.format(full_spectrum))
3245
time.sleep(1.0)

0 commit comments

Comments
 (0)