Skip to content

Add wait for conversion #12

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 6 commits into from
Jul 10, 2018
Merged
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
56 changes: 32 additions & 24 deletions adafruit_ads1x15/adafruit_ads1x15.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,22 @@ def __init__(self, i2c, address=ADS1X15_DEFAULT_ADDRESS):
ADC_Channel(self, 2),
ADC_Channel(self, 3)]

def _write_register(self, reg, value):
"""Write 16 bit value to register."""
self.buf[0] = reg
self.buf[1] = (value >> 8) & 0xFF
self.buf[2] = value & 0xFF
with self.i2c_device as i2c:
i2c.write(self.buf)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for not using Adafruit_CircuitPython_Register?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just haven't used it yet. I'll take a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferring this to a future PR.


def _read_register(self, reg):
"""Return 16 bit register value as tuple of (LSB, MSB)."""
self.buf[0] = reg
with self.i2c_device as i2c:
i2c.write(self.buf, end=1, stop=False)
i2c.readinto(self.buf, start=1)
return self.buf[1] << 8 | self.buf[2]

def _data_rate_default(self):
"""Retrieve the default data rate for this ADC (in samples per second).
Should be implemented by subclasses.
Expand Down Expand Up @@ -164,6 +180,13 @@ def _read_channel_volts(self, channel):
"""
raise NotImplementedError('Subclass must implement _read_channel_volts function!')

def _conversion_complete(self):
"""Return status of ADC conversion."""
# OS is bit 15
# OS = 0: Device is currently performing a conversion
# OS = 1: Device is not currently performing a conversion
return self._read_register(ADS1X15_POINTER_CONFIG) & 0x8000

def _read(self, mux, gain, data_rate, mode):
"""Perform an ADC read with the provided mux, gain, data_rate, and mode
values. Returns the signed integer result of the read.
Expand All @@ -186,40 +209,25 @@ def _read(self, mux, gain, data_rate, mode):
config |= self._data_rate_config(data_rate)
config |= ADS1X15_CONFIG_COMP_QUE_DISABLE # Disble comparator mode.
# Send the config value to start the ADC conversion.
# Explicitly break the 16-bit value down to a big endian pair of bytes.
self.buf[0] = ADS1X15_POINTER_CONFIG
self.buf[1] = (config >> 8) & 0xFF
self.buf[2] = config & 0xFF
with self.i2c_device as i2c:
i2c.write(self.buf)
# Wait for the ADC sample to finish based on the sample rate plus a
# small offset to be sure (0.1 millisecond).
time.sleep(1.0/data_rate+0.0001)
# Retrieve the result.
self.buf[0] = ADS1X15_POINTER_CONVERSION
i2c.write(self.buf, end=1, stop=False)
i2c.readinto(self.buf, start=1)
return self._conversion_value(self.buf[2], self.buf[1])
self._write_register(ADS1X15_POINTER_CONFIG, config)
# Wait for conversion to complete
while not self._conversion_complete():
time.sleep(0.01)
# Return the result
return self.get_last_result()

def stop_adc(self):
"""Stop all continuous ADC conversions (either normal or difference mode).
"""
# Set the config register to its default value of 0x8583 to stop
# continuous conversions.
self.buf[0] = ADS1X15_POINTER_CONFIG
self.buf[1] = 0x85
self.buf[2] = 0x83
with self.i2c_device as i2c:
i2c.write(self.buf)
self._write_register(ADS1X15_POINTER_CONFIG, 0x8583)

def get_last_result(self):
"""Read the last conversion result when in continuous conversion mode.
Will return a signed integer value.
"""
# Retrieve the conversion register value, convert to a signed int, and
# return it.
self.buf[0] = ADS1X15_POINTER_CONVERSION
with self.i2c_device as i2c:
i2c.write(self.buf, end=1, stop=False)
i2c.readinto(self.buf, start=1)
return self._conversion_value(self.buf[2], self.buf[1])
result = self._read_register(ADS1X15_POINTER_CONVERSION)
return self._conversion_value(result & 0xff, result >> 8)