@@ -131,6 +131,23 @@ def __init__(self, i2c, address=ADS1X15_DEFAULT_ADDRESS):
131
131
ADC_Channel (self , 2 ),
132
132
ADC_Channel (self , 3 )]
133
133
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
+
134
151
def _data_rate_default (self ):
135
152
"""Retrieve the default data rate for this ADC (in samples per second).
136
153
Should be implemented by subclasses.
@@ -164,6 +181,13 @@ def _read_channel_volts(self, channel):
164
181
"""
165
182
raise NotImplementedError ('Subclass must implement _read_channel_volts function!' )
166
183
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
+
167
191
def _read (self , mux , gain , data_rate , mode ):
168
192
"""Perform an ADC read with the provided mux, gain, data_rate, and mode
169
193
values. Returns the signed integer result of the read.
@@ -186,40 +210,25 @@ def _read(self, mux, gain, data_rate, mode):
186
210
config |= self ._data_rate_config (data_rate )
187
211
config |= ADS1X15_CONFIG_COMP_QUE_DISABLE # Disble comparator mode.
188
212
# 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 ()
203
219
204
220
def stop_adc (self ):
205
221
"""Stop all continuous ADC conversions (either normal or difference mode).
206
222
"""
207
223
# Set the config register to its default value of 0x8583 to stop
208
224
# 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 )
214
226
215
227
def get_last_result (self ):
216
228
"""Read the last conversion result when in continuous conversion mode.
217
229
Will return a signed integer value.
218
230
"""
219
231
# Retrieve the conversion register value, convert to a signed int, and
220
232
# 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