Skip to content

Commit 1bd8bd5

Browse files
author
caternuson
committed
scotts tweaks
1 parent 3bb981f commit 1bd8bd5

File tree

1 file changed

+30
-35
lines changed

1 file changed

+30
-35
lines changed

adafruit_tsl2561.py

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,21 @@
2929
"""
3030
from adafruit_bus_device.i2c_device import I2CDevice
3131

32-
TSL2561_DEFAULT_ADDRESS = 0x39
33-
TSL2561_COMMAND_BIT = 0x80
34-
TSL2561_WORD_BIT = 0x20
32+
TSL2561_DEFAULT_ADDRESS = 0x39
33+
TSL2561_COMMAND_BIT = 0x80
34+
TSL2561_WORD_BIT = 0x20
3535

36-
TSL2561_CONTROL_POWERON = 0x03
37-
TSL2561_CONTROL_POWEROFF = 0x00
36+
TSL2561_CONTROL_POWERON = 0x03
37+
TSL2561_CONTROL_POWEROFF = 0x00
3838

39-
TSL2561_REGISTER_CONTROL = 0x00
40-
TSL2561_REGISTER_TIMING = 0x01
41-
TSL2561_REGISTER_CHAN0_LOW = 0x0C
42-
TSL2561_REGISTER_CHAN1_LOW = 0x0E
43-
TSL2561_REGISTER_ID = 0x0A
39+
TSL2561_REGISTER_CONTROL = 0x00
40+
TSL2561_REGISTER_TIMING = 0x01
41+
TSL2561_REGISTER_CHAN0_LOW = 0x0C
42+
TSL2561_REGISTER_CHAN1_LOW = 0x0E
43+
TSL2561_REGISTER_ID = 0x0A
4444

45-
TSL2561_SCALE = (1 / 0.034, 1 / 0.252, 1)
45+
TSL2561_GAIN_SCALE = (16, 1)
46+
TSL2561_TIME_SCALE = (1 / 0.034, 1 / 0.252, 1)
4647

4748
class TSL2561():
4849
"""Class which provides interface to TSL2561 light sensor."""
@@ -56,20 +57,17 @@ def __init__(self, address=TSL2561_DEFAULT_ADDRESS, i2c=None, **kwargs):
5657
self.i2c_device = I2CDevice(i2c, address)
5758

5859
@property
59-
def id(self, ):
60+
def id(self):
6061
"""A tuple containing the part number and the revision number."""
6162
id = self._read_register(TSL2561_REGISTER_ID)
6263
partno = (id >> 4 ) & 0x0f
6364
revno = id & 0x0f
6465
return (partno, revno)
6566

6667
@property
67-
def enabled(self, ):
68+
def enabled(self):
6869
"""The state of the sensor."""
69-
if self._read_register(TSL2561_REGISTER_CONTROL) & 0x03:
70-
return True
71-
else:
72-
return False
70+
return self._read_register(TSL2561_REGISTER_CONTROL) & 0x03
7371

7472
@enabled.setter
7573
def enabled(self, enable):
@@ -80,28 +78,28 @@ def enabled(self, enable):
8078
self._disable()
8179

8280
@property
83-
def lux(self, ):
81+
def light(self):
8482
"""The computed lux value."""
8583
return self._compute_lux()
8684

8785
@property
88-
def broadband(self, ):
86+
def broadband(self):
8987
"""The broadband channel value."""
9088
return self._read_broadband()
9189

9290
@property
93-
def infrared(self, ):
91+
def infrared(self):
9492
"""The infrared channel value."""
9593
return self._read_infrared()
9694

9795
@property
98-
def luminosity(self, ):
96+
def luminosity(self):
9997
"""The overall luminosity as a tuple containing the broadband
10098
channel and the infrared channel value."""
10199
return (self.broadband, self.infrared)
102100

103101
@property
104-
def gain(self, ):
102+
def gain(self):
105103
"""The gain. 0:1x, 1:16x."""
106104
return self._read_register(TSL2561_REGISTER_TIMING) >> 4 & 0x01
107105

@@ -117,7 +115,7 @@ def gain(self, value):
117115
i2c.write(self.buf, end=2)
118116

119117
@property
120-
def integration_time(self, ):
118+
def integration_time(self):
121119
"""The integration time. 0:13.7ms, 1:101ms, 2:402ms, or 3:manual"""
122120
current = self._read_register(TSL2561_REGISTER_TIMING)
123121
return current & 0x03
@@ -132,7 +130,7 @@ def integration_time(self, time):
132130
with self.i2c_device as i2c:
133131
i2c.write(self.buf, end=2)
134132

135-
def _compute_lux(self, ):
133+
def _compute_lux(self):
136134
"""Based on datasheet for FN package."""
137135
ch0, ch1 = self.luminosity
138136
if ch0 == 0: return 0
@@ -150,17 +148,16 @@ def _compute_lux(self, ):
150148
# Pretty sure the floating point math formula on pg. 23 of datasheet
151149
# is based on 16x gain and 402ms integration time. Need to scale
152150
# result for other settings.
153-
# correct for gain
154-
if not self.gain:
155-
lux *= 16
156-
# correct for integration time
157-
lux *= TSL2561_SCALE[self.integration_time]
151+
# Scale for gain.
152+
lux *= TSL2561_GAIN_SCALE[self.gain]
153+
# Scale for integration time.
154+
lux *= TSL2561_TIME_SCALE[self.integration_time]
158155
return lux
159156

160-
def _enable(self, ):
157+
def _enable(self):
161158
self._write_control_register(TSL2561_CONTROL_POWERON)
162159

163-
def _disable(self, ):
160+
def _disable(self):
164161
self._write_control_register(TSL2561_CONTROL_POWEROFF)
165162

166163
def _read_register(self, reg, count=1):
@@ -181,12 +178,10 @@ def _write_control_register(self, reg):
181178
with self.i2c_device as i2c:
182179
i2c.write(self.buf, end=2)
183180

184-
def _read_broadband(self, ):
185-
# *broadband = read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW);
181+
def _read_broadband(self):
186182
low, high = self._read_register(TSL2561_REGISTER_CHAN0_LOW, 2)
187183
return high << 8 | low
188184

189-
def _read_infrared(self, ):
190-
# *ir = read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW);
185+
def _read_infrared(self):
191186
low, high = self._read_register(TSL2561_REGISTER_CHAN1_LOW, 2)
192187
return high << 8 | low

0 commit comments

Comments
 (0)