Skip to content

Commit c6b9d5c

Browse files
authored
Merge pull request #16 from iBug/patch-1
Change assertions to raising appropriate Exceptions
2 parents 7f16dc9 + 1754752 commit c6b9d5c

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

adafruit_ads1x15/differential.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ def read_adc_difference(self, differential, gain=1, data_rate=None):
5151
- 2 = Channel 1 minus channel 3
5252
- 3 = Channel 2 minus channel 3
5353
"""
54-
assert 0 <= differential <= 3, 'Differential must be a value within 0-3!'
54+
if not 0 <= differential <= 3:
55+
raise ValueError('Differential must be a value within 0-3!')
5556
# Perform a single shot read using the provided differential value
5657
# as the mux value (which will enable differential mode).
5758
return self._read(differential, gain, data_rate, ADS1X15_CONFIG_MODE_SINGLE)
@@ -64,7 +65,8 @@ def read_volts_difference(self, differential, gain=1, data_rate=None):
6465
- 2 = Channel 1 minus channel 3
6566
- 3 = Channel 2 minus channel 3
6667
"""
67-
assert 0 <= differential <= 3, 'Differential must be a value within 0-3!'
68+
if not 0 <= differential <= 3:
69+
raise ValueError('Differential must be a value within 0-3!')
6870
raw = self.read_adc_difference(differential, gain, data_rate)
6971
volts = raw * (ADS1X15_PGA_RANGE[gain] / (2**(self.bits-1) - 1))
7072
return volts
@@ -80,7 +82,8 @@ def start_adc_difference(self, differential, gain=1, data_rate=None):
8082
function continuously to read the most recent conversion result. Call
8183
stop_adc() to stop conversions.
8284
"""
83-
assert 0 <= differential <= 3, 'Differential must be a value within 0-3!'
85+
if not 0 <= differential <= 3:
86+
raise ValueError('Differential must be a value within 0-3!')
8487
# Perform a single shot read using the provided differential value
8588
# as the mux value (which will enable differential mode).
8689
return self._read(differential, gain, data_rate, ADS1X15_CONFIG_MODE_CONTINUOUS)

adafruit_ads1x15/single_ended.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def read_adc(self, channel, gain=1, data_rate=None):
4747
"""Read a single ADC channel and return the ADC value as a signed integer
4848
result. Channel must be a value within 0-3.
4949
"""
50-
assert 0 <= channel <= 3, 'Channel must be a value within 0-3!'
50+
if not 0 <= channel <= 3:
51+
raise ValueError('Channel must be a value within 0-3!')
5152
# Perform a single shot read and set the mux value to the channel plus
5253
# the highest bit (bit 3) set.
5354
return self._read(channel + 0x04, gain, data_rate, ADS1X15_CONFIG_MODE_SINGLE)
@@ -56,7 +57,8 @@ def read_volts(self, channel, gain=1, data_rate=None):
5657
"""Read a single ADC channel and return the voltage value as a floating point
5758
result. Channel must be a value within 0-3.
5859
"""
59-
assert 0 <= channel <= 3, 'Channel must be a value within 0-3!'
60+
if not 0 <= channel <= 3:
61+
raise ValueError('Channel must be a value within 0-3!')
6062
raw = self.read_adc(channel, gain, data_rate)
6163
volts = raw * (ADS1X15_PGA_RANGE[gain] / (2**(self.bits-1) - 1))
6264
return volts
@@ -67,7 +69,8 @@ def start_adc(self, channel, gain=1, data_rate=None):
6769
function to read the most recent conversion result. Call stop_adc() to
6870
stop conversions.
6971
"""
70-
assert 0 <= channel <= 3, 'Channel must be a value within 0-3!'
72+
if not 0 <= channel <= 3:
73+
raise ValueError('Channel must be a value within 0-3!')
7174
# Start continuous reads and set the mux value to the channel plus
7275
# the highest bit (bit 3) set.
7376
return self._read(channel + 0x04, gain, data_rate, ADS1X15_CONFIG_MODE_CONTINUOUS)

0 commit comments

Comments
 (0)