Skip to content

Commit

Permalink
Merge pull request #16 from iBug/patch-1
Browse files Browse the repository at this point in the history
Change assertions to raising appropriate Exceptions
  • Loading branch information
caternuson authored Oct 4, 2018
2 parents 7f16dc9 + 1754752 commit c6b9d5c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
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

0 comments on commit c6b9d5c

Please sign in to comment.