53
53
54
54
_MCP4728_CH_A_MULTI_EEPROM = 0x50
55
55
56
+
56
57
class CV :
57
58
"""struct helper"""
58
59
@@ -73,14 +74,15 @@ def is_valid(cls, value):
73
74
"Returns true if the given value is a member of the CV"
74
75
return value in cls .string
75
76
77
+
76
78
class Vref (CV ):
77
79
"""Options for ``vref``"""
78
- pass #pylint: disable=unnecessary-pass
79
80
80
- Vref .add_values ((
81
- ('VDD' , 0 , "VDD" , None ),
82
- ('INTERNAL' , 1 , "Internal 2.048V" , None ),
83
- ))
81
+ pass # pylint: disable=unnecessary-pass
82
+
83
+
84
+ Vref .add_values ((("VDD" , 0 , "VDD" , None ), ("INTERNAL" , 1 , "Internal 2.048V" , None ),))
85
+
84
86
85
87
class MCP4728 :
86
88
"""Helper library for the Microchip MCP4728 I2C 12-bit Quad DAC.
@@ -103,9 +105,9 @@ def __init__(self, i2c_bus, address=_MCP4728_DEFAULT_ADDRESS):
103
105
104
106
@staticmethod
105
107
def _get_flags (high_byte ):
106
- vref = (high_byte & 1 << 7 ) > 0
107
- gain = (high_byte & 1 << 4 ) > 0
108
- power_state = (high_byte & 0b011 << 5 ) >> 5
108
+ vref = (high_byte & 1 << 7 ) > 0
109
+ gain = (high_byte & 1 << 4 ) > 0
110
+ power_state = (high_byte & 0b011 << 5 ) >> 5
109
111
return (vref , gain , power_state )
110
112
111
113
@staticmethod
@@ -122,7 +124,9 @@ def _read_registers(self):
122
124
# and 3 for the eeprom. Here we only care about the output regoster so we throw out
123
125
# the eeprom values as 'n/a'
124
126
current_values = []
125
- for header , high_byte , low_byte , na_1 , na_2 , na_3 in self ._chunk (buf , 6 ):#pylint:disable=unused-variable
127
+ # pylint:disable=unused-variable
128
+ for header , high_byte , low_byte , na_1 , na_2 , na_3 in self ._chunk (buf , 6 ):
129
+ # pylint:enable=unused-variable
126
130
value = (high_byte & 0b00001111 ) << 8 | low_byte
127
131
vref , gain , power_state = self ._get_flags (high_byte )
128
132
current_values .append ((value , vref , gain , power_state ))
@@ -149,15 +153,15 @@ def _write_multi_eeprom(self, byte_list):
149
153
with self .i2c_device as i2c :
150
154
i2c .write (buf )
151
155
152
- sleep (0.015 ) # the better to write you with
156
+ sleep (0.015 ) # the better to write you with
153
157
154
158
def sync_vrefs (self ):
155
159
"""Syncs the driver's vref state with the DAC"""
156
160
gain_setter_command = 0b10000000
157
- gain_setter_command |= ( self .channel_a .vref << 3 )
158
- gain_setter_command |= ( self .channel_b .vref << 2 )
159
- gain_setter_command |= ( self .channel_c .vref << 1 )
160
- gain_setter_command |= ( self .channel_d .vref )
161
+ gain_setter_command |= self .channel_a .vref << 3
162
+ gain_setter_command |= self .channel_b .vref << 2
163
+ gain_setter_command |= self .channel_c .vref << 1
164
+ gain_setter_command |= self .channel_d .vref
161
165
162
166
buf = bytearray (1 )
163
167
pack_into (">B" , buf , 0 , gain_setter_command )
@@ -168,10 +172,10 @@ def sync_gains(self):
168
172
"""Syncs the driver's gain state with the DAC"""
169
173
170
174
sync_setter_command = 0b11000000
171
- sync_setter_command |= ( self .channel_a .gain << 3 )
172
- sync_setter_command |= ( self .channel_b .gain << 2 )
173
- sync_setter_command |= ( self .channel_c .gain << 1 )
174
- sync_setter_command |= ( self .channel_d .gain )
175
+ sync_setter_command |= self .channel_a .gain << 3
176
+ sync_setter_command |= self .channel_b .gain << 2
177
+ sync_setter_command |= self .channel_c .gain << 1
178
+ sync_setter_command |= self .channel_d .gain
175
179
176
180
buf = bytearray (1 )
177
181
pack_into (">B" , buf , 0 , sync_setter_command )
@@ -183,8 +187,8 @@ def _set_value(self, channel):
183
187
184
188
channel_bytes = self ._generate_bytes_with_flags (channel )
185
189
186
- write_command_byte = 0b01000000 # 0 1 0 0 0 DAC1 DAC0 UDAC
187
- write_command_byte |= ( channel .channel_index << 1 )
190
+ write_command_byte = 0b01000000 # 0 1 0 0 0 DAC1 DAC0 UDAC
191
+ write_command_byte |= channel .channel_index << 1
188
192
189
193
output_buffer = bytearray ([write_command_byte ])
190
194
output_buffer .extend (channel_bytes )
@@ -206,23 +210,25 @@ def _generate_bytes_with_flags(channel):
206
210
def _chunk (big_list , chunk_size ):
207
211
"""Divides a given list into `chunk_size` sized chunks"""
208
212
for i in range (0 , len (big_list ), chunk_size ):
209
- yield big_list [i :i + chunk_size ]
213
+ yield big_list [i : i + chunk_size ]
214
+
210
215
211
216
class Channel :
212
217
"""An instance of a single channel for a multi-channel DAC.
213
218
214
219
**All available channels are created automatically and should not be created by the user**"""
220
+
215
221
def __init__ (self , dac_instance , cache_page , index ):
216
- self ._vref = cache_page [' vref' ]
217
- self ._gain = cache_page [' gain' ]
218
- self ._raw_value = cache_page [' value' ]
222
+ self ._vref = cache_page [" vref" ]
223
+ self ._gain = cache_page [" gain" ]
224
+ self ._raw_value = cache_page [" value" ]
219
225
self ._dac = dac_instance
220
226
self .channel_index = index
221
227
222
228
@property
223
229
def normalized_value (self ):
224
230
"""The DAC value as a floating point number in the range 0.0 to 1.0."""
225
- return self .raw_value / (2 ** 12 - 1 )
231
+ return self .raw_value / (2 ** 12 - 1 )
226
232
227
233
@normalized_value .setter
228
234
def normalized_value (self , value ):
@@ -235,12 +241,14 @@ def normalized_value(self, value):
235
241
def value (self ):
236
242
"""The 16-bit scaled current value for the channel. Note that the MCP4728 is a 12-bit piece
237
243
so quantization errors will occour"""
238
- return self .normalized_value * (2 ** 16 - 1 )
244
+ return self .normalized_value * (2 ** 16 - 1 )
239
245
240
246
@value .setter
241
247
def value (self , value ):
242
- if value < 0 or value > (2 ** 16 - 1 ):
243
- raise AttributeError ("`value` must be a 16-bit integer between 0 and %s" % (2 ** 16 - 1 ))
248
+ if value < 0 or value > (2 ** 16 - 1 ):
249
+ raise AttributeError (
250
+ "`value` must be a 16-bit integer between 0 and %s" % (2 ** 16 - 1 )
251
+ )
244
252
245
253
# Scale from 16-bit to 12-bit value (quantization errors will occur!).
246
254
self .raw_value = value >> 4
@@ -252,12 +260,14 @@ def raw_value(self):
252
260
253
261
@raw_value .setter
254
262
def raw_value (self , value ):
255
- if value < 0 or value > (2 ** 12 - 1 ):
256
- raise AttributeError ("`raw_value` must be a 12-bit integer between 0 and %s" % (2 ** 12 - 1 ))
263
+ if value < 0 or value > (2 ** 12 - 1 ):
264
+ raise AttributeError (
265
+ "`raw_value` must be a 12-bit integer between 0 and %s" % (2 ** 12 - 1 )
266
+ )
257
267
self ._raw_value = value
258
268
# disabling the protected access warning here because making it public would be
259
269
# more confusing
260
- self ._dac ._set_value (self ) # pylint:disable=protected-access
270
+ self ._dac ._set_value (self ) # pylint:disable=protected-access
261
271
262
272
@property
263
273
def gain (self ):
@@ -272,7 +282,7 @@ def gain(self):
272
282
def gain (self , value ):
273
283
if not value in (1 , 2 ):
274
284
raise AttributeError ("`gain` must be 1 or 2" )
275
- self ._gain = value - 1
285
+ self ._gain = value - 1
276
286
self ._dac .sync_gains ()
277
287
278
288
@property
0 commit comments