Skip to content

Commit 8a5cb46

Browse files
authored
Merge pull request #12 from caternuson/wait_for_conversion
Add wait for conversion
2 parents ca78c9a + e028d90 commit 8a5cb46

File tree

1 file changed

+32
-24
lines changed

1 file changed

+32
-24
lines changed

adafruit_ads1x15/adafruit_ads1x15.py

+32-24
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,22 @@ def __init__(self, i2c, address=ADS1X15_DEFAULT_ADDRESS):
131131
ADC_Channel(self, 2),
132132
ADC_Channel(self, 3)]
133133

134+
def _write_register(self, reg, value):
135+
"""Write 16 bit value to register."""
136+
self.buf[0] = reg
137+
self.buf[1] = (value >> 8) & 0xFF
138+
self.buf[2] = value & 0xFF
139+
with self.i2c_device as i2c:
140+
i2c.write(self.buf)
141+
142+
def _read_register(self, reg):
143+
"""Return 16 bit register value as tuple of (LSB, MSB)."""
144+
self.buf[0] = reg
145+
with self.i2c_device as i2c:
146+
i2c.write(self.buf, end=1, stop=False)
147+
i2c.readinto(self.buf, start=1)
148+
return self.buf[1] << 8 | self.buf[2]
149+
134150
def _data_rate_default(self):
135151
"""Retrieve the default data rate for this ADC (in samples per second).
136152
Should be implemented by subclasses.
@@ -164,6 +180,13 @@ def _read_channel_volts(self, channel):
164180
"""
165181
raise NotImplementedError('Subclass must implement _read_channel_volts function!')
166182

183+
def _conversion_complete(self):
184+
"""Return status of ADC conversion."""
185+
# OS is bit 15
186+
# OS = 0: Device is currently performing a conversion
187+
# OS = 1: Device is not currently performing a conversion
188+
return self._read_register(ADS1X15_POINTER_CONFIG) & 0x8000
189+
167190
def _read(self, mux, gain, data_rate, mode):
168191
"""Perform an ADC read with the provided mux, gain, data_rate, and mode
169192
values. Returns the signed integer result of the read.
@@ -186,40 +209,25 @@ def _read(self, mux, gain, data_rate, mode):
186209
config |= self._data_rate_config(data_rate)
187210
config |= ADS1X15_CONFIG_COMP_QUE_DISABLE # Disble comparator mode.
188211
# Send the config value to start the ADC conversion.
189-
# Explicitly break the 16-bit value down to a big endian pair of bytes.
190-
self.buf[0] = ADS1X15_POINTER_CONFIG
191-
self.buf[1] = (config >> 8) & 0xFF
192-
self.buf[2] = config & 0xFF
193-
with self.i2c_device as i2c:
194-
i2c.write(self.buf)
195-
# Wait for the ADC sample to finish based on the sample rate plus a
196-
# small offset to be sure (0.1 millisecond).
197-
time.sleep(1.0/data_rate+0.0001)
198-
# Retrieve the result.
199-
self.buf[0] = ADS1X15_POINTER_CONVERSION
200-
i2c.write(self.buf, end=1, stop=False)
201-
i2c.readinto(self.buf, start=1)
202-
return self._conversion_value(self.buf[2], self.buf[1])
212+
self._write_register(ADS1X15_POINTER_CONFIG, config)
213+
# Wait for conversion to complete
214+
while not self._conversion_complete():
215+
time.sleep(0.01)
216+
# Return the result
217+
return self.get_last_result()
203218

204219
def stop_adc(self):
205220
"""Stop all continuous ADC conversions (either normal or difference mode).
206221
"""
207222
# Set the config register to its default value of 0x8583 to stop
208223
# continuous conversions.
209-
self.buf[0] = ADS1X15_POINTER_CONFIG
210-
self.buf[1] = 0x85
211-
self.buf[2] = 0x83
212-
with self.i2c_device as i2c:
213-
i2c.write(self.buf)
224+
self._write_register(ADS1X15_POINTER_CONFIG, 0x8583)
214225

215226
def get_last_result(self):
216227
"""Read the last conversion result when in continuous conversion mode.
217228
Will return a signed integer value.
218229
"""
219230
# Retrieve the conversion register value, convert to a signed int, and
220231
# return it.
221-
self.buf[0] = ADS1X15_POINTER_CONVERSION
222-
with self.i2c_device as i2c:
223-
i2c.write(self.buf, end=1, stop=False)
224-
i2c.readinto(self.buf, start=1)
225-
return self._conversion_value(self.buf[2], self.buf[1])
232+
result = self._read_register(ADS1X15_POINTER_CONVERSION)
233+
return self._conversion_value(result & 0xff, result >> 8)

0 commit comments

Comments
 (0)