29
29
"""
30
30
from adafruit_bus_device .i2c_device import I2CDevice
31
31
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
35
35
36
- TSL2561_CONTROL_POWERON = 0x03
37
- TSL2561_CONTROL_POWEROFF = 0x00
36
+ TSL2561_CONTROL_POWERON = 0x03
37
+ TSL2561_CONTROL_POWEROFF = 0x00
38
38
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
44
44
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 )
46
47
47
48
class TSL2561 ():
48
49
"""Class which provides interface to TSL2561 light sensor."""
@@ -56,20 +57,17 @@ def __init__(self, address=TSL2561_DEFAULT_ADDRESS, i2c=None, **kwargs):
56
57
self .i2c_device = I2CDevice (i2c , address )
57
58
58
59
@property
59
- def id (self , ):
60
+ def id (self ):
60
61
"""A tuple containing the part number and the revision number."""
61
62
id = self ._read_register (TSL2561_REGISTER_ID )
62
63
partno = (id >> 4 ) & 0x0f
63
64
revno = id & 0x0f
64
65
return (partno , revno )
65
66
66
67
@property
67
- def enabled (self , ):
68
+ def enabled (self ):
68
69
"""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
73
71
74
72
@enabled .setter
75
73
def enabled (self , enable ):
@@ -80,28 +78,28 @@ def enabled(self, enable):
80
78
self ._disable ()
81
79
82
80
@property
83
- def lux (self , ):
81
+ def light (self ):
84
82
"""The computed lux value."""
85
83
return self ._compute_lux ()
86
84
87
85
@property
88
- def broadband (self , ):
86
+ def broadband (self ):
89
87
"""The broadband channel value."""
90
88
return self ._read_broadband ()
91
89
92
90
@property
93
- def infrared (self , ):
91
+ def infrared (self ):
94
92
"""The infrared channel value."""
95
93
return self ._read_infrared ()
96
94
97
95
@property
98
- def luminosity (self , ):
96
+ def luminosity (self ):
99
97
"""The overall luminosity as a tuple containing the broadband
100
98
channel and the infrared channel value."""
101
99
return (self .broadband , self .infrared )
102
100
103
101
@property
104
- def gain (self , ):
102
+ def gain (self ):
105
103
"""The gain. 0:1x, 1:16x."""
106
104
return self ._read_register (TSL2561_REGISTER_TIMING ) >> 4 & 0x01
107
105
@@ -117,7 +115,7 @@ def gain(self, value):
117
115
i2c .write (self .buf , end = 2 )
118
116
119
117
@property
120
- def integration_time (self , ):
118
+ def integration_time (self ):
121
119
"""The integration time. 0:13.7ms, 1:101ms, 2:402ms, or 3:manual"""
122
120
current = self ._read_register (TSL2561_REGISTER_TIMING )
123
121
return current & 0x03
@@ -132,7 +130,7 @@ def integration_time(self, time):
132
130
with self .i2c_device as i2c :
133
131
i2c .write (self .buf , end = 2 )
134
132
135
- def _compute_lux (self , ):
133
+ def _compute_lux (self ):
136
134
"""Based on datasheet for FN package."""
137
135
ch0 , ch1 = self .luminosity
138
136
if ch0 == 0 : return 0
@@ -150,17 +148,16 @@ def _compute_lux(self, ):
150
148
# Pretty sure the floating point math formula on pg. 23 of datasheet
151
149
# is based on 16x gain and 402ms integration time. Need to scale
152
150
# 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 ]
158
155
return lux
159
156
160
- def _enable (self , ):
157
+ def _enable (self ):
161
158
self ._write_control_register (TSL2561_CONTROL_POWERON )
162
159
163
- def _disable (self , ):
160
+ def _disable (self ):
164
161
self ._write_control_register (TSL2561_CONTROL_POWEROFF )
165
162
166
163
def _read_register (self , reg , count = 1 ):
@@ -181,12 +178,10 @@ def _write_control_register(self, reg):
181
178
with self .i2c_device as i2c :
182
179
i2c .write (self .buf , end = 2 )
183
180
184
- def _read_broadband (self , ):
185
- # *broadband = read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW);
181
+ def _read_broadband (self ):
186
182
low , high = self ._read_register (TSL2561_REGISTER_CHAN0_LOW , 2 )
187
183
return high << 8 | low
188
184
189
- def _read_infrared (self , ):
190
- # *ir = read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW);
185
+ def _read_infrared (self ):
191
186
low , high = self ._read_register (TSL2561_REGISTER_CHAN1_LOW , 2 )
192
187
return high << 8 | low
0 commit comments