Skip to content

Change assertions to raising appropriate Exceptions #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions adafruit_ads1x15/differential.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def read_adc_difference(self, differential, gain=1, data_rate=None):
- 2 = Channel 1 minus channel 3
- 3 = Channel 2 minus channel 3
"""
assert 0 <= differential <= 3, 'Differential must be a value within 0-3!'
if not 0 <= differential <= 3:
raise ValueError('Differential must be a value within 0-3!')
# Perform a single shot read using the provided differential value
# as the mux value (which will enable differential mode).
return self._read(differential, gain, data_rate, ADS1X15_CONFIG_MODE_SINGLE)
Expand All @@ -64,7 +65,8 @@ def read_volts_difference(self, differential, gain=1, data_rate=None):
- 2 = Channel 1 minus channel 3
- 3 = Channel 2 minus channel 3
"""
assert 0 <= differential <= 3, 'Differential must be a value within 0-3!'
if not 0 <= differential <= 3:
raise ValueError('Differential must be a value within 0-3!')
raw = self.read_adc_difference(differential, gain, data_rate)
volts = raw * (ADS1X15_PGA_RANGE[gain] / (2**(self.bits-1) - 1))
return volts
Expand All @@ -80,7 +82,8 @@ def start_adc_difference(self, differential, gain=1, data_rate=None):
function continuously to read the most recent conversion result. Call
stop_adc() to stop conversions.
"""
assert 0 <= differential <= 3, 'Differential must be a value within 0-3!'
if not 0 <= differential <= 3:
raise ValueError('Differential must be a value within 0-3!')
# Perform a single shot read using the provided differential value
# as the mux value (which will enable differential mode).
return self._read(differential, gain, data_rate, ADS1X15_CONFIG_MODE_CONTINUOUS)
Expand Down
9 changes: 6 additions & 3 deletions adafruit_ads1x15/single_ended.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def read_adc(self, channel, gain=1, data_rate=None):
"""Read a single ADC channel and return the ADC value as a signed integer
result. Channel must be a value within 0-3.
"""
assert 0 <= channel <= 3, 'Channel must be a value within 0-3!'
if not 0 <= channel <= 3:
raise ValueError('Channel must be a value within 0-3!')
# Perform a single shot read and set the mux value to the channel plus
# the highest bit (bit 3) set.
return self._read(channel + 0x04, gain, data_rate, ADS1X15_CONFIG_MODE_SINGLE)
Expand All @@ -56,7 +57,8 @@ def read_volts(self, channel, gain=1, data_rate=None):
"""Read a single ADC channel and return the voltage value as a floating point
result. Channel must be a value within 0-3.
"""
assert 0 <= channel <= 3, 'Channel must be a value within 0-3!'
if not 0 <= channel <= 3:
raise ValueError('Channel must be a value within 0-3!')
raw = self.read_adc(channel, gain, data_rate)
volts = raw * (ADS1X15_PGA_RANGE[gain] / (2**(self.bits-1) - 1))
return volts
Expand All @@ -67,7 +69,8 @@ def start_adc(self, channel, gain=1, data_rate=None):
function to read the most recent conversion result. Call stop_adc() to
stop conversions.
"""
assert 0 <= channel <= 3, 'Channel must be a value within 0-3!'
if not 0 <= channel <= 3:
raise ValueError('Channel must be a value within 0-3!')
# Start continuous reads and set the mux value to the channel plus
# the highest bit (bit 3) set.
return self._read(channel + 0x04, gain, data_rate, ADS1X15_CONFIG_MODE_CONTINUOUS)
Expand Down