32
32
33
33
import sys
34
34
import time
35
+ import logging
35
36
from Adafruit_I2C import Adafruit_I2C
36
37
37
38
@@ -162,9 +163,9 @@ def write8(self, reg, value):
162
163
:param reg: Register / Address to write value to
163
164
:param value: Byte to write to Address
164
165
"""
165
- if self . _debug == True : print " write8"
166
+ logging . debug ( ' write8' )
166
167
self ._i2c .write8 (reg , value )
167
- if self . _debug == True : print " write8_end"
168
+ logging . debug ( ' write8_end' )
168
169
169
170
def read8 (self , reg ):
170
171
"""
@@ -173,9 +174,9 @@ def read8(self, reg):
173
174
:param reg: Register / Address to read value from
174
175
:return: Unsigned byte
175
176
"""
176
- if self . _debug == True : print " read8"
177
+ logging . debug ( ' read8' )
177
178
return self ._i2c .readU8 (reg )
178
- if self . _debug == True : print " read8_end"
179
+ logging . debug ( ' read8_end' )
179
180
180
181
def read16 (self , reg ):
181
182
"""
@@ -184,33 +185,33 @@ def read16(self, reg):
184
185
:param reg: Register / Address to read value from
185
186
:return: Unsigned word
186
187
"""
187
- if self . _debug == True : print " read16"
188
+ logging . debug ( ' read16' )
188
189
return self ._i2c .readU16 (reg )
189
- if self . _debug == True : print " read16_end"
190
+ logging . debug ( ' read16_end' )
190
191
191
192
def enable (self ):
192
193
"""
193
194
Enables the device
194
195
"""
195
- if self . _debug == True : print " enable"
196
+ logging . debug ( ' enable' )
196
197
# Enable the device by setting the control bit to 0x03 */
197
198
self ._i2c .write8 (self .TSL2561_COMMAND_BIT | self .TSL2561_REGISTER_CONTROL , self .TSL2561_CONTROL_POWERON )
198
- if self . _debug == True : print " enable_end"
199
+ logging . debug ( ' enable_end' )
199
200
200
201
def disable (self ):
201
202
"""
202
203
Disables the device (putting it in lower power sleep mode)
203
204
"""
204
- if self . _debug == True : print " disable"
205
+ logging . debug ( ' disable' )
205
206
# Turn the device off to save power */
206
207
self ._i2c .write8 (self .TSL2561_COMMAND_BIT | self .TSL2561_REGISTER_CONTROL , self .TSL2561_CONTROL_POWEROFF )
207
- if self . _debug == True : print " disable_end"
208
+ logging . debug ( ' disable_end' )
208
209
209
210
def get_data (self ):
210
211
"""
211
212
Private function to read luminosity on both channels
212
213
"""
213
- if self . _debug == True : print " get_data"
214
+ logging . debug ( ' get_data' )
214
215
215
216
# Enables the device by setting the control bit to 0x03
216
217
self .enable ()
@@ -231,7 +232,7 @@ def get_data(self):
231
232
232
233
# Turn the device off to save power */
233
234
self .disable ()
234
- if self . _debug == True : print " getData_end"
235
+ logging . debug ( ' getData_end"' )
235
236
236
237
def __init__ (self , addr = TSL2561_ADDR_FLOAT , debug = False ):
237
238
"""
@@ -241,7 +242,7 @@ def __init__(self, addr=TSL2561_ADDR_FLOAT, debug=False):
241
242
:param debug: Turn on debugging, defaults to False
242
243
"""
243
244
self ._debug = debug
244
- if self . _debug == True : print " __init__"
245
+ logging . debug ( ' __init__"' )
245
246
self ._addr = addr
246
247
self ._tsl2561Initialised = False
247
248
self ._tsl2561AutoGain = False
@@ -251,7 +252,7 @@ def __init__(self, addr=TSL2561_ADDR_FLOAT, debug=False):
251
252
self ._luminosity = 0
252
253
self ._broadband = 0
253
254
self ._ir = 0
254
- if self . _debug == True : print " __init___end"
255
+ logging . debug ( ' __init___end' )
255
256
256
257
def begin (self ):
257
258
"""
@@ -262,7 +263,7 @@ def begin(self):
262
263
263
264
:return: True if connected to a TSL2561
264
265
"""
265
- if self . _debug == True : print " begin"
266
+ logging . debug ( ' begin' )
266
267
# Make sure we're actually connected */
267
268
x = self .read8 (self .TSL2561_REGISTER_ID )
268
269
if not (x & 0x0A ):
@@ -275,7 +276,7 @@ def begin(self):
275
276
276
277
# Note: by default, the device is in power down mode on bootup */
277
278
self .disable ()
278
- if self . _debug == True : print " begin_end"
279
+ logging . debug ( ' begin_end' )
279
280
280
281
return True
281
282
@@ -286,12 +287,12 @@ def enable_auto_gain(self, enable):
286
287
287
288
:param enable: True to enable
288
289
"""
289
- if self . _debug == True : print " enable_auto_gain"
290
- if enable == True :
290
+ logging . debug ( ' enable_auto_gain' )
291
+ if enable :
291
292
self ._tsl2561AutoGain = enable
292
293
else :
293
294
self ._tsl2561AutoGain = False
294
- if self . _debug == True : print " enableAutoGain_end"
295
+ logging . debug ( ' enableAutoGain_end' )
295
296
296
297
def set_integration_time (self , integration_time ):
297
298
"""
@@ -300,7 +301,7 @@ def set_integration_time(self, integration_time):
300
301
:param integration_time:
301
302
:return:
302
303
"""
303
- if self . _debug == True : print " set_integration_time"
304
+ logging . debug ( ' set_integration_time' )
304
305
if not self ._tsl2561Initialised :
305
306
self .begin ()
306
307
@@ -315,15 +316,15 @@ def set_integration_time(self, integration_time):
315
316
316
317
# Turn the device off to save power */
317
318
self .disable ()
318
- if self . _debug == True : print " setIntegrationTime_end"
319
+ logging . debug ( ' setIntegrationTime_end' )
319
320
320
321
def set_gain (self , gain ):
321
322
"""
322
323
Adjusts the gain on the TSL2561 (adjusts the sensitivity to light)
323
324
324
325
:param gain:
325
326
"""
326
- if self . _debug == True : print " set_gain"
327
+ logging . debug ( ' set_gain' )
327
328
if not self ._tsl2561Initialised :
328
329
self .begin ()
329
330
@@ -338,7 +339,7 @@ def set_gain(self, gain):
338
339
339
340
# Turn the device off to save power */
340
341
self .disable ()
341
- if self . _debug == True : print " setGain_end"
342
+ logging . debug ( ' setGain_end' )
342
343
343
344
def get_luminosity (self ):
344
345
"""
@@ -352,7 +353,7 @@ def get_luminosity(self):
352
353
if self ._tsl2561AutoGain :
353
354
self .set_gain (self .TSL2561_GAIN_1X )
354
355
355
- if self . _debug == True : print " get_luminosity"
356
+ logging . debug ( ' get_luminosity' )
356
357
valid = False
357
358
358
359
if not self ._tsl2561Initialised :
@@ -406,7 +407,7 @@ def get_luminosity(self):
406
407
# This avoids endless loops where a value is at one extreme pre-gain,
407
408
# and the the other extreme post-gain */
408
409
valid = True
409
- if self . _debug == True : print " getLuminosity_end"
410
+ logging . debug ( ' getLuminosity_end' )
410
411
411
412
def calculate_lux (self ):
412
413
"""
@@ -415,7 +416,7 @@ def calculate_lux(self):
415
416
416
417
:return: lux value, unsigned 16bit word (0 - 65535)
417
418
"""
418
- if self . _debug == True : print " calculate_lux"
419
+ logging . debug ( ' calculate_lux' )
419
420
self .get_luminosity ()
420
421
# Make sure the sensor isn't saturated! */
421
422
if self ._tsl2561IntegrationTime == self .TSL2561_INTEGRATIONTIME_13MS :
@@ -520,7 +521,7 @@ def calculate_lux(self):
520
521
lux = temp >> self .TSL2561_LUX_LUXSCALE
521
522
522
523
# Signal I2C had no errors */
523
- if self . _debug == True : print " calculateLux_end"
524
+ logging . debug ( ' calculateLux_end' )
524
525
return lux
525
526
526
527
def calculate_avg_lux (self , testavg = TSL2561_NO_OF_AVG_SAMPLES ):
@@ -547,6 +548,8 @@ def calculate_avg_lux(self, testavg=TSL2561_NO_OF_AVG_SAMPLES):
547
548
LightSensor = AdafruitTSL2561 ()
548
549
LightSensor .enable_auto_gain (True )
549
550
551
+ logging .basicConfig (level = logging .DEBUG )
552
+
550
553
# See if "loop" has been passed as an arg.
551
554
try :
552
555
arg = sys .argv [1 ]
0 commit comments