Skip to content

Commit 189fd90

Browse files
author
caternuson
committed
refactor reg read/write, add wait for conversion
1 parent ca78c9a commit 189fd90

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

adafruit_ads1x15/adafruit_ads1x15.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,23 @@ 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+
# LSB MSB
149+
return self.buf[2], self.buf[1]
150+
134151
def _data_rate_default(self):
135152
"""Retrieve the default data rate for this ADC (in samples per second).
136153
Should be implemented by subclasses.
@@ -164,6 +181,13 @@ def _read_channel_volts(self, channel):
164181
"""
165182
raise NotImplementedError('Subclass must implement _read_channel_volts function!')
166183

184+
def _conversion_complete(self):
185+
"""Return status of ADC conversion."""
186+
# OS is bit 15
187+
# OS = 0: Device is currently performing a conversion
188+
# OS = 1: Device is not currently performing a conversion
189+
return self._read_register(ADS1X15_POINTER_CONFIG)[1] & 0x80
190+
167191
def _read(self, mux, gain, data_rate, mode):
168192
"""Perform an ADC read with the provided mux, gain, data_rate, and mode
169193
values. Returns the signed integer result of the read.
@@ -186,40 +210,25 @@ def _read(self, mux, gain, data_rate, mode):
186210
config |= self._data_rate_config(data_rate)
187211
config |= ADS1X15_CONFIG_COMP_QUE_DISABLE # Disble comparator mode.
188212
# 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])
213+
self._write_register(ADS1X15_POINTER_CONFIG, config)
214+
# Wait for conversion to complete
215+
while not self._conversion_complete():
216+
pass
217+
# Return the result
218+
return self.get_last_result()
203219

204220
def stop_adc(self):
205221
"""Stop all continuous ADC conversions (either normal or difference mode).
206222
"""
207223
# Set the config register to its default value of 0x8583 to stop
208224
# 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)
225+
self._write_register(ADS1X15_POINTER_CONFIG, 0x8583)
214226

215227
def get_last_result(self):
216228
"""Read the last conversion result when in continuous conversion mode.
217229
Will return a signed integer value.
218230
"""
219231
# Retrieve the conversion register value, convert to a signed int, and
220232
# 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])
233+
lsb, msb = self._read_register(ADS1X15_POINTER_CONVERSION)
234+
return self._conversion_value(lsb, msb)

0 commit comments

Comments
 (0)