@@ -534,7 +534,7 @@ def get_progress(self) -> int:
534
534
a += 1
535
535
536
536
p = CP .MAX_SAMPLES // 4
537
- progress = self ._get_initial_states_and_progress ()[1 ]
537
+ progress = self ._get_initial_states_and_progress ()[0 ]
538
538
for a in active_channels :
539
539
p = min (progress [a ], p )
540
540
@@ -550,7 +550,38 @@ def get_initial_states(self) -> Dict[str, bool]:
550
550
state, e.g. {'LA1': True, 'LA2': True, 'LA3': True, 'LA4': False}.
551
551
True means HIGH, False means LOW.
552
552
"""
553
- return self ._get_initial_states_and_progress ()[0 ]
553
+ before , after = self ._get_initial_states_and_progress ()[1 :]
554
+ initial_states = {
555
+ "LA1" : (before & 1 != 0 ),
556
+ "LA2" : (before & 2 != 0 ),
557
+ "LA3" : (before & 4 != 0 ),
558
+ "LA4" : (before & 8 != 0 ),
559
+ }
560
+
561
+ if before != after :
562
+ disagreements = before ^ after
563
+ uncertain_states = [
564
+ disagreements & 1 != 0 ,
565
+ disagreements & 2 != 0 ,
566
+ disagreements & 4 != 0 ,
567
+ disagreements & 8 != 0 ,
568
+ ]
569
+ timestamps = self .fetch_data ()
570
+
571
+ if len (timestamps ) == 1 :
572
+ # One channel mode does not record states after start.
573
+ return initial_states
574
+
575
+ channels = ["LA1" , "LA2" , "LA3" , "LA4" ]
576
+ for i , state in enumerate (uncertain_states [: len (timestamps )]):
577
+ if state :
578
+ if timestamps [i ][0 ] > 0.1 : # µs
579
+ # States captured immediately after start are usually
580
+ # better than immediately before, except when an event
581
+ # was captures within ~100 ns of start.
582
+ initial_states [channels [i ]] = not initial_states [channels [i ]]
583
+
584
+ return initial_states
554
585
555
586
def get_xy (
556
587
self , timestamps : List [np .ndarray ], initial_states : Dict [str , bool ] = None
@@ -593,7 +624,7 @@ def get_xy(
593
624
594
625
return xy
595
626
596
- def _get_initial_states_and_progress (self ) -> Tuple [Dict [ str , bool ], List [ int ] ]:
627
+ def _get_initial_states_and_progress (self ) -> Tuple [int , int , int ]:
597
628
self ._device .send_byte (CP .TIMING )
598
629
self ._device .send_byte (CP .GET_INITIAL_DIGITAL_STATES )
599
630
initial = self ._device .get_int ()
@@ -602,14 +633,8 @@ def _get_initial_states_and_progress(self) -> Tuple[Dict[str, bool], List[int]]:
602
633
progress [1 ] = (self ._device .get_int () - initial ) // 2 - CP .MAX_SAMPLES // 4
603
634
progress [2 ] = (self ._device .get_int () - initial ) // 2 - 2 * CP .MAX_SAMPLES // 4
604
635
progress [3 ] = (self ._device .get_int () - initial ) // 2 - 3 * CP .MAX_SAMPLES // 4
605
- s = self ._device .get_byte ()
606
- initial_states = {
607
- "LA1" : (s & 1 != 0 ),
608
- "LA2" : (s & 2 != 0 ),
609
- "LA3" : (s & 4 != 0 ),
610
- "LA4" : (s & 8 != 0 ),
611
- }
612
- self ._device .get_byte () # INITIAL_DIGITAL_STATES_ERR
636
+ states_immediately_before_start = self ._device .get_byte ()
637
+ states_immediately_after_start = self ._device .get_byte ()
613
638
self ._device .get_ack ()
614
639
615
640
for e , i in enumerate (progress ):
@@ -618,7 +643,7 @@ def _get_initial_states_and_progress(self) -> Tuple[Dict[str, bool], List[int]]:
618
643
elif i < 0 :
619
644
progress [e ] = 0
620
645
621
- return initial_states , progress
646
+ return progress , states_immediately_before_start , states_immediately_after_start
622
647
623
648
def configure_trigger (self , trigger_channel : str , trigger_mode : str ):
624
649
"""Set up trigger channel and trigger condition.
0 commit comments