Skip to content

Commit c7b7ace

Browse files
committed
Fix initial states sometimes being wrong
1 parent 5bd5531 commit c7b7ace

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

pslab/instrument/logic_analyzer.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ def get_progress(self) -> int:
534534
a += 1
535535

536536
p = CP.MAX_SAMPLES // 4
537-
progress = self._get_initial_states_and_progress()[1]
537+
progress = self._get_initial_states_and_progress()[0]
538538
for a in active_channels:
539539
p = min(progress[a], p)
540540

@@ -550,7 +550,38 @@ def get_initial_states(self) -> Dict[str, bool]:
550550
state, e.g. {'LA1': True, 'LA2': True, 'LA3': True, 'LA4': False}.
551551
True means HIGH, False means LOW.
552552
"""
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
554585

555586
def get_xy(
556587
self, timestamps: List[np.ndarray], initial_states: Dict[str, bool] = None
@@ -593,7 +624,7 @@ def get_xy(
593624

594625
return xy
595626

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]:
597628
self._device.send_byte(CP.TIMING)
598629
self._device.send_byte(CP.GET_INITIAL_DIGITAL_STATES)
599630
initial = self._device.get_int()
@@ -602,14 +633,8 @@ def _get_initial_states_and_progress(self) -> Tuple[Dict[str, bool], List[int]]:
602633
progress[1] = (self._device.get_int() - initial) // 2 - CP.MAX_SAMPLES // 4
603634
progress[2] = (self._device.get_int() - initial) // 2 - 2 * CP.MAX_SAMPLES // 4
604635
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()
613638
self._device.get_ack()
614639

615640
for e, i in enumerate(progress):
@@ -618,7 +643,7 @@ def _get_initial_states_and_progress(self) -> Tuple[Dict[str, bool], List[int]]:
618643
elif i < 0:
619644
progress[e] = 0
620645

621-
return initial_states, progress
646+
return progress, states_immediately_before_start, states_immediately_after_start
622647

623648
def configure_trigger(self, trigger_channel: str, trigger_mode: str):
624649
"""Set up trigger channel and trigger condition.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy