5353
5454_MCP4728_CH_A_MULTI_EEPROM = 0x50
5555
56+
5657class CV :
5758 """struct helper"""
5859
@@ -73,14 +74,15 @@ def is_valid(cls, value):
7374 "Returns true if the given value is a member of the CV"
7475 return value in cls .string
7576
77+
7678class Vref (CV ):
7779 """Options for ``vref``"""
78- pass #pylint: disable=unnecessary-pass
7980
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+
8486
8587class MCP4728 :
8688 """Helper library for the Microchip MCP4728 I2C 12-bit Quad DAC.
@@ -103,9 +105,9 @@ def __init__(self, i2c_bus, address=_MCP4728_DEFAULT_ADDRESS):
103105
104106 @staticmethod
105107 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
109111 return (vref , gain , power_state )
110112
111113 @staticmethod
@@ -122,7 +124,9 @@ def _read_registers(self):
122124 # and 3 for the eeprom. Here we only care about the output regoster so we throw out
123125 # the eeprom values as 'n/a'
124126 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
126130 value = (high_byte & 0b00001111 ) << 8 | low_byte
127131 vref , gain , power_state = self ._get_flags (high_byte )
128132 current_values .append ((value , vref , gain , power_state ))
@@ -149,15 +153,15 @@ def _write_multi_eeprom(self, byte_list):
149153 with self .i2c_device as i2c :
150154 i2c .write (buf )
151155
152- sleep (0.015 ) # the better to write you with
156+ sleep (0.015 ) # the better to write you with
153157
154158 def sync_vrefs (self ):
155159 """Syncs the driver's vref state with the DAC"""
156160 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
161165
162166 buf = bytearray (1 )
163167 pack_into (">B" , buf , 0 , gain_setter_command )
@@ -168,10 +172,10 @@ def sync_gains(self):
168172 """Syncs the driver's gain state with the DAC"""
169173
170174 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
175179
176180 buf = bytearray (1 )
177181 pack_into (">B" , buf , 0 , sync_setter_command )
@@ -183,8 +187,8 @@ def _set_value(self, channel):
183187
184188 channel_bytes = self ._generate_bytes_with_flags (channel )
185189
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
188192
189193 output_buffer = bytearray ([write_command_byte ])
190194 output_buffer .extend (channel_bytes )
@@ -206,23 +210,25 @@ def _generate_bytes_with_flags(channel):
206210 def _chunk (big_list , chunk_size ):
207211 """Divides a given list into `chunk_size` sized chunks"""
208212 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+
210215
211216class Channel :
212217 """An instance of a single channel for a multi-channel DAC.
213218
214219 **All available channels are created automatically and should not be created by the user**"""
220+
215221 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" ]
219225 self ._dac = dac_instance
220226 self .channel_index = index
221227
222228 @property
223229 def normalized_value (self ):
224230 """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 )
226232
227233 @normalized_value .setter
228234 def normalized_value (self , value ):
@@ -235,12 +241,14 @@ def normalized_value(self, value):
235241 def value (self ):
236242 """The 16-bit scaled current value for the channel. Note that the MCP4728 is a 12-bit piece
237243 so quantization errors will occour"""
238- return self .normalized_value * (2 ** 16 - 1 )
244+ return self .normalized_value * (2 ** 16 - 1 )
239245
240246 @value .setter
241247 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+ )
244252
245253 # Scale from 16-bit to 12-bit value (quantization errors will occur!).
246254 self .raw_value = value >> 4
@@ -252,12 +260,14 @@ def raw_value(self):
252260
253261 @raw_value .setter
254262 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+ )
257267 self ._raw_value = value
258268 # disabling the protected access warning here because making it public would be
259269 # more confusing
260- self ._dac ._set_value (self ) # pylint:disable=protected-access
270+ self ._dac ._set_value (self ) # pylint:disable=protected-access
261271
262272 @property
263273 def gain (self ):
@@ -272,7 +282,7 @@ def gain(self):
272282 def gain (self , value ):
273283 if not value in (1 , 2 ):
274284 raise AttributeError ("`gain` must be 1 or 2" )
275- self ._gain = value - 1
285+ self ._gain = value - 1
276286 self ._dac .sync_gains ()
277287
278288 @property
0 commit comments