55
55
56
56
_MCP4728_DEFAULT_ADDRESS = 0x60
57
57
58
- _MCP4728_CH_A_SINGLE = 0x58
59
- _MCP4728_CH_B_SINGLE = 0x5A
60
- _MCP4728_CH_C_SINGLE = 0x5C
61
- _MCP4728_CH_D_SINGLE = 0x5E
58
+ """
59
+ # DAC1, DAC0 DAC Channel Selection bits:
60
+ # 00 = Channel A
61
+ # 01 = Channel B
62
+ # 10 = Channel C
63
+ # 11 = Channel D
64
+
65
+ 0 = slave addr(auto)
66
+ 0 1 0 0 0 DAC1 DAC0 UDAC[A]
67
+ 01000 + 00 + UDAC =
68
+ 0b01000000 = 0x40 (+2 for each successive)
69
+ VREF PD1 PD0 Gx D11 D10 D9 D8 [A]
70
+ D7 D6 D5 D4 D3 D2 D1 D0 [A]
71
+
72
+ 0 1 0 0 0 Multi-Write for DAC
73
+ 0 1 0 1 0 Sequential Write for DAC Input Registers and EEPROM
74
+ 0 1 0 1 1 Single Write for DAC Input Register and EEPROM
75
+
76
+ 1 0 0 0 0 Write Reference (VREF) selection bits to Input Registers
77
+ 1 1 0 0 0 Write Gain selection bits to Input Registers
78
+ 1 0 1 0 0 Write Power-Down bits to Input Registers
79
+ """
80
+
81
+ '0b 010 00 000'
82
+ _MCP4728_CH_A_MULTI_IB = 0x40
83
+ _MCP4728_CH_B_MULTI_IB = 0x42
84
+ _MCP4728_CH_C_MULTI_IB = 0x44
85
+ _MCP4728_CH_D_MULTI_IB = 0x46
86
+
87
+ '0b 010 11 000'
88
+ _MCP4728_CH_A_SINGLE_EEPROM = 0x58
89
+ _MCP4728_CH_B_SINGLE_EEPROM = 0x5A
90
+ _MCP4728_CH_C_SINGLE_EEPROM = 0x5C
91
+ _MCP4728_CH_D_SINGLE_EEPROM = 0x5E
92
+
93
+ '0b 010 10 000'
94
+ _MCP4728_CH_A_MULTI_EEPROM = 0x50
62
95
63
96
class MCP4728 :
64
97
"""Helper library for the Microchip MCP4728 I2C 12-bit Quad DAC.
@@ -67,11 +100,17 @@ class MCP4728:
67
100
:param address: The I2C slave address of the sensor
68
101
69
102
"""
103
+ _channel_a_single_write_eeprom = UnaryStruct (_MCP4728_CH_A_SINGLE_EEPROM , ">H" )
104
+ _channel_b_single_write_eeprom = UnaryStruct (_MCP4728_CH_B_SINGLE_EEPROM , ">H" )
105
+ _channel_c_single_write_eeprom = UnaryStruct (_MCP4728_CH_C_SINGLE_EEPROM , ">H" )
106
+ _channel_d_single_write_eeprom = UnaryStruct (_MCP4728_CH_D_SINGLE_EEPROM , ">H" )
107
+
108
+ _channel_a_multi_write = UnaryStruct (_MCP4728_CH_A_MULTI_IB , ">H" )
109
+ _channel_b_multi_write = UnaryStruct (_MCP4728_CH_B_MULTI_IB , ">H" )
110
+ _channel_c_multi_write = UnaryStruct (_MCP4728_CH_C_MULTI_IB , ">H" )
111
+ _channel_d_multi_write = UnaryStruct (_MCP4728_CH_D_MULTI_IB , ">H" )
70
112
71
- _channel_a_single_write = UnaryStruct (_MCP4728_CH_A_SINGLE , ">H" )
72
- _channel_b_single_write = UnaryStruct (_MCP4728_CH_B_SINGLE , ">H" )
73
- _channel_c_single_write = UnaryStruct (_MCP4728_CH_C_SINGLE , ">H" )
74
- _channel_d_single_write = UnaryStruct (_MCP4728_CH_D_SINGLE , ">H" )
113
+ _multi_write_channel_a_start = UnaryStruct (_MCP4728_CH_A_MULTI_EEPROM , ">HHHH" )
75
114
76
115
def __init__ (self , i2c_bus , address = _MCP4728_DEFAULT_ADDRESS ):
77
116
self .i2c_device = i2c_device .I2CDevice (i2c_bus , address )
@@ -83,7 +122,7 @@ def channel_a(self):
83
122
84
123
@channel_a .setter
85
124
def channel_a (self , value ):
86
- self ._channel_a_single_write = value
125
+ self ._channel_a_multi = value
87
126
88
127
89
128
@property
@@ -93,7 +132,7 @@ def channel_b(self):
93
132
94
133
@channel_b .setter
95
134
def channel_b (self , value ):
96
- self ._channel_b_single_write = value
135
+ self ._channel_b_multi = value
97
136
98
137
@property
99
138
def channel_c (self ):
@@ -102,7 +141,7 @@ def channel_c(self):
102
141
103
142
@channel_c .setter
104
143
def channel_c (self , value ):
105
- self ._channel_c_single_write = value
144
+ self ._channel_c_multi = value
106
145
107
146
@property
108
147
def channel_d (self ):
@@ -111,4 +150,108 @@ def channel_d(self):
111
150
112
151
@channel_d .setter
113
152
def channel_d (self , value ):
114
- self ._channel_d_single_write = value
153
+ self ._channel_d_multi = value
154
+
155
+
156
+ def write_init (self , register_address , struct_format ):
157
+ self .format = struct_format
158
+ self .address = register_address
159
+
160
+ # def read(self, obj):
161
+ # buf = bytearray(1+struct.calcsize(self.format))
162
+ # buf[0] = self.address
163
+ # with self.i2c_device as i2c:
164
+ # i2c.write_then_readinto(buf, buf, out_end=1, in_start=1)
165
+ # return struct.unpack_from(self.format, buf, 1)[0]
166
+
167
+ # def write(self, obj, value):
168
+ # buf = bytearray(1+struct.calcsize(self.format))
169
+ # buf[0] = self.address
170
+ # struct.pack_into(self.format, buf, 1, value)
171
+ # with self.i2c_device as i2c:
172
+ # i2c.write(buf)
173
+ def chunks (self , l , n ):
174
+ # For item i in a range that is a length of l,
175
+ for i in range (0 , len (l ), n ):
176
+ # Create an index range for l of n items:
177
+ yield l [i :i + n ]
178
+
179
+ def get_flags (self , high_byte ):
180
+ vref = (high_byte & 1 << 7 ) > 0
181
+ gain = (high_byte & 1 << 4 ) > 0
182
+ pd = (high_byte & 0b011 << 5 )>> 5
183
+ return (vref , gain , pd )
184
+
185
+ def read_registers (self ):
186
+ buf = bytearray (24 )
187
+
188
+ with self .i2c_device as i2c :
189
+ i2c .readinto (buf )
190
+ index = 0
191
+ for index , value in enumerate (buf ):
192
+ if index % 3 is 0 :
193
+ print ("\n %4s\t " % index , end = "" )
194
+ print ("%s %s " % ( format (value , '#010b' ), hex (value )), end = "" )
195
+ print ()
196
+ # stride is 6 because we get 6 bytes for each channel; 3 for the output regs
197
+ # and 3 for the eeprom. here we only care about the output buffer
198
+ current_values = []
199
+ for header , high_byte , low_byte , na_1 , na_2 , na_3 in self .chunks (buf ,6 ):
200
+ value = (high_byte & 0b00001111 ) << 8 | low_byte
201
+ vref , gain , pd = self .get_flags (high_byte )
202
+ current_values << (value , vref , pd , gain )
203
+
204
+ return current_values
205
+ # ch_a_header, ch_a_hb, ch_a_lb = buf[0:3]
206
+ # ch_aee_header, ch_aee_hb, ch_aee_lb = buf[3:6]
207
+ # # ch_c_header, ch_c_hb, ch_c_lb = buf[6:9]
208
+ # # ch_d_header, ch_d_hb, ch_d_lb = buf[9:12]
209
+
210
+ # ch_a_val =
211
+
212
+ # print("%s %s %s"%( self.b(ch_a_header),self.b(ch_a_hb), self.b(ch_a_lb) ))
213
+ # print("%s %s %s"%( self.b(ch_b_header),self.b(ch_b_hb), self.b(ch_b_lb) ))
214
+ # # print("%s %s %s"%( self.b(ch_c_header),self.b(ch_c_hb), self.b(ch_c_lb) ))
215
+ # # print("%s %s %s"%( self.b(ch_d_header),self.b(ch_d_hb), self.b(ch_d_lb) ))
216
+
217
+ def b (self , byte_val ):
218
+ return format (byte_val , '#010b' )
219
+
220
+ def write_multi_eeprom (self , byte_list , start = 0 ):
221
+ buffer_list = [_MCP4728_CH_A_MULTI_EEPROM ]
222
+ buffer_list += byte_list
223
+ print ("Byte List:" )
224
+ print (buffer_list )
225
+ buf = bytearray (buffer_list )
226
+ # struct.pack_into(self.format, buf, 1, value)
227
+ with self .i2c_device as i2c :
228
+ i2c .write (buf )
229
+
230
+
231
+
232
+
233
+
234
+
235
+ @property
236
+ def ch_a (self ):
237
+ return "poo"
238
+
239
+ @ch_a .setter
240
+ def ch_a (self , value ):
241
+ self ._channel_a_multi = value
242
+
243
+ """
244
+ # set the gain for a channel
245
+ cache each, set all?
246
+
247
+ # set reference source for a channel
248
+ cache each, set all?
249
+
250
+ # save settings to eeprom
251
+
252
+ # write a channel
253
+
254
+ # write all channels
255
+
256
+ # latching?
257
+ """
0 commit comments