Skip to content

Commit 685e717

Browse files
committed
Reduce complexity in capture
1 parent a49938a commit 685e717

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

PSL/logic_analyzer.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
class LogicAnalyzer:
2121
def __init__(self, device: packet_handler.Handler = None):
22-
self.H = device
2322
self._device = device
2423
self._channels = {
2524
d: digital_channel.DigitalInput(d) for d in digital_channel.DIGITAL_INPUTS
@@ -167,13 +166,13 @@ def capture(
167166
channels: int,
168167
events: int = CP.MAX_SAMPLES // 4 - 1,
169168
timeout: float = None,
170-
e2e_time: float = 0,
171169
modes: List[str] = 4 * ("any",),
170+
e2e_time: float = None,
172171
block: bool = True,
173172
):
173+
self._check_arguments(channels, events)
174174
events += 1 # Capture an extra event in case we get a spurious zero.
175175
self.clear_buffer(0, CP.MAX_SAMPLES)
176-
self._invalidate_buffer()
177176
self._configure_trigger(channels)
178177
modes = [digital_channel.MODES[m] for m in modes]
179178
old_progress = self.get_progress()
@@ -191,17 +190,12 @@ def capture(
191190
self._capture_one()
192191
elif channels == 2:
193192
self._capture_two()
194-
elif channels == 4:
193+
else:
195194
self._capture_four(e2e_time)
196195

197196
if block:
198-
start = time.time()
199-
self._wait_for_progress(old_progress, timeout=timeout)
200-
201-
while self.get_progress() < events:
202-
if timeout is not None:
203-
if time.time() - start >= timeout:
204-
raise RuntimeError("Capture timed out.")
197+
self._wait_for_progress(old_progress, timeout)
198+
self._timeout(events, timeout)
205199
else:
206200
return
207201

@@ -212,6 +206,21 @@ def capture(
212206

213207
return timestamps[:channels] # Discard 4:th channel if user asked for 3.
214208

209+
@staticmethod
210+
def _check_arguments(channels: int, events: int):
211+
max_events = CP.MAX_SAMPLES // 4 - 1
212+
if events > max_events:
213+
raise ValueError(f"Events must be fewer than {max_events}.")
214+
elif channels < 0 or channels > 4:
215+
raise ValueError("Channels must be between 1-4.")
216+
217+
def _timeout(self, events: int, timeout: float):
218+
start = time.time()
219+
while self.get_progress() < events:
220+
if timeout is not None:
221+
if time.time() - start >= timeout:
222+
raise RuntimeError("Capture timed out.")
223+
215224
def _capture_one(self):
216225
self._channels[self._channel_one_map].prescaler = 0
217226
self._device.send_byte(CP.TIMING)
@@ -246,6 +255,8 @@ def _capture_two(self):
246255

247256
def _capture_four(self, e2e_time: float):
248257
rollover_time = (2 ** 16 - 1) / CLOCK_RATE
258+
e2e_time = 0 if e2e_time is None else e2e_time
259+
249260
if e2e_time > rollover_time * PRESCALERS[3]:
250261
raise ValueError("Timegap too big for four channel mode.")
251262
elif e2e_time > rollover_time * PRESCALERS[2]:
@@ -555,6 +566,7 @@ def clear_buffer(self, starting_position, total_points):
555566
self.H.__sendInt__(starting_position)
556567
self.H.__sendInt__(total_points)
557568
self.H.__get_ack__()
569+
self._invalidate_buffer()
558570

559571
def _invalidate_buffer(self):
560572
for c in self._channels.values():

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