38
38
from Adafruit_I2C import Adafruit_I2C
39
39
40
40
class Adafruit_TSL2561 (Adafruit_I2C ):
41
- TSL2561_VISIBLE = 2 # channel 0 - channel 1
42
- TSL2561_INFRARED = 1 # channel 1
43
- TSL2561_FULLSPECTRUM = 0 # channel 0
41
+ TSL2561_VISIBLE = 2 # channel 0 - channel 1
42
+ TSL2561_INFRARED = 1 # channel 1
43
+ TSL2561_FULLSPECTRUM = 0 # channel 0
44
44
45
45
# I2C address options
46
- TSL2561_ADDR_LOW = 0x29
47
- TSL2561_ADDR_FLOAT = 0x39 # Default address (pin left floating)
48
- TSL2561_ADDR_HIGH = 0x49
46
+ TSL2561_ADDR_LOW = 0x29
47
+ TSL2561_ADDR_FLOAT = 0x39 # Default address (pin left floating)
48
+ TSL2561_ADDR_HIGH = 0x49
49
49
50
50
# Lux calculations differ slightly for CS package
51
- TSL2561_PACKAGE_CS = 0
52
- TSL2561_PACKAGE_T_FN_CL = 1
51
+ TSL2561_PACKAGE_CS = 0
52
+ TSL2561_PACKAGE_T_FN_CL = 1
53
53
54
- TSL2561_COMMAND_BIT = 0x80 # Must be 1
55
- TSL2561_CLEAR_BIT = 0x40 # Clears any pending interrupt (write 1 to clear)
56
- TSL2561_WORD_BIT = 0x20 # 1 = read/write word (rather than byte)
57
- TSL2561_BLOCK_BIT = 0x10 # 1 = using block read/write
54
+ TSL2561_COMMAND_BIT = 0x80 # Must be 1
55
+ TSL2561_CLEAR_BIT = 0x40 # Clears any pending interrupt (write 1 to clear)
56
+ TSL2561_WORD_BIT = 0x20 # 1 = read/write word (rather than byte)
57
+ TSL2561_BLOCK_BIT = 0x10 # 1 = using block read/write
58
58
59
- TSL2561_CONTROL_POWERON = 0x03
60
- TSL2561_CONTROL_POWEROFF = 0x00
59
+ TSL2561_CONTROL_POWERON = 0x03
60
+ TSL2561_CONTROL_POWEROFF = 0x00
61
61
62
- TSL2561_LUX_LUXSCALE = 14 # Scale by 2^14
63
- TSL2561_LUX_RATIOSCALE = 9 # Scale ratio by 2^9
64
- TSL2561_LUX_CHSCALE = 10 # Scale channel values by 2^10
65
- TSL2561_LUX_CHSCALE_TINT0 = 0x7517 # 322/11 * 2^TSL2561_LUX_CHSCALE
66
- TSL2561_LUX_CHSCALE_TINT1 = 0x0FE7 # 322/81 * 2^TSL2561_LUX_CHSCALE
62
+ TSL2561_LUX_LUXSCALE = 14 # Scale by 2^14
63
+ TSL2561_LUX_RATIOSCALE = 9 # Scale ratio by 2^9
64
+ TSL2561_LUX_CHSCALE = 10 # Scale channel values by 2^10
65
+ TSL2561_LUX_CHSCALE_TINT0 = 0x7517 # 322/11 * 2^TSL2561_LUX_CHSCALE
66
+ TSL2561_LUX_CHSCALE_TINT1 = 0x0FE7 # 322/81 * 2^TSL2561_LUX_CHSCALE
67
67
68
68
# T, FN and CL package values
69
- TSL2561_LUX_K1T = 0x0040 # 0.125 * 2^RATIO_SCALE
70
- TSL2561_LUX_B1T = 0x01f2 # 0.0304 * 2^LUX_SCALE
71
- TSL2561_LUX_M1T = 0x01be # 0.0272 * 2^LUX_SCALE
72
- TSL2561_LUX_K2T = 0x0080 # 0.250 * 2^RATIO_SCALE
73
- TSL2561_LUX_B2T = 0x0214 # 0.0325 * 2^LUX_SCALE
74
- TSL2561_LUX_M2T = 0x02d1 # 0.0440 * 2^LUX_SCALE
75
- TSL2561_LUX_K3T = 0x00c0 # 0.375 * 2^RATIO_SCALE
76
- TSL2561_LUX_B3T = 0x023f # 0.0351 * 2^LUX_SCALE
77
- TSL2561_LUX_M3T = 0x037b # 0.0544 * 2^LUX_SCALE
78
- TSL2561_LUX_K4T = 0x0100 # 0.50 * 2^RATIO_SCALE
79
- TSL2561_LUX_B4T = 0x0270 # 0.0381 * 2^LUX_SCALE
80
- TSL2561_LUX_M4T = 0x03fe # 0.0624 * 2^LUX_SCALE
81
- TSL2561_LUX_K5T = 0x0138 # 0.61 * 2^RATIO_SCALE
82
- TSL2561_LUX_B5T = 0x016f # 0.0224 * 2^LUX_SCALE
83
- TSL2561_LUX_M5T = 0x01fc # 0.0310 * 2^LUX_SCALE
84
- TSL2561_LUX_K6T = 0x019a # 0.80 * 2^RATIO_SCALE
85
- TSL2561_LUX_B6T = 0x00d2 # 0.0128 * 2^LUX_SCALE
86
- TSL2561_LUX_M6T = 0x00fb # 0.0153 * 2^LUX_SCALE
87
- TSL2561_LUX_K7T = 0x029a # 1.3 * 2^RATIO_SCALE
88
- TSL2561_LUX_B7T = 0x0018 # 0.00146 * 2^LUX_SCALE
89
- TSL2561_LUX_M7T = 0x0012 # 0.00112 * 2^LUX_SCALE
90
- TSL2561_LUX_K8T = 0x029a # 1.3 * 2^RATIO_SCALE
91
- TSL2561_LUX_B8T = 0x0000 # 0.000 * 2^LUX_SCALE
92
- TSL2561_LUX_M8T = 0x0000 # 0.000 * 2^LUX_SCALE
69
+ TSL2561_LUX_K1T = 0x0040 # 0.125 * 2^RATIO_SCALE
70
+ TSL2561_LUX_B1T = 0x01f2 # 0.0304 * 2^LUX_SCALE
71
+ TSL2561_LUX_M1T = 0x01be # 0.0272 * 2^LUX_SCALE
72
+ TSL2561_LUX_K2T = 0x0080 # 0.250 * 2^RATIO_SCALE
73
+ TSL2561_LUX_B2T = 0x0214 # 0.0325 * 2^LUX_SCALE
74
+ TSL2561_LUX_M2T = 0x02d1 # 0.0440 * 2^LUX_SCALE
75
+ TSL2561_LUX_K3T = 0x00c0 # 0.375 * 2^RATIO_SCALE
76
+ TSL2561_LUX_B3T = 0x023f # 0.0351 * 2^LUX_SCALE
77
+ TSL2561_LUX_M3T = 0x037b # 0.0544 * 2^LUX_SCALE
78
+ TSL2561_LUX_K4T = 0x0100 # 0.50 * 2^RATIO_SCALE
79
+ TSL2561_LUX_B4T = 0x0270 # 0.0381 * 2^LUX_SCALE
80
+ TSL2561_LUX_M4T = 0x03fe # 0.0624 * 2^LUX_SCALE
81
+ TSL2561_LUX_K5T = 0x0138 # 0.61 * 2^RATIO_SCALE
82
+ TSL2561_LUX_B5T = 0x016f # 0.0224 * 2^LUX_SCALE
83
+ TSL2561_LUX_M5T = 0x01fc # 0.0310 * 2^LUX_SCALE
84
+ TSL2561_LUX_K6T = 0x019a # 0.80 * 2^RATIO_SCALE
85
+ TSL2561_LUX_B6T = 0x00d2 # 0.0128 * 2^LUX_SCALE
86
+ TSL2561_LUX_M6T = 0x00fb # 0.0153 * 2^LUX_SCALE
87
+ TSL2561_LUX_K7T = 0x029a # 1.3 * 2^RATIO_SCALE
88
+ TSL2561_LUX_B7T = 0x0018 # 0.00146 * 2^LUX_SCALE
89
+ TSL2561_LUX_M7T = 0x0012 # 0.00112 * 2^LUX_SCALE
90
+ TSL2561_LUX_K8T = 0x029a # 1.3 * 2^RATIO_SCALE
91
+ TSL2561_LUX_B8T = 0x0000 # 0.000 * 2^LUX_SCALE
92
+ TSL2561_LUX_M8T = 0x0000 # 0.000 * 2^LUX_SCALE
93
93
94
94
# CS package values
95
- TSL2561_LUX_K1C = 0x0043 # 0.130 * 2^RATIO_SCALE
96
- TSL2561_LUX_B1C = 0x0204 # 0.0315 * 2^LUX_SCALE
97
- TSL2561_LUX_M1C = 0x01ad # 0.0262 * 2^LUX_SCALE
98
- TSL2561_LUX_K2C = 0x0085 # 0.260 * 2^RATIO_SCALE
99
- TSL2561_LUX_B2C = 0x0228 # 0.0337 * 2^LUX_SCALE
100
- TSL2561_LUX_M2C = 0x02c1 # 0.0430 * 2^LUX_SCALE
101
- TSL2561_LUX_K3C = 0x00c8 # 0.390 * 2^RATIO_SCALE
102
- TSL2561_LUX_B3C = 0x0253 # 0.0363 * 2^LUX_SCALE
103
- TSL2561_LUX_M3C = 0x0363 # 0.0529 * 2^LUX_SCALE
104
- TSL2561_LUX_K4C = 0x010a # 0.520 * 2^RATIO_SCALE
105
- TSL2561_LUX_B4C = 0x0282 # 0.0392 * 2^LUX_SCALE
106
- TSL2561_LUX_M4C = 0x03df # 0.0605 * 2^LUX_SCALE
107
- TSL2561_LUX_K5C = 0x014d # 0.65 * 2^RATIO_SCALE
108
- TSL2561_LUX_B5C = 0x0177 # 0.0229 * 2^LUX_SCALE
109
- TSL2561_LUX_M5C = 0x01dd # 0.0291 * 2^LUX_SCALE
110
- TSL2561_LUX_K6C = 0x019a # 0.80 * 2^RATIO_SCALE
111
- TSL2561_LUX_B6C = 0x0101 # 0.0157 * 2^LUX_SCALE
112
- TSL2561_LUX_M6C = 0x0127 # 0.0180 * 2^LUX_SCALE
113
- TSL2561_LUX_K7C = 0x029a # 1.3 * 2^RATIO_SCALE
114
- TSL2561_LUX_B7C = 0x0037 # 0.00338 * 2^LUX_SCALE
115
- TSL2561_LUX_M7C = 0x002b # 0.00260 * 2^LUX_SCALE
116
- TSL2561_LUX_K8C = 0x029a # 1.3 * 2^RATIO_SCALE
117
- TSL2561_LUX_B8C = 0x0000 # 0.000 * 2^LUX_SCALE
118
- TSL2561_LUX_M8C = 0x0000 # 0.000 * 2^LUX_SCALE
95
+ TSL2561_LUX_K1C = 0x0043 # 0.130 * 2^RATIO_SCALE
96
+ TSL2561_LUX_B1C = 0x0204 # 0.0315 * 2^LUX_SCALE
97
+ TSL2561_LUX_M1C = 0x01ad # 0.0262 * 2^LUX_SCALE
98
+ TSL2561_LUX_K2C = 0x0085 # 0.260 * 2^RATIO_SCALE
99
+ TSL2561_LUX_B2C = 0x0228 # 0.0337 * 2^LUX_SCALE
100
+ TSL2561_LUX_M2C = 0x02c1 # 0.0430 * 2^LUX_SCALE
101
+ TSL2561_LUX_K3C = 0x00c8 # 0.390 * 2^RATIO_SCALE
102
+ TSL2561_LUX_B3C = 0x0253 # 0.0363 * 2^LUX_SCALE
103
+ TSL2561_LUX_M3C = 0x0363 # 0.0529 * 2^LUX_SCALE
104
+ TSL2561_LUX_K4C = 0x010a # 0.520 * 2^RATIO_SCALE
105
+ TSL2561_LUX_B4C = 0x0282 # 0.0392 * 2^LUX_SCALE
106
+ TSL2561_LUX_M4C = 0x03df # 0.0605 * 2^LUX_SCALE
107
+ TSL2561_LUX_K5C = 0x014d # 0.65 * 2^RATIO_SCALE
108
+ TSL2561_LUX_B5C = 0x0177 # 0.0229 * 2^LUX_SCALE
109
+ TSL2561_LUX_M5C = 0x01dd # 0.0291 * 2^LUX_SCALE
110
+ TSL2561_LUX_K6C = 0x019a # 0.80 * 2^RATIO_SCALE
111
+ TSL2561_LUX_B6C = 0x0101 # 0.0157 * 2^LUX_SCALE
112
+ TSL2561_LUX_M6C = 0x0127 # 0.0180 * 2^LUX_SCALE
113
+ TSL2561_LUX_K7C = 0x029a # 1.3 * 2^RATIO_SCALE
114
+ TSL2561_LUX_B7C = 0x0037 # 0.00338 * 2^LUX_SCALE
115
+ TSL2561_LUX_M7C = 0x002b # 0.00260 * 2^LUX_SCALE
116
+ TSL2561_LUX_K8C = 0x029a # 1.3 * 2^RATIO_SCALE
117
+ TSL2561_LUX_B8C = 0x0000 # 0.000 * 2^LUX_SCALE
118
+ TSL2561_LUX_M8C = 0x0000 # 0.000 * 2^LUX_SCALE
119
119
120
120
# Auto-gain thresholds
121
- TSL2561_AGC_THI_13MS = 4850 # Max value at Ti 13ms = 5047
122
- TSL2561_AGC_TLO_13MS = 100
123
- TSL2561_AGC_THI_101MS = 36000 # Max value at Ti 101ms = 37177
124
- TSL2561_AGC_TLO_101MS = 200
125
- TSL2561_AGC_THI_402MS = 63000 # Max value at Ti 402ms = 65535
126
- TSL2561_AGC_TLO_402MS = 500
121
+ TSL2561_AGC_THI_13MS = 4850 # Max value at Ti 13ms = 5047
122
+ TSL2561_AGC_TLO_13MS = 100
123
+ TSL2561_AGC_THI_101MS = 36000 # Max value at Ti 101ms = 37177
124
+ TSL2561_AGC_TLO_101MS = 200
125
+ TSL2561_AGC_THI_402MS = 63000 # Max value at Ti 402ms = 65535
126
+ TSL2561_AGC_TLO_402MS = 500
127
127
128
128
# Clipping thresholds
129
- TSL2561_CLIPPING_13MS = 4900
130
- TSL2561_CLIPPING_101MS = 37000
131
- TSL2561_CLIPPING_402MS = 65000
129
+ TSL2561_CLIPPING_13MS = 4900
130
+ TSL2561_CLIPPING_101MS = 37000
131
+ TSL2561_CLIPPING_402MS = 65000
132
132
133
133
TSL2561_REGISTER_CONTROL = 0x00
134
134
TSL2561_REGISTER_TIMING = 0x01
@@ -160,51 +160,51 @@ class Adafruit_TSL2561(Adafruit_I2C):
160
160
161
161
162
162
163
- #**************************************************************************/
163
+ #**************************************************************************
164
164
# Writes a register and an 8 bit value over I2C
165
- #**************************************************************************/
165
+ #**************************************************************************
166
166
def write8 (self , reg , value ):
167
167
if (self ._debug == True ): print "write8"
168
168
self ._i2c .write8 (reg , value )
169
169
if (self ._debug == True ): print "write8_end"
170
170
171
- #**************************************************************************/
171
+ #**************************************************************************
172
172
# Reads an 8 bit value over I2C
173
- #**************************************************************************/
173
+ #**************************************************************************
174
174
def read8 (self , reg ):
175
175
if (self ._debug == True ): print "read8"
176
176
return self ._i2c .readU8 (reg )
177
177
if (self ._debug == True ): print "read8_end"
178
178
179
- #**************************************************************************/
179
+ #**************************************************************************
180
180
# Reads a 16 bit values over I2C
181
- #**************************************************************************/
181
+ #**************************************************************************
182
182
def read16 (self , reg ):
183
183
if (self ._debug == True ): print "read16"
184
184
return self ._i2c .readU16 (reg )
185
185
if (self ._debug == True ): print "read16_end"
186
186
187
- #**************************************************************************/
187
+ #**************************************************************************
188
188
# Enables the device
189
- #**************************************************************************/
189
+ #**************************************************************************
190
190
def enable (self ):
191
191
if (self ._debug == True ): print "enable"
192
192
# Enable the device by setting the control bit to 0x03 */
193
193
self ._i2c .write8 (self .TSL2561_COMMAND_BIT | self .TSL2561_REGISTER_CONTROL , self .TSL2561_CONTROL_POWERON )
194
194
if (self ._debug == True ): print "enable_end"
195
195
196
- #**************************************************************************/
196
+ #**************************************************************************
197
197
# Disables the device (putting it in lower power sleep mode)
198
- #**************************************************************************/
198
+ #**************************************************************************
199
199
def disable (self ):
200
200
if (self ._debug == True ): print "disable"
201
201
# Turn the device off to save power */
202
202
self ._i2c .write8 (self .TSL2561_COMMAND_BIT | self .TSL2561_REGISTER_CONTROL , self .TSL2561_CONTROL_POWEROFF )
203
203
if (self ._debug == True ): print "disable_end"
204
204
205
- #**************************************************************************/
205
+ #**************************************************************************
206
206
# Private function to read luminosity on both channels
207
- #**************************************************************************/
207
+ #**************************************************************************
208
208
def getData (self ):
209
209
if (self ._debug == True ): print "getData"
210
210
# Enable the device by setting the control bit to 0x03 */
@@ -229,9 +229,9 @@ def getData (self):
229
229
self .disable ();
230
230
if (self ._debug == True ): print "getData_end"
231
231
232
- #**************************************************************************/
232
+ #**************************************************************************
233
233
# Constructor
234
- #**************************************************************************/
234
+ #**************************************************************************
235
235
def __init__ (self , addr = TSL2561_ADDR_FLOAT , debug = False ):
236
236
self ._debug = debug
237
237
if (self ._debug == True ): print "__init__"
@@ -246,10 +246,10 @@ def __init__(self, addr=TSL2561_ADDR_FLOAT, debug=False):
246
246
self ._ir = 0
247
247
if (self ._debug == True ): print "__init___end"
248
248
249
- #**************************************************************************/
249
+ #**************************************************************************
250
250
# Initializes I2C and configures the sensor (call this function before
251
251
# doing anything else)
252
- #**************************************************************************/
252
+ #**************************************************************************
253
253
def begin (self ):
254
254
if (self ._debug == True ): print "begin"
255
255
# Make sure we're actually connected */
@@ -268,10 +268,10 @@ def begin(self):
268
268
269
269
return True
270
270
271
- #**************************************************************************/
271
+ #**************************************************************************
272
272
# Enables or disables the auto-gain settings when reading
273
273
# data from the sensor
274
- #**************************************************************************/
274
+ #**************************************************************************
275
275
def enableAutoGain (self , enable ):
276
276
if (self ._debug == True ): print "enableAutoGain"
277
277
self ._tsl2561AutoGain = enable if True else False
@@ -281,9 +281,9 @@ def enableAutoGain(self, enable):
281
281
self ._tsl2561AutoGain = False
282
282
if (self ._debug == True ): print "enableAutoGain_end"
283
283
284
- #**************************************************************************/
284
+ #**************************************************************************
285
285
# Sets the integration time for the TSL2561
286
- #**************************************************************************/
286
+ #**************************************************************************
287
287
def setIntegrationTime (self , time ):
288
288
if (self ._debug == True ): print "setIntegrationTime"
289
289
if (not self ._tsl2561Initialised ):
@@ -302,9 +302,9 @@ def setIntegrationTime(self, time):
302
302
self .disable ()
303
303
if (self ._debug == True ): print "setIntegrationTime_end"
304
304
305
- #**************************************************************************/
305
+ #**************************************************************************
306
306
# Adjusts the gain on the TSL2561 (adjusts the sensitivity to light)
307
- #**************************************************************************/
307
+ #**************************************************************************
308
308
def setGain (self , gain ):
309
309
if (self ._debug == True ): print "setGain"
310
310
if (not self ._tsl2561Initialised ):
@@ -323,10 +323,10 @@ def setGain(self, gain):
323
323
self .disable ()
324
324
if (self ._debug == True ): print "setGain_end"
325
325
326
- #**************************************************************************/
326
+ #**************************************************************************
327
327
# Gets the broadband (mixed lighting) and IR only values from
328
328
# the TSL2561, adjusting gain if auto-gain is enabled
329
- #**************************************************************************/
329
+ #**************************************************************************
330
330
def getLuminosity (self ):
331
331
# This is a hack to ensure that when looping with autogain the gain can go up and down as without
332
332
# setting the gain to 1X before every reading it doesn't seem able to go from 16X
@@ -390,10 +390,10 @@ def getLuminosity (self):
390
390
valid = True
391
391
if (self ._debug == True ): print "getLuminosity_end"
392
392
393
- #**************************************************************************/
393
+ #**************************************************************************
394
394
# Converts the raw sensor values to the standard SI lux equivalent.
395
395
# Returns 0 if the sensor is saturated and the values are unreliable.
396
- #**************************************************************************/
396
+ #**************************************************************************
397
397
def calculateLux (self ):
398
398
if (self ._debug == True ): print "calculateLux"
399
399
self .getLuminosity ()
@@ -501,9 +501,9 @@ def calculateLux(self):
501
501
if (self ._debug == True ): print "calculateLux_end"
502
502
return lux
503
503
504
- #**************************************************************************/
504
+ #**************************************************************************
505
505
# Calculates an averaged Lux value over default 25 samples
506
- #**************************************************************************/
506
+ #**************************************************************************
507
507
def calculateAvgLux (self , testavg = TSL2561_NO_OF_AVG_SAMPLES ):
508
508
# Set initial vars
509
509
count = 0
0 commit comments