@@ -280,128 +280,6 @@ def xfer(self, chan, data):
280
280
return reply
281
281
282
282
283
- class DACCHAN :
284
- def __init__ (self , name , span , channum , ** kwargs ):
285
- self .name = name
286
- self .channum = channum
287
- self .VREF = kwargs .get ('VREF' , 0 )
288
- self .SwitchedOff = kwargs .get ('STATE' , 0 )
289
- self .range = span
290
- slope = (span [1 ] - span [0 ])
291
- intercept = span [0 ]
292
- self .VToCode = np .poly1d ([4095. / slope , - 4095. * intercept / slope ])
293
- self .CodeToV = np .poly1d ([slope / 4095. , intercept ])
294
- self .calibration_enabled = False
295
- self .calibration_table = []
296
- self .slope = 1
297
- self .offset = 0
298
-
299
- def load_calibration_table (self , table ):
300
- self .calibration_enabled = 'table'
301
- self .calibration_table = table
302
-
303
- def load_calibration_twopoint (self , slope , offset ):
304
- self .calibration_enabled = 'twopoint'
305
- self .slope = slope
306
- self .offset = offset
307
-
308
- # print('########################',slope,offset)
309
-
310
- def apply_calibration (self , v ):
311
- if self .calibration_enabled == 'table' : # Each point is individually calibrated
312
- return int (np .clip (v + self .calibration_table [v ], 0 , 4095 ))
313
- elif self .calibration_enabled == 'twopoint' : # Overall slope and offset correction is applied
314
- # print (self.slope,self.offset,v)
315
- return int (np .clip (v * self .slope + self .offset , 0 , 4095 ))
316
- else :
317
- return v
318
-
319
-
320
- class MCP4728 :
321
- defaultVDD = 3300
322
- RESET = 6
323
- WAKEUP = 9
324
- UPDATE = 8
325
- WRITEALL = 64
326
- WRITEONE = 88
327
- SEQWRITE = 80
328
- VREFWRITE = 128
329
- GAINWRITE = 192
330
- POWERDOWNWRITE = 160
331
- GENERALCALL = 0
332
-
333
- # def __init__(self,I2C,vref=3.3,devid=0):
334
- def __init__ (self , H , vref = 3.3 , devid = 0 ):
335
- self .devid = devid
336
- self .addr = 0x60 | self .devid # 0x60 is the base address
337
- self .H = H
338
- self .I2C = I2C (self .H )
339
- self .SWITCHEDOFF = [0 , 0 , 0 , 0 ]
340
- self .VREFS = [0 , 0 , 0 , 0 ] # 0=Vdd,1=Internal reference
341
- self .CHANS = {'PCS' : DACCHAN ('PCS' , [0 , 3.3e-3 ], 0 ), 'PV3' : DACCHAN ('PV3' , [0 , 3.3 ], 1 ),
342
- 'PV2' : DACCHAN ('PV2' , [- 3.3 , 3.3 ], 2 ), 'PV1' : DACCHAN ('PV1' , [- 5. , 5. ], 3 )}
343
- self .CHANNEL_MAP = {0 : 'PCS' , 1 : 'PV3' , 2 : 'PV2' , 3 : 'PV1' }
344
- self .values = {'PV1' : 0 , 'PV2' : 0 , 'PV3' : 0 , 'PCS' : 0 }
345
-
346
- def __ignoreCalibration__ (self , name ):
347
- self .CHANS [name ].calibration_enabled = False
348
-
349
- def setVoltage (self , name , v ):
350
- chan = self .CHANS [name ]
351
- v = int (round (chan .VToCode (v )))
352
- return self .__setRawVoltage__ (name , v )
353
-
354
- def getVoltage (self , name ):
355
- return self .values [name ]
356
-
357
- def setCurrent (self , v ):
358
- chan = self .CHANS ['PCS' ]
359
- v = int (round (chan .VToCode (v )))
360
- return self .__setRawVoltage__ ('PCS' , v )
361
-
362
- def __setRawVoltage__ (self , name , v ):
363
- v = int (np .clip (v , 0 , 4095 ))
364
- CHAN = self .CHANS [name ]
365
- '''
366
- self.H.__sendByte__(CP.DAC) #DAC write coming through.(MCP4728)
367
- self.H.__sendByte__(CP.SET_DAC)
368
- self.H.__sendByte__(self.addr<<1) #I2C address
369
- self.H.__sendByte__(CHAN.channum) #DAC channel
370
- if self.calibration_enabled[name]:
371
- val = v+self.calibration_tables[name][v]
372
- #print (val,v,self.calibration_tables[name][v])
373
- self.H.__sendInt__((CHAN.VREF << 15) | (CHAN.SwitchedOff << 13) | (0 << 12) | (val) )
374
- else:
375
- self.H.__sendInt__((CHAN.VREF << 15) | (CHAN.SwitchedOff << 13) | (0 << 12) | v )
376
-
377
- self.H.__get_ack__()
378
- '''
379
- val = self .CHANS [name ].apply_calibration (v )
380
- self .I2C .writeBulk (self .addr , [64 | (CHAN .channum << 1 ), (val >> 8 ) & 0x0F , val & 0xFF ])
381
- self .values [name ] = CHAN .CodeToV (v )
382
- return self .values [name ]
383
-
384
- def __writeall__ (self , v1 , v2 , v3 , v4 ):
385
- self .I2C .start (self .addr , 0 )
386
- self .I2C .send ((v1 >> 8 ) & 0xF )
387
- self .I2C .send (v1 & 0xFF )
388
- self .I2C .send ((v2 >> 8 ) & 0xF )
389
- self .I2C .send (v2 & 0xFF )
390
- self .I2C .send ((v3 >> 8 ) & 0xF )
391
- self .I2C .send (v3 & 0xFF )
392
- self .I2C .send ((v4 >> 8 ) & 0xF )
393
- self .I2C .send (v4 & 0xFF )
394
- self .I2C .stop ()
395
-
396
- def stat (self ):
397
- self .I2C .start (self .addr , 0 )
398
- self .I2C .send (0x0 ) # read raw values starting from address
399
- self .I2C .restart (self .addr , 1 )
400
- vals = self .I2C .read (24 )
401
- self .I2C .stop ()
402
- print (vals )
403
-
404
-
405
283
class NRF24L01 ():
406
284
# Commands
407
285
R_REG = 0x00
0 commit comments