Skip to content

Commit 2ac7469

Browse files
committed
Changed printing to use logger, shortened if enable == True to match convention
1 parent e24ff65 commit 2ac7469

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

Adafruit_TSL2561/Adafruit_TSL2561.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
import sys
3434
import time
35+
import logging
3536
from Adafruit_I2C import Adafruit_I2C
3637

3738

@@ -162,9 +163,9 @@ def write8(self, reg, value):
162163
:param reg: Register / Address to write value to
163164
:param value: Byte to write to Address
164165
"""
165-
if self._debug == True: print "write8"
166+
logging.debug('write8')
166167
self._i2c.write8(reg, value)
167-
if self._debug == True: print "write8_end"
168+
logging.debug('write8_end')
168169

169170
def read8(self, reg):
170171
"""
@@ -173,9 +174,9 @@ def read8(self, reg):
173174
:param reg: Register / Address to read value from
174175
:return: Unsigned byte
175176
"""
176-
if self._debug == True: print "read8"
177+
logging.debug('read8')
177178
return self._i2c.readU8(reg)
178-
if self._debug == True: print "read8_end"
179+
logging.debug('read8_end')
179180

180181
def read16(self, reg):
181182
"""
@@ -184,33 +185,33 @@ def read16(self, reg):
184185
:param reg: Register / Address to read value from
185186
:return: Unsigned word
186187
"""
187-
if self._debug == True: print "read16"
188+
logging.debug('read16')
188189
return self._i2c.readU16(reg)
189-
if self._debug == True: print "read16_end"
190+
logging.debug('read16_end')
190191

191192
def enable(self):
192193
"""
193194
Enables the device
194195
"""
195-
if self._debug == True: print "enable"
196+
logging.debug('enable')
196197
# Enable the device by setting the control bit to 0x03 */
197198
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')
199200

200201
def disable(self):
201202
"""
202203
Disables the device (putting it in lower power sleep mode)
203204
"""
204-
if self._debug == True: print "disable"
205+
logging.debug('disable')
205206
# Turn the device off to save power */
206207
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')
208209

209210
def get_data(self):
210211
"""
211212
Private function to read luminosity on both channels
212213
"""
213-
if self._debug == True: print "get_data"
214+
logging.debug('get_data')
214215

215216
# Enables the device by setting the control bit to 0x03
216217
self.enable()
@@ -231,7 +232,7 @@ def get_data(self):
231232

232233
# Turn the device off to save power */
233234
self.disable()
234-
if self._debug == True: print "getData_end"
235+
logging.debug('getData_end"')
235236

236237
def __init__(self, addr=TSL2561_ADDR_FLOAT, debug=False):
237238
"""
@@ -241,7 +242,7 @@ def __init__(self, addr=TSL2561_ADDR_FLOAT, debug=False):
241242
:param debug: Turn on debugging, defaults to False
242243
"""
243244
self._debug = debug
244-
if self._debug == True: print "__init__"
245+
logging.debug('__init__"')
245246
self._addr = addr
246247
self._tsl2561Initialised = False
247248
self._tsl2561AutoGain = False
@@ -251,7 +252,7 @@ def __init__(self, addr=TSL2561_ADDR_FLOAT, debug=False):
251252
self._luminosity = 0
252253
self._broadband = 0
253254
self._ir = 0
254-
if self._debug == True: print "__init___end"
255+
logging.debug('__init___end')
255256

256257
def begin(self):
257258
"""
@@ -262,7 +263,7 @@ def begin(self):
262263
263264
:return: True if connected to a TSL2561
264265
"""
265-
if self._debug == True: print "begin"
266+
logging.debug('begin')
266267
# Make sure we're actually connected */
267268
x = self.read8(self.TSL2561_REGISTER_ID)
268269
if not(x & 0x0A):
@@ -275,7 +276,7 @@ def begin(self):
275276

276277
# Note: by default, the device is in power down mode on bootup */
277278
self.disable()
278-
if self._debug == True: print "begin_end"
279+
logging.debug('begin_end')
279280

280281
return True
281282

@@ -286,12 +287,12 @@ def enable_auto_gain(self, enable):
286287
287288
:param enable: True to enable
288289
"""
289-
if self._debug == True: print "enable_auto_gain"
290-
if enable == True:
290+
logging.debug('enable_auto_gain')
291+
if enable:
291292
self._tsl2561AutoGain = enable
292293
else:
293294
self._tsl2561AutoGain = False
294-
if self._debug == True: print "enableAutoGain_end"
295+
logging.debug('enableAutoGain_end')
295296

296297
def set_integration_time(self, integration_time):
297298
"""
@@ -300,7 +301,7 @@ def set_integration_time(self, integration_time):
300301
:param integration_time:
301302
:return:
302303
"""
303-
if self._debug == True: print "set_integration_time"
304+
logging.debug('set_integration_time')
304305
if not self._tsl2561Initialised:
305306
self.begin()
306307

@@ -315,15 +316,15 @@ def set_integration_time(self, integration_time):
315316

316317
# Turn the device off to save power */
317318
self.disable()
318-
if self._debug == True: print "setIntegrationTime_end"
319+
logging.debug('setIntegrationTime_end')
319320

320321
def set_gain(self, gain):
321322
"""
322323
Adjusts the gain on the TSL2561 (adjusts the sensitivity to light)
323324
324325
:param gain:
325326
"""
326-
if self._debug == True: print "set_gain"
327+
logging.debug('set_gain')
327328
if not self._tsl2561Initialised:
328329
self.begin()
329330

@@ -338,7 +339,7 @@ def set_gain(self, gain):
338339

339340
# Turn the device off to save power */
340341
self.disable()
341-
if self._debug == True: print "setGain_end"
342+
logging.debug('setGain_end')
342343

343344
def get_luminosity(self):
344345
"""
@@ -352,7 +353,7 @@ def get_luminosity(self):
352353
if self._tsl2561AutoGain:
353354
self.set_gain(self.TSL2561_GAIN_1X)
354355

355-
if self._debug == True: print "get_luminosity"
356+
logging.debug('get_luminosity')
356357
valid = False
357358

358359
if not self._tsl2561Initialised:
@@ -406,7 +407,7 @@ def get_luminosity(self):
406407
# This avoids endless loops where a value is at one extreme pre-gain,
407408
# and the the other extreme post-gain */
408409
valid = True
409-
if self._debug == True: print "getLuminosity_end"
410+
logging.debug('getLuminosity_end')
410411

411412
def calculate_lux(self):
412413
"""
@@ -415,7 +416,7 @@ def calculate_lux(self):
415416
416417
:return: lux value, unsigned 16bit word (0 - 65535)
417418
"""
418-
if self._debug == True: print "calculate_lux"
419+
logging.debug('calculate_lux')
419420
self.get_luminosity()
420421
# Make sure the sensor isn't saturated! */
421422
if self._tsl2561IntegrationTime == self.TSL2561_INTEGRATIONTIME_13MS:
@@ -520,7 +521,7 @@ def calculate_lux(self):
520521
lux = temp >> self.TSL2561_LUX_LUXSCALE
521522

522523
# Signal I2C had no errors */
523-
if self._debug == True: print "calculateLux_end"
524+
logging.debug('calculateLux_end')
524525
return lux
525526

526527
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):
547548
LightSensor = AdafruitTSL2561()
548549
LightSensor.enable_auto_gain(True)
549550

551+
logging.basicConfig(level=logging.DEBUG)
552+
550553
# See if "loop" has been passed as an arg.
551554
try:
552555
arg = sys.argv[1]

0 commit comments

Comments
 (0)