Skip to content

Commit bb0be79

Browse files
committed
PEP 8 convention changes
1 parent 3a4090b commit bb0be79

File tree

2 files changed

+71
-71
lines changed

2 files changed

+71
-71
lines changed

Adafruit_TSL2561/Adafruit_TSL2561.py

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
Minor fixes and changes
2525
1.0 - Initial release - Iain Colledge
2626
Removed commented out C++ code
27-
Added calculateAvgLux
28-
Changed main method to use calculateAvgLux and loop argument support added.
27+
Added calculate_avg_lux
28+
Changed main method to use calculate_avg_lux and loop argument support added.
2929
Ported "Extended delays to take into account loose timing with 'delay'" update from CPP code
3030
Added hack so that with autogain every sample goes from 1x to 16x as going from 16x to 1x does not work
3131
"""
@@ -34,7 +34,8 @@
3434
import time
3535
from Adafruit_I2C import Adafruit_I2C
3636

37-
class Adafruit_TSL2561(Adafruit_I2C):
37+
38+
class AdafruitTSL2561(Adafruit_I2C):
3839
TSL2561_VISIBLE = 2 # channel 0 - channel 1
3940
TSL2561_INFRARED = 1 # channel 1
4041
TSL2561_FULLSPECTRUM = 0 # channel 0
@@ -87,7 +88,7 @@ class Adafruit_TSL2561(Adafruit_I2C):
8788
TSL2561_LUX_K8T = 0x029a # 1.3 * 2^RATIO_SCALE
8889
TSL2561_LUX_B8T = 0x0000 # 0.000 * 2^LUX_SCALE
8990
TSL2561_LUX_M8T = 0x0000 # 0.000 * 2^LUX_SCALE
90-
91+
9192
# CS package values
9293
TSL2561_LUX_K1C = 0x0043 # 0.130 * 2^RATIO_SCALE
9394
TSL2561_LUX_B1C = 0x0204 # 0.0315 * 2^LUX_SCALE
@@ -140,21 +141,21 @@ class Adafruit_TSL2561(Adafruit_I2C):
140141
TSL2561_REGISTER_CHAN0_HIGH = 0x0D
141142
TSL2561_REGISTER_CHAN1_LOW = 0x0E
142143
TSL2561_REGISTER_CHAN1_HIGH = 0x0F
143-
144+
144145
TSL2561_INTEGRATIONTIME_13MS = 0x00 # 13.7ms
145146
TSL2561_INTEGRATIONTIME_101MS = 0x01 # 101ms
146147
TSL2561_INTEGRATIONTIME_402MS = 0x02 # 402ms
147148

148149
TSL2561_DELAY_INTTIME_13MS = 0.015
149150
TSL2561_DELAY_INTTIME_101MS = 0.120
150151
TSL2561_DELAY_INTTIME_402MS = 0.450
151-
152+
152153
TSL2561_GAIN_1X = 0x00 # No gain
153154
TSL2561_GAIN_16X = 0x10 # 16x gain
154155

155156
TSL2561_NO_OF_AVG_SAMPLES = 25 # How many samples to make an average reading
156-
157-
def write8 (self, reg, value):
157+
158+
def write8(self, reg, value):
158159
"""
159160
Writes a register and an 8 bit value over I2C
160161
@@ -205,32 +206,31 @@ def disable(self):
205206
self._i2c.write8(self.TSL2561_COMMAND_BIT | self.TSL2561_REGISTER_CONTROL, self.TSL2561_CONTROL_POWEROFF)
206207
if (self._debug == True): print "disable_end"
207208

208-
def getData (self):
209+
def get_data(self):
209210
"""
210211
Private function to read luminosity on both channels
211212
"""
212-
if (self._debug == True): print "getData"
213+
if (self._debug == True): print "get_data"
213214

214-
#Enables the device by setting the control bit to 0x03
215-
self.enable();
215+
# Enables the device by setting the control bit to 0x03
216+
self.enable()
216217

217218
# Wait x ms for ADC to complete */
218219
if self._tsl2561IntegrationTime == self.TSL2561_INTEGRATIONTIME_13MS:
219220
time.sleep(self.TSL2561_DELAY_INTTIME_13MS)
220221
elif self._tsl2561IntegrationTime == self.TSL2561_INTEGRATIONTIME_101MS:
221-
time.sleep(self.TSL2561_DELAY_INTTIME_101MS)
222+
time.sleep(self.TSL2561_DELAY_INTTIME_101MS)
222223
else:
223-
time.sleep(self.TSL2561_DELAY_INTTIME_402MS)
224-
224+
time.sleep(self.TSL2561_DELAY_INTTIME_402MS)
225225

226226
# Reads a two byte value from channel 0 (visible + infrared) */
227-
self._broadband = self.read16(self.TSL2561_COMMAND_BIT | self.TSL2561_WORD_BIT | self.TSL2561_REGISTER_CHAN0_LOW);
227+
self._broadband = self.read16(self.TSL2561_COMMAND_BIT | self.TSL2561_WORD_BIT | self.TSL2561_REGISTER_CHAN0_LOW)
228228

229229
# Reads a two byte value from channel 1 (infrared) */
230-
self._ir = self.read16(self.TSL2561_COMMAND_BIT | self.TSL2561_WORD_BIT | self.TSL2561_REGISTER_CHAN1_LOW);
230+
self._ir = self.read16(self.TSL2561_COMMAND_BIT | self.TSL2561_WORD_BIT | self.TSL2561_REGISTER_CHAN1_LOW)
231231

232232
# Turn the device off to save power */
233-
self.disable();
233+
self.disable()
234234
if (self._debug == True): print "getData_end"
235235

236236
def __init__(self, addr=TSL2561_ADDR_FLOAT, debug=False):
@@ -264,48 +264,48 @@ def begin(self):
264264
"""
265265
if (self._debug == True): print "begin"
266266
# Make sure we're actually connected */
267-
x = self.read8(self.TSL2561_REGISTER_ID);
267+
x = self.read8(self.TSL2561_REGISTER_ID)
268268
if not(x & 0x0A):
269269
return False
270270
self._tsl2561Initialised = True
271271

272272
# Set default integration time and gain */
273-
self.setIntegrationTime(self._tsl2561IntegrationTime)
274-
self.setGain(self._tsl2561Gain)
273+
self.set_integration_time(self._tsl2561IntegrationTime)
274+
self.set_gain(self._tsl2561Gain)
275275

276276
# Note: by default, the device is in power down mode on bootup */
277277
self.disable()
278278
if (self._debug == True): print "begin_end"
279279

280280
return True
281-
282-
def enableAutoGain(self, enable):
281+
282+
def enable_auto_gain(self, enable):
283283
"""
284284
Enables or disables the auto-gain settings when reading
285285
data from the sensor
286286
287287
:param enable: True to enable
288288
"""
289-
if (self._debug == True): print "enableAutoGain"
289+
if (self._debug == True): print "enable_auto_gain"
290290
if (enable == True):
291291
self._tsl2561AutoGain = enable
292292
else:
293293
self._tsl2561AutoGain = False
294294
if (self._debug == True): print "enableAutoGain_end"
295295

296-
def setIntegrationTime(self, time):
296+
def set_integration_time(self, time):
297297
"""
298298
Sets the integration time for the TSL2561
299299
300300
:param time:
301301
:return:
302302
"""
303-
if (self._debug == True): print "setIntegrationTime"
303+
if (self._debug == True): print "set_integration_time"
304304
if (not self._tsl2561Initialised):
305305
self.begin
306306

307307
# Enable the device by setting the control bit to 0x03 */
308-
self.enable();
308+
self.enable()
309309

310310
# Update the timing register */
311311
self.write8(self.TSL2561_COMMAND_BIT | self.TSL2561_REGISTER_TIMING, time | self._tsl2561Gain)
@@ -316,14 +316,14 @@ def setIntegrationTime(self, time):
316316
# Turn the device off to save power */
317317
self.disable()
318318
if (self._debug == True): print "setIntegrationTime_end"
319-
320-
def setGain(self, gain):
319+
320+
def set_gain(self, gain):
321321
"""
322322
Adjusts the gain on the TSL2561 (adjusts the sensitivity to light)
323323
324324
:param gain:
325325
"""
326-
if (self._debug == True): print "setGain"
326+
if (self._debug == True): print "set_gain"
327327
if (not self._tsl2561Initialised):
328328
self.begin
329329

@@ -340,7 +340,7 @@ def setGain(self, gain):
340340
self.disable()
341341
if (self._debug == True): print "setGain_end"
342342

343-
def getLuminosity (self):
343+
def get_luminosity(self):
344344
"""
345345
Gets the broadband (mixed lighting) and IR only values from
346346
the TSL2561, adjusting gain if auto-gain is enabled
@@ -350,23 +350,23 @@ def getLuminosity (self):
350350
# setting the gain to 1X before every reading it doesn't seem able to go from 16X
351351
# back to 1X again. Going from 1X to 16X works fine. - IC
352352
if (self._tsl2561AutoGain):
353-
self.setGain(self.TSL2561_GAIN_1X)
353+
self.set_gain(self.TSL2561_GAIN_1X)
354354

355-
if (self._debug == True): print "getLuminosity"
355+
if (self._debug == True): print "get_luminosity"
356356
valid = False
357357

358358
if (not self._tsl2561Initialised):
359-
self.begin
359+
self.begin
360360

361361
# If Auto gain disabled get a single reading and continue */
362362
if(not self._tsl2561AutoGain):
363-
self.getData()
363+
self.get_data()
364364
return
365365

366366
# Read data until we find a valid range */
367367
_agcCheck = False
368368
while (not valid):
369-
_it = self._tsl2561IntegrationTime;
369+
_it = self._tsl2561IntegrationTime
370370

371371
# Get the hi/low threshold for the current integration time */
372372
if _it==self.TSL2561_INTEGRATIONTIME_13MS:
@@ -379,22 +379,22 @@ def getLuminosity (self):
379379
_hi = self.TSL2561_AGC_THI_402MS
380380
_lo = self.TSL2561_AGC_TLO_402MS
381381

382-
self.getData()
382+
self.get_data()
383383

384384
# Run an auto-gain check if we haven't already done so ... */
385385
if (not _agcCheck):
386386
if ((self._broadband < _lo) and (self._tsl2561Gain == self.TSL2561_GAIN_1X)):
387387
# Increase the gain and try again */
388-
self.setGain(self.TSL2561_GAIN_16X)
388+
self.set_gain(self.TSL2561_GAIN_16X)
389389
# Drop the previous conversion results */
390-
self.getData()
390+
self.get_data()
391391
# Set a flag to indicate we've adjusted the gain */
392392
_agcCheck = True
393393
elif ((self._broadband > _hi) and (self._tsl2561Gain == self.TSL2561_GAIN_16X)):
394394
# Drop gain to 1x and try again */
395-
self.setGain(self.TSL2561_GAIN_1X)
395+
self.set_gain(self.TSL2561_GAIN_1X)
396396
# Drop the previous conversion results */
397-
self.getData()
397+
self.get_data()
398398
# Set a flag to indicate we've adjusted the gain */
399399
_agcCheck = True
400400
else:
@@ -407,49 +407,49 @@ def getLuminosity (self):
407407
# and the the other extreme post-gain */
408408
valid = True
409409
if (self._debug == True): print "getLuminosity_end"
410-
411-
def calculateLux(self):
410+
411+
def calculate_lux(self):
412412
"""
413413
Converts the raw sensor values to the standard SI lux equivalent.
414414
Returns 0 if the sensor is saturated and the values are unreliable.
415415
416416
:return: lux value, unsigned 16bit word (0 - 65535)
417417
"""
418-
if (self._debug == True): print "calculateLux"
419-
self.getLuminosity()
418+
if (self._debug == True): print "calculate_lux"
419+
self.get_luminosity()
420420
# Make sure the sensor isn't saturated! */
421421
if (self._tsl2561IntegrationTime == self.TSL2561_INTEGRATIONTIME_13MS):
422-
clipThreshold = self.TSL2561_CLIPPING_13MS
422+
clip_threshold = self.TSL2561_CLIPPING_13MS
423423
elif (self._tsl2561IntegrationTime == self.TSL2561_INTEGRATIONTIME_101MS):
424-
clipThreshold = self.TSL2561_CLIPPING_101MS
424+
clip_threshold = self.TSL2561_CLIPPING_101MS
425425
else:
426-
clipThreshold = self.TSL2561_CLIPPING_402MS
426+
clip_threshold = self.TSL2561_CLIPPING_402MS
427427

428428
# Return 0 lux if the sensor is saturated */
429429
# TODO: Throw an exception rather than return 0
430-
if ((self._broadband > clipThreshold) or (self._ir > clipThreshold)):
430+
if ((self._broadband > clip_threshold) or (self._ir > clip_threshold)):
431431
return 0
432432

433433
# Get the correct scale depending on the intergration time */
434434
if (self._tsl2561IntegrationTime ==self.TSL2561_INTEGRATIONTIME_13MS):
435-
chScale = self.TSL2561_LUX_CHSCALE_TINT0
435+
ch_scale = self.TSL2561_LUX_CHSCALE_TINT0
436436
elif (self._tsl2561IntegrationTime ==self.TSL2561_INTEGRATIONTIME_101MS):
437-
chScale = self.TSL2561_LUX_CHSCALE_TINT1
437+
ch_scale = self.TSL2561_LUX_CHSCALE_TINT1
438438
else:
439-
chScale = (1 << self.TSL2561_LUX_CHSCALE)
439+
ch_scale = (1 << self.TSL2561_LUX_CHSCALE)
440440

441441
# Scale for gain (1x or 16x) */
442442
if (not self._tsl2561Gain):
443-
chScale = chScale << 4
443+
ch_scale = ch_scale << 4
444444

445445
# Scale the channel values */
446-
channel0 = (self._broadband * chScale) >> self.TSL2561_LUX_CHSCALE
447-
channel1 = (self._ir * chScale) >> self.TSL2561_LUX_CHSCALE
446+
channel0 = (self._broadband * ch_scale) >> self.TSL2561_LUX_CHSCALE
447+
channel1 = (self._ir * ch_scale) >> self.TSL2561_LUX_CHSCALE
448448

449449
# Find the ratio of the channel values (Channel1/Channel0) */
450-
ratio1 = 0;
450+
ratio1 = 0
451451
if (channel0 != 0):
452-
ratio1 = (channel1 << (self.TSL2561_LUX_RATIOSCALE+1)) / channel0
452+
ratio1 = (channel1 << (self.TSL2561_LUX_RATIOSCALE + 1)) / channel0
453453

454454
# round the ratio value */
455455
ratio = (ratio1 + 1) >> 1
@@ -504,25 +504,25 @@ def calculateLux(self):
504504
elif (ratio > self.TSL2561_LUX_K8T):
505505
b=self.TSL2561_LUX_B8T
506506
m=self.TSL2561_LUX_M8T
507-
#endif
507+
# endif
508508

509509
temp = ((channel0 * b) - (channel1 * m))
510510

511511
# Do not allow negative lux value */
512-
if (temp < 0):
512+
if (temp < 0):
513513
temp = 0
514514

515515
# Round lsb (2^(LUX_SCALE-1)) */
516-
temp += (1 << (self.TSL2561_LUX_LUXSCALE-1))
516+
temp += (1 << (self.TSL2561_LUX_LUXSCALE - 1))
517517

518518
# Strip off fractional portion */
519-
lux = temp >> self.TSL2561_LUX_LUXSCALE;
519+
lux = temp >> self.TSL2561_LUX_LUXSCALE
520520

521521
# Signal I2C had no errors */
522522
if (self._debug == True): print "calculateLux_end"
523523
return lux
524524

525-
def calculateAvgLux(self, testavg=TSL2561_NO_OF_AVG_SAMPLES):
525+
def calculate_avg_lux(self, testavg=TSL2561_NO_OF_AVG_SAMPLES):
526526
"""
527527
Calculates an averaged Lux value, useful for flickering lights and for smoothing values due to noise
528528
@@ -534,28 +534,28 @@ def calculateAvgLux(self, testavg=TSL2561_NO_OF_AVG_SAMPLES):
534534
luxavgtotal = 0
535535
# Create a cumulative total of values for 'testavg' tests
536536
while True:
537-
capture = self.calculateLux()
537+
capture = self.calculate_lux()
538538
luxavgtotal = capture + luxavgtotal
539539
count += 1
540540
# Once we reach the number of required tests, work out the average
541-
if ( count >= testavg ):
541+
if (count >= testavg):
542542
luxavg = round(luxavgtotal / testavg)
543543
return (luxavg)
544544

545545
if __name__ == "__main__":
546546
LightSensor = Adafruit_TSL2561()
547-
LightSensor.enableAutoGain(True)
547+
LightSensor.enable_auto_gain(True)
548548

549549
# See if "loop" has been passed as an arg.
550550
try:
551551
arg = sys.argv[1]
552-
if ( arg == "loop" ):
552+
if (arg == "loop"):
553553
while True:
554554
try:
555-
print (int(LightSensor.calculateAvgLux()))
555+
print (int(LightSensor.calculate_avg_lux()))
556556
except KeyboardInterrupt:
557557
quit()
558558
else:
559559
print ("Invalid arg(s):", sys.argv)
560560
except IndexError:
561-
print (int(LightSensor.calculateAvgLux()))
561+
print (int(LightSensor.calculate_avg_lux()))

Adafruit_TSL2561/Adafruit_TSL2561_example.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
from Adafruit_TSL2561 import Adafruit_TSL2561
44

55
# Initialise the sensor
6-
LightSensor = Adafruit_TSL2561.Adafruit_TSL2651()
6+
LightSensor = Adafruit_TSL2561.AdafruitTSL2651()
77

88
# Enable auto gain switching between 1x and 16x
99
# Default is False
10-
LightSensor.enableAutoGain(True)
10+
LightSensor.enable_auto_gain(True)
1111

1212
# Get the calculated lux value, this is a spot reading so if you're under light
13-
# lux = Adafruit_TSL2561.calculateLux()
13+
# lux = Adafruit_TSL2561.calculate_lux()
1414

1515

0 commit comments

Comments
 (0)