1
- #!/usr/bin/python
1
+ #!/usr/bin/env python3
2
2
3
3
import time
4
4
import smbus
@@ -142,7 +142,7 @@ def __init__(self, address=0x48, ic=__IC_ADS1015, debug=False):
142
142
# Make sure the IC specified is valid
143
143
if ((ic < self .__IC_ADS1015 ) | (ic > self .__IC_ADS1115 )):
144
144
if (self .debug ):
145
- print "ADS1x15: Invalid IC specfied: %h" % ic
145
+ print ( "ADS1x15: Invalid IC specfied: %h" % ic )
146
146
return - 1
147
147
else :
148
148
self .ic = ic
@@ -162,7 +162,7 @@ def readADCSingleEnded(self, channel=0, pga=6144, sps=250):
162
162
# With invalid channel return -1
163
163
if (channel > 3 ):
164
164
if (self .debug ):
165
- print "ADS1x15: Invalid channel specified: %d" % channel
165
+ print ( "ADS1x15: Invalid channel specified: %d" % channel )
166
166
return - 1
167
167
168
168
# Disable comparator, Non-latching, Alert/Rdy active low
@@ -180,12 +180,12 @@ def readADCSingleEnded(self, channel=0, pga=6144, sps=250):
180
180
config |= self .spsADS1015 .setdefault (sps , self .__ADS1015_REG_CONFIG_DR_1600SPS )
181
181
else :
182
182
if ( (sps not in self .spsADS1115 ) & self .debug ):
183
- print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps
183
+ print ( "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps )
184
184
config |= self .spsADS1115 .setdefault (sps , self .__ADS1115_REG_CONFIG_DR_250SPS )
185
185
186
186
# Set PGA/voltage range, defaults to +-6.144V
187
187
if ( (pga not in self .pgaADS1x15 ) & self .debug ):
188
- print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps
188
+ print ( "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps )
189
189
config |= self .pgaADS1x15 .setdefault (pga , self .__ADS1015_REG_CONFIG_PGA_6_144V )
190
190
self .pga = pga
191
191
@@ -253,7 +253,7 @@ def readADCDifferential(self, chP=0, chN=1, pga=6144, sps=250):
253
253
config |= self .__ADS1015_REG_CONFIG_MUX_DIFF_1_3
254
254
else :
255
255
if (self .debug ):
256
- print "ADS1x15: Invalid channels specified: %d, %d" % (chP , chN )
256
+ print ( "ADS1x15: Invalid channels specified: %d, %d" % (chP , chN ) )
257
257
return - 1
258
258
259
259
# Set sample per seconds, defaults to 250sps
@@ -263,12 +263,12 @@ def readADCDifferential(self, chP=0, chN=1, pga=6144, sps=250):
263
263
config |= self .spsADS1015 .setdefault (sps , self .__ADS1015_REG_CONFIG_DR_1600SPS )
264
264
else :
265
265
if ( (sps not in self .spsADS1115 ) & self .debug ):
266
- print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps
266
+ print ( "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps )
267
267
config |= self .spsADS1115 .setdefault (sps , self .__ADS1115_REG_CONFIG_DR_250SPS )
268
268
269
269
# Set PGA/voltage range, defaults to +-6.144V
270
270
if ( (pga not in self .pgaADS1x15 ) & self .debug ):
271
- print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps
271
+ print ( "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps )
272
272
config |= self .pgaADS1x15 .setdefault (pga , self .__ADS1015_REG_CONFIG_PGA_6_144V )
273
273
self .pga = pga
274
274
@@ -347,7 +347,7 @@ def startContinuousConversion(self, channel=0, pga=6144, sps=250):
347
347
# Default to channel 0 with invalid channel, or return -1?
348
348
if (channel > 3 ):
349
349
if (self .debug ):
350
- print "ADS1x15: Invalid channel specified: %d" % channel
350
+ print ( "ADS1x15: Invalid channel specified: %d" % channel )
351
351
return - 1
352
352
353
353
# Disable comparator, Non-latching, Alert/Rdy active low
@@ -366,12 +366,12 @@ def startContinuousConversion(self, channel=0, pga=6144, sps=250):
366
366
config |= self .spsADS1015 .setdefault (sps , self .__ADS1015_REG_CONFIG_DR_1600SPS )
367
367
else :
368
368
if ( (sps not in self .spsADS1115 ) & self .debug ):
369
- print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps
369
+ print ( "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps )
370
370
config |= self .spsADS1115 .setdefault (sps , self .__ADS1115_REG_CONFIG_DR_250SPS )
371
371
372
372
# Set PGA/voltage range, defaults to +-6.144V
373
373
if ( (pga not in self .pgaADS1x15 ) & self .debug ):
374
- print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps
374
+ print ( "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps )
375
375
config |= self .pgaADS1x15 .setdefault (pga , self .__ADS1015_REG_CONFIG_PGA_6_144V )
376
376
self .pga = pga
377
377
@@ -439,12 +439,12 @@ def startContinuousDifferentialConversion(self, chP=0, chN=1, pga=6144, sps=250)
439
439
config |= self .spsADS1015 .setdefault (sps , self .__ADS1015_REG_CONFIG_DR_1600SPS )
440
440
else :
441
441
if ( (sps not in self .spsADS1115 ) & self .debug ):
442
- print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps
442
+ print ( "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps )
443
443
config |= self .spsADS1115 .setdefault (sps , self .__ADS1115_REG_CONFIG_DR_250SPS )
444
444
445
445
# Set PGA/voltage range, defaults to +-6.144V
446
446
if ( (pga not in self .pgaADS1x15 ) & self .debug ):
447
- print "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps
447
+ print ( "ADS1x15: Invalid pga specified: %d, using 6144mV" % sps )
448
448
config |= self .pgaADS1x15 .setdefault (pga , self .__ADS1015_REG_CONFIG_PGA_6_144V )
449
449
self .pga = pga
450
450
@@ -459,7 +459,7 @@ def startContinuousDifferentialConversion(self, chP=0, chN=1, pga=6144, sps=250)
459
459
config |= self .__ADS1015_REG_CONFIG_MUX_DIFF_1_3
460
460
else :
461
461
if (self .debug ):
462
- print "ADS1x15: Invalid channels specified: %d, %d" % (chP , chN )
462
+ print ( "ADS1x15: Invalid channels specified: %d, %d" % (chP , chN ) )
463
463
return - 1
464
464
465
465
# Set 'start single-conversion' bit to begin conversions
@@ -540,7 +540,7 @@ def startSingleEndedComparator(self, channel, thresholdHigh, thresholdLow, \
540
540
# With invalid channel return -1
541
541
if (channel > 3 ):
542
542
if (self .debug ):
543
- print "ADS1x15: Invalid channel specified: %d" % channel
543
+ print ( "ADS1x15: Invalid channel specified: %d" % channel )
544
544
return - 1
545
545
546
546
# Continuous mode
@@ -573,16 +573,16 @@ def startSingleEndedComparator(self, channel, thresholdHigh, thresholdLow, \
573
573
# othewise it returns the value for 250sps. This saves a lot of if/elif/else code!
574
574
if (self .ic == self .__IC_ADS1015 ):
575
575
if ( (sps not in self .spsADS1015 ) & self .debug ):
576
- print "ADS1x15: Invalid sps specified: %d, using 1600sps" % sps
576
+ print ( "ADS1x15: Invalid sps specified: %d, using 1600sps" % sps )
577
577
config |= self .spsADS1015 .setdefault (sps , self .__ADS1015_REG_CONFIG_DR_1600SPS )
578
578
else :
579
579
if ( (sps not in self .spsADS1115 ) & self .debug ):
580
- print "ADS1x15: Invalid sps specified: %d, using 250sps" % sps
580
+ print ( "ADS1x15: Invalid sps specified: %d, using 250sps" % sps )
581
581
config |= self .spsADS1115 .setdefault (sps , self .__ADS1115_REG_CONFIG_DR_250SPS )
582
582
583
583
# Set PGA/voltage range, defaults to +-6.144V
584
584
if ( (pga not in self .pgaADS1x15 ) & self .debug ):
585
- print "ADS1x15: Invalid pga specified: %d, using 6144mV" % pga
585
+ print ( "ADS1x15: Invalid pga specified: %d, using 6144mV" % pga )
586
586
config |= self .pgaADS1x15 .setdefault (pga , self .__ADS1015_REG_CONFIG_PGA_6_144V )
587
587
self .pga = pga
588
588
@@ -668,16 +668,16 @@ def startDifferentialComparator(self, chP, chN, thresholdHigh, thresholdLow, \
668
668
# othewise it returns the value for 250sps. This saves a lot of if/elif/else code!
669
669
if (self .ic == self .__IC_ADS1015 ):
670
670
if ( (sps not in self .spsADS1015 ) & self .debug ):
671
- print "ADS1x15: Invalid sps specified: %d, using 1600sps" % sps
671
+ print ( "ADS1x15: Invalid sps specified: %d, using 1600sps" % sps )
672
672
config |= self .spsADS1015 .setdefault (sps , self .__ADS1015_REG_CONFIG_DR_1600SPS )
673
673
else :
674
674
if ( (sps not in self .spsADS1115 ) & self .debug ):
675
- print "ADS1x15: Invalid sps specified: %d, using 250sps" % sps
675
+ print ( "ADS1x15: Invalid sps specified: %d, using 250sps" % sps )
676
676
config |= self .spsADS1115 .setdefault (sps , self .__ADS1115_REG_CONFIG_DR_250SPS )
677
677
678
678
# Set PGA/voltage range, defaults to +-6.144V
679
679
if ( (pga not in self .pgaADS1x15 ) & self .debug ):
680
- print "ADS1x15: Invalid pga specified: %d, using 6144mV" % pga
680
+ print ( "ADS1x15: Invalid pga specified: %d, using 6144mV" % pga )
681
681
config |= self .pgaADS1x15 .setdefault (pga , self .__ADS1015_REG_CONFIG_PGA_6_144V )
682
682
self .pga = pga
683
683
@@ -692,7 +692,7 @@ def startDifferentialComparator(self, chP, chN, thresholdHigh, thresholdLow, \
692
692
config |= self .__ADS1015_REG_CONFIG_MUX_DIFF_1_3
693
693
else :
694
694
if (self .debug ):
695
- print "ADS1x15: Invalid channels specified: %d, %d" % (chP , chN )
695
+ print ( "ADS1x15: Invalid channels specified: %d, %d" % (chP , chN ) )
696
696
return - 1
697
697
698
698
# Set 'start single-conversion' bit to begin conversions
@@ -718,5 +718,4 @@ def startDifferentialComparator(self, chP, chN, thresholdHigh, thresholdLow, \
718
718
# Once we write the ADC will convert continously and alert when things happen,
719
719
# we can read the converted values using getLastConversionResult
720
720
bytes = [(config >> 8 ) & 0xFF , config & 0xFF ]
721
- self .i2c .writeList (self .__ADS1015_REG_POINTER_CONFIG , bytes )
722
-
721
+ self .i2c .writeList (self .__ADS1015_REG_POINTER_CONFIG , bytes )
0 commit comments