@@ -34,10 +34,11 @@ class I2C():
34
34
35
35
"""
36
36
samples = 0
37
- total_bytes = 0
37
+ total_bytes = 0
38
38
channels = 0
39
- tg = 100
39
+ tg = 100
40
40
MAX_SAMPLES = 10000
41
+
41
42
def __init__ (self , H ):
42
43
self .H = H
43
44
from PSL import sensorlist
@@ -284,7 +285,6 @@ def read_repeat(self):
284
285
except Exception as ex :
285
286
self .raiseException (ex , "Communication Error , Function : " + inspect .currentframe ().f_code .co_name )
286
287
287
-
288
288
def read_end (self ):
289
289
try :
290
290
self .H .__sendByte__ (CP .I2C_HEADER )
@@ -295,7 +295,6 @@ def read_end(self):
295
295
except Exception as ex :
296
296
self .raiseException (ex , "Communication Error , Function : " + inspect .currentframe ().f_code .co_name )
297
297
298
-
299
298
def read_status (self ):
300
299
try :
301
300
self .H .__sendByte__ (CP .I2C_HEADER )
@@ -306,7 +305,6 @@ def read_status(self):
306
305
except Exception as ex :
307
306
self .raiseException (ex , "Communication Error , Function : " + inspect .currentframe ().f_code .co_name )
308
307
309
-
310
308
def readBulk (self , device_address , register_address , bytes_to_read ):
311
309
try :
312
310
self .H .__sendByte__ (CP .I2C_HEADER )
@@ -379,7 +377,7 @@ def scan(self, frequency=100000, verbose=False):
379
377
self .stop ()
380
378
return addrs
381
379
382
- def __captureStart__ (self ,address ,location ,sample_length ,total_samples ,tg ):
380
+ def __captureStart__ (self , address , location , sample_length , total_samples , tg ):
383
381
"""
384
382
Blocking call that starts fetching data from I2C sensors like an oscilloscope fetches voltage readings
385
383
You will then have to call `__retrievebuffer__` to fetch this data, and `__dataProcessor` to process and return separate channels
@@ -399,16 +397,15 @@ def __captureStart__(self,address,location,sample_length,total_samples,tg):
399
397
:return: Arrays X(timestamps),Y1,Y2 ...
400
398
401
399
"""
402
- if (tg < 20 ):tg = 20
403
- total_bytes = total_samples * sample_length
404
- print ('total bytes calculated : ' ,total_bytes )
405
- if (total_bytes > self .MAX_SAMPLES * 2 ):
406
- print ('Sample limit exceeded. 10,000 int / 20000 bytes total' )
407
- total_bytes = self .MAX_SAMPLES * 2
408
- total_samples = total_bytes / sample_length #2* because sample array is in Integers, and we're using it to store bytes
409
-
410
-
411
- print ('length of each channel : ' ,sample_length )
400
+ if (tg < 20 ): tg = 20
401
+ total_bytes = total_samples * sample_length
402
+ print ('total bytes calculated : ' , total_bytes )
403
+ if (total_bytes > self .MAX_SAMPLES * 2 ):
404
+ print ('Sample limit exceeded. 10,000 int / 20000 bytes total' )
405
+ total_bytes = self .MAX_SAMPLES * 2
406
+ total_samples = total_bytes / sample_length # 2* because sample array is in Integers, and we're using it to store bytes
407
+
408
+ print ('length of each channel : ' , sample_length )
412
409
self .total_bytes = total_bytes
413
410
self .channels = sample_length
414
411
self .samples = total_samples
@@ -419,55 +416,57 @@ def __captureStart__(self,address,location,sample_length,total_samples,tg):
419
416
self .H .__sendByte__ (address )
420
417
self .H .__sendByte__ (location )
421
418
self .H .__sendByte__ (sample_length )
422
- self .H .__sendInt__ (total_samples ) # total number of samples to record
423
- self .H .__sendInt__ (tg ) # Timegap between samples. 1MHz timer clock
419
+ self .H .__sendInt__ (total_samples ) # total number of samples to record
420
+ self .H .__sendInt__ (tg ) # Timegap between samples. 1MHz timer clock
424
421
self .H .__get_ack__ ()
425
- return 1e-6 * self .samples * self .tg + .01
422
+ return 1e-6 * self .samples * self .tg + .01
426
423
427
424
def __retrievebuffer__ (self ):
428
425
'''
429
426
Fetch data acquired by the I2C scope. refer to :func:`__captureStart__`
430
427
'''
431
- total_int_samples = self .total_bytes / 2
428
+ total_int_samples = self .total_bytes / 2
432
429
DATA_SPLITTING = 500
433
- print ('fetchin samples : ' ,total_int_samples ,' split' ,DATA_SPLITTING )
434
- data = b''
435
- for i in range (int (total_int_samples / DATA_SPLITTING )):
430
+ print ('fetchin samples : ' , total_int_samples , ' split' , DATA_SPLITTING )
431
+ data = b''
432
+ for i in range (int (total_int_samples / DATA_SPLITTING )):
436
433
self .H .__sendByte__ (CP .ADC )
437
434
self .H .__sendByte__ (CP .GET_CAPTURE_CHANNEL )
438
- self .H .__sendByte__ (0 ) # starts with A0 on PIC
435
+ self .H .__sendByte__ (0 ) # starts with A0 on PIC
439
436
self .H .__sendInt__ (DATA_SPLITTING )
440
- self .H .__sendInt__ (i * DATA_SPLITTING )
441
- rem = DATA_SPLITTING * 2 + 1
437
+ self .H .__sendInt__ (i * DATA_SPLITTING )
438
+ rem = DATA_SPLITTING * 2 + 1
442
439
for _ in range (200 ):
443
- partial = self .H .fd .read (rem ) #reading int by int sometimes causes a communication error. this works better.
444
- rem -= len (partial )
445
- data += partial
446
- #print ('partial: ',len(partial), end=",")
447
- if rem <= 0 :
440
+ partial = self .H .fd .read (
441
+ rem ) # reading int by int sometimes causes a communication error. this works better.
442
+ rem -= len (partial )
443
+ data += partial
444
+ # print ('partial: ',len(partial), end=",")
445
+ if rem <= 0 :
448
446
break
449
- data = data [:- 1 ]
450
- #print ('Pass : len=',len(data), ' i = ',i)
447
+ data = data [:- 1 ]
448
+ # print ('Pass : len=',len(data), ' i = ',i)
451
449
452
- if total_int_samples % DATA_SPLITTING :
450
+ if total_int_samples % DATA_SPLITTING :
453
451
self .H .__sendByte__ (CP .ADC )
454
452
self .H .__sendByte__ (CP .GET_CAPTURE_CHANNEL )
455
- self .H .__sendByte__ (0 ) # starts with A0 on PIC
456
- self .H .__sendInt__ (total_int_samples % DATA_SPLITTING )
457
- self .H .__sendInt__ (total_int_samples - total_int_samples % DATA_SPLITTING )
458
- rem = 2 * (total_int_samples % DATA_SPLITTING )+ 1
453
+ self .H .__sendByte__ (0 ) # starts with A0 on PIC
454
+ self .H .__sendInt__ (total_int_samples % DATA_SPLITTING )
455
+ self .H .__sendInt__ (total_int_samples - total_int_samples % DATA_SPLITTING )
456
+ rem = 2 * (total_int_samples % DATA_SPLITTING ) + 1
459
457
for _ in range (20 ):
460
- partial = self .H .fd .read (rem ) #reading int by int sometimes causes a communication error. this works better.
461
- rem -= len (partial )
462
- data += partial
463
- #print ('partial: ',len(partial), end="")
464
- if rem <= 0 :
458
+ partial = self .H .fd .read (
459
+ rem ) # reading int by int sometimes causes a communication error. this works better.
460
+ rem -= len (partial )
461
+ data += partial
462
+ # print ('partial: ',len(partial), end="")
463
+ if rem <= 0 :
465
464
break
466
- data = data [:- 1 ]
467
- print ('Final Pass : len=' ,len (data ))
465
+ data = data [:- 1 ]
466
+ print ('Final Pass : len=' , len (data ))
468
467
return data
469
468
470
- def __dataProcessor__ (self ,data ,* args ):
469
+ def __dataProcessor__ (self , data , * args ):
471
470
'''
472
471
Interpret data acquired by the I2C scope. refer to :func:`__retrievebuffer__` to fetch data
473
472
@@ -482,19 +481,19 @@ def __dataProcessor__(self,data,*args):
482
481
483
482
try :
484
483
data = [ord (a ) for a in data ]
485
- if ('int' in args ):
486
- for a in range (self .channels * self .samples / 2 ): self .buff [a ] = np .int16 ((data [a * 2 ]<< 8 )| data [a * 2 + 1 ])
484
+ if ('int' in args ):
485
+ for a in range (self .channels * self .samples / 2 ): self .buff [a ] = np .int16 (
486
+ (data [a * 2 ] << 8 ) | data [a * 2 + 1 ])
487
487
else :
488
- for a in range (self .channels * self .samples ): self .buff [a ] = data [a ]
488
+ for a in range (self .channels * self .samples ): self .buff [a ] = data [a ]
489
489
490
- yield np .linspace (0 ,self .tg * (self .samples - 1 ),self .samples )
491
- for a in range (int (self .channels / 2 )):
492
- yield self .buff [a :self .samples * self .channels / 2 ][::self .channels / 2 ]
490
+ yield np .linspace (0 , self .tg * (self .samples - 1 ), self .samples )
491
+ for a in range (int (self .channels / 2 )):
492
+ yield self .buff [a :self .samples * self .channels / 2 ][::self .channels / 2 ]
493
493
except Exception as ex :
494
- msg = "Incorrect number of bytes received" ,ex
494
+ msg = "Incorrect number of bytes received" , ex
495
495
raise RuntimeError (msg )
496
496
497
-
498
497
def capture (self , address , location , sample_length , total_samples , tg , * args ):
499
498
"""
500
499
Blocking call that fetches data from I2C sensors like an oscilloscope fetches voltage readings
@@ -525,10 +524,10 @@ def capture(self, address, location, sample_length, total_samples, tg, *args):
525
524
:return: Arrays X(timestamps),Y1,Y2 ...
526
525
527
526
"""
528
- t = self .__captureStart__ (address ,location ,sample_length ,total_samples ,tg )
527
+ t = self .__captureStart__ (address , location , sample_length , total_samples , tg )
529
528
time .sleep (t )
530
529
data = self .__retrievebuffer__ ()
531
- return self .__dataProcessor__ (data ,* args )
530
+ return self .__dataProcessor__ (data , * args )
532
531
533
532
534
533
class SPI ():
@@ -665,7 +664,6 @@ def send8(self, value):
665
664
except Exception as ex :
666
665
self .raiseException (ex , "Communication Error , Function : " + inspect .currentframe ().f_code .co_name )
667
666
668
-
669
667
def send16 (self , value ):
670
668
"""
671
669
SENDS 16-bit data over SPI
@@ -691,7 +689,6 @@ def send16(self, value):
691
689
except Exception as ex :
692
690
self .raiseException (ex , "Communication Error , Function : " + inspect .currentframe ().f_code .co_name )
693
691
694
-
695
692
def send8_burst (self , value ):
696
693
"""
697
694
SENDS 8-bit data over SPI
@@ -736,14 +733,15 @@ def send16_burst(self, value):
736
733
except Exception as ex :
737
734
self .raiseException (ex , "Communication Error , Function : " + inspect .currentframe ().f_code .co_name )
738
735
739
- def xfer (self ,chan ,data ):
736
+ def xfer (self , chan , data ):
740
737
self .start (chan )
741
- reply = []
738
+ reply = []
742
739
for a in data :
743
740
reply .append (self .send8 (a ))
744
741
self .stop (chan )
745
742
return reply
746
743
744
+
747
745
class DACCHAN :
748
746
def __init__ (self , name , span , channum , ** kwargs ):
749
747
self .name = name
@@ -771,7 +769,6 @@ def load_calibration_twopoint(self, slope, offset):
771
769
772
770
# print('########################',slope,offset)
773
771
774
-
775
772
def apply_calibration (self , v ):
776
773
if self .calibration_enabled == 'table' : # Each point is individually calibrated
777
774
return int (np .clip (v + self .calibration_table [v ], 0 , 4095 ))
@@ -1014,7 +1011,6 @@ def rxchar(self):
1014
1011
except Exception as ex :
1015
1012
self .raiseException (ex , "Communication Error , Function : " + inspect .currentframe ().f_code .co_name )
1016
1013
1017
-
1018
1014
def txchar (self , char ):
1019
1015
'''
1020
1016
Transmits a single character
@@ -1040,7 +1036,6 @@ def hasData(self):
1040
1036
except Exception as ex :
1041
1037
self .raiseException (ex , "Communication Error , Function : " + inspect .currentframe ().f_code .co_name )
1042
1038
1043
-
1044
1039
def flush (self ):
1045
1040
'''
1046
1041
Flushes the TX and RX FIFOs
@@ -1083,7 +1078,6 @@ def read_register(self, address):
1083
1078
except Exception as ex :
1084
1079
self .raiseException (ex , "Communication Error , Function : " + inspect .currentframe ().f_code .co_name )
1085
1080
1086
-
1087
1081
def get_status (self ):
1088
1082
'''
1089
1083
Returns a byte representing the STATUS register on the radio.
0 commit comments