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