@@ -93,12 +93,12 @@ class Mux(CV):
93
93
94
94
Range .add_values (
95
95
(
96
- ("RANGE_6_144V" , 0x0 , "± 6.144 V" , 3 ),
97
- ("RANGE_4_096V" , 0x1 , "± 4.096 V" , 2 ),
98
- ("RANGE_2_048V" , 0x2 , "± 2.048 V" , 1 ),
99
- ("RANGE_1_024V" , 0x3 , "± 1.024 V" , 0.5 ),
100
- ("RANGE_0_512V" , 0x4 , "± 0.512 V" , 0.25 ),
101
- ("RANGE_0_256V" , 0x5 , "± 0.256 V" , 0.125 ),
96
+ ("RANGE_6_144V" , 0x0 , 6.144 , 3 ),
97
+ ("RANGE_4_096V" , 0x1 , 4.096 , 2 ),
98
+ ("RANGE_2_048V" , 0x2 , 2.048 , 1 ),
99
+ ("RANGE_1_024V" , 0x3 , 1.024 , 0.5 ),
100
+ ("RANGE_0_512V" , 0x4 , 0.512 , 0.25 ),
101
+ ("RANGE_0_256V" , 0x5 , 0.256 , 0.125 ),
102
102
)
103
103
)
104
104
DataRate .add_values (
@@ -131,7 +131,7 @@ class Mux(CV):
131
131
# GND – 0.3 V < V(AINX) < VDD + 0.3 V
132
132
133
133
134
- class TLA2024 :
134
+ class TLA2024 : # pylint:disable=too-many-instance-attributes
135
135
"""I2C Interface for analog voltage measurements using the TI TLA2024 12-bit 4-channel ADC
136
136
137
137
params:
@@ -154,8 +154,9 @@ def __init__(self, i2c_bus, address=_TLA_DEFAULT_ADDRESS):
154
154
self ._last_one_shot = None
155
155
self .mode = Mode .CONTINUOUS
156
156
self .mux = Mux .MUX_AIN0_GND
157
+ # default to widest range and highest sample rate
157
158
self .data_rate = DataRate .RATE_3300SPS
158
- self .range = Range .RANGE_2_048V
159
+ self .range = Range .RANGE_6_144V
159
160
160
161
@property
161
162
def voltage (self ):
@@ -187,7 +188,7 @@ def mode(self):
187
188
@mode .setter
188
189
def mode (self , mode ):
189
190
if not Mode .is_valid (mode ):
190
- raise AttributeError ("``mode`` must be a valid `` Mode`` " )
191
+ raise AttributeError ("``mode`` must be a valid Mode" )
191
192
if mode == Mode .CONTINUOUS : # pylint:disable=no-member
192
193
self ._mode = mode
193
194
return
@@ -199,6 +200,18 @@ def mode(self, mode):
199
200
200
201
self ._last_one_shot = self ._read_volts ()
201
202
203
+ @property
204
+ def range (self ):
205
+ """The measurement range of the ADC, changed by adjusting the Programmable Gain Amplifier
206
+ `range` must be an ``adafruit_tla202x.Range``"""
207
+ return self ._pga
208
+
209
+ @range .setter
210
+ def range (self , measurement_range ):
211
+ if not Range .is_valid (measurement_range ):
212
+ raise AttributeError ("range must be a valid Range" )
213
+ self ._pga = measurement_range
214
+
202
215
@property
203
216
def data_rate (self ):
204
217
"""selects the rate at which measurement samples are taken. Must be an
@@ -208,7 +221,7 @@ def data_rate(self):
208
221
@data_rate .setter
209
222
def data_rate (self , rate ):
210
223
if not DataRate .is_valid (rate ): # pylint:disable=no-member
211
- raise AttributeError ("``data_rate`` must be a valid `` DataRate`` " )
224
+ raise AttributeError ("``data_rate`` must be a valid DataRate" )
212
225
self ._data_rate = rate
213
226
214
227
@property
@@ -220,15 +233,17 @@ def mux(self):
220
233
221
234
@mux .setter
222
235
def mux (self , mux_connection ):
236
+ print ("muxin'" )
223
237
if not Mux .is_valid (mux_connection ): # pylint:disable=no-member
224
- raise AttributeError ("``mux`` must be a valid `` Mux`` " )
238
+ raise AttributeError ("``mux`` must be a valid Mux" )
225
239
self ._mux = mux_connection
226
240
227
241
def read (self , channel ):
228
- """Switch to the given channel and take a single voltage reading in One Shot mode"""
229
- self .input_channel = channel
242
+ """Switch to the given channel and take a single ADC reading in One Shot mode"""
243
+ if not self .input_channel == channel :
244
+ self .input_channel = channel
230
245
self .mode = Mode .ONE_SHOT # pylint:disable=no-member
231
- return self .voltage
246
+ return self ._read_adc ()
232
247
233
248
def _read_volts (self ):
234
249
"""From datasheet:
@@ -240,16 +255,18 @@ def _read_volts(self):
240
255
241
256
"""
242
257
243
- value_lsb = self ._read_lsb ()
244
- v_fsr = 8192 >> self .range
245
- if self .range == 0 :
246
- v_fsr = 6144
247
- v_fsr = float (v_fsr )
248
- converted = (value_lsb / 2047.0 ) * v_fsr
258
+ value_lsb = self ._read_adc ()
259
+ # print("value_lsb:", hex(value_lsb))
260
+ # v_fsr = 8192 >> self.range
261
+ # if self.range == 0:
262
+ # v_fsr = 6144
263
+ # v_fsr = float(v_fsr)
264
+ # converted = (value_lsb / 2047.0) * v_fsr
249
265
250
- return converted / 1000.0
266
+ # return converted / 1000.0
267
+ return value_lsb * Range .lsb [self .range ] / 1000.0
251
268
252
- def _read_lsb (self ):
269
+ def _read_adc (self ):
253
270
value_lsb = self ._raw_adc_read
254
271
value_lsb >>= 4
255
272
0 commit comments