Skip to content

Commit 07dd0fe

Browse files
Updated get_time_axis() in base.py (#155)
* Updated get_time_axis() in base.py - Removed int() cast to stop incorrect offsets * Updated get_time_axis() in base.py - Removed int() cast to stop incorrect offsets * Added base dataclass storage for PicoScopeBase (base.py) * Updated get_time_axis and run_block_capture * Updated get_time_axis() in base.py - Removed int() cast to stop incorrect offsets * Added base dataclass storage for PicoScopeBase (base.py) * Updated get_time_axis and run_block_capture
1 parent 8c73dbe commit 07dd0fe

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

pypicosdk/_classes/_general.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"This file contains general classes for pyPicoSDK"
2+
3+
from dataclasses import dataclass
4+
5+
6+
@dataclass
7+
class BaseDataClass:
8+
"Class containing data for PicoScopeBase"
9+
last_pre_trig: float = 50

pypicosdk/base.py

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from .common import (
3030
_get_literal,
3131
)
32+
from ._classes import _general
3233

3334

3435
class PicoScopeBase:
@@ -66,6 +67,8 @@ def __init__(self, dll_name, *args, **kwargs):
6667
self._actual_interval = 0
6768
self.last_used_volt_unit: str = 'mv'
6869

70+
self.base_dataclass = _general.BaseDataClass()
71+
6972
def __exit__(self):
7073
self.close_unit()
7174

@@ -487,7 +490,8 @@ def get_time_axis(
487490
Args:
488491
timebase (int): PicoScope timebase
489492
samples (int): Number of samples captured
490-
pre_trig_percent (int): Percent to offset the 0 point by. If None, default is 0.
493+
pre_trig_percent (int): Percent to offset the 0 point by. If None, defaults to last
494+
used pre_trig_percent or 50.
491495
unit (str): Unit of seconds the time axis is returned in.
492496
Default is 'ns' (nanoseconds).
493497
ratio (int): If using a downsampling ratio, this will scale the time interval
@@ -496,15 +500,24 @@ def get_time_axis(
496500
Returns:
497501
np.ndarray: Array of time values in nano-seconds
498502
"""
503+
# Check and save pre_trig to base dataclass
504+
if pre_trig_percent == None:
505+
pre_trig_percent = self.base_dataclass.last_pre_trig
506+
self.base_dataclass.last_pre_trig = pre_trig_percent
507+
508+
# Default to 1 when using ratio
499509
ratio = max(1, ratio)
510+
511+
# Get unit scalar value
500512
scalar = cst.TimeUnitStd_M['ns'] / cst.TimeUnitStd_M[unit]
513+
514+
# Get the interval for the specified timebase/samples
501515
interval = self.get_timebase(timebase, samples)['Interval(ns)'] * ratio / scalar
516+
517+
# Maths
502518
time_axis = np.arange(samples) * interval
503-
if pre_trig_percent is None:
504-
return time_axis
505-
else:
506-
offset = int(time_axis.max() * (pre_trig_percent / 100))
507-
return time_axis - offset
519+
offset = time_axis.max() * (pre_trig_percent / 100)
520+
return time_axis - offset
508521

509522
def realign_downsampled_data(
510523
self,
@@ -1885,22 +1898,34 @@ def run_simple_rapid_block_capture(
18851898
# Return data
18861899
return channel_buffer, time_axis
18871900

1888-
def run_block_capture(self, timebase, samples, pre_trig_percent=50, segment=0) -> int:
1901+
def run_block_capture(
1902+
self,
1903+
timebase: int,
1904+
samples: int,
1905+
pre_trig_percent: float | None = None,
1906+
segment: int = 0,
1907+
) -> int:
18891908
"""
18901909
Runs a block capture using the specified timebase and number of samples.
18911910
18921911
This sets up the PicoScope to begin collecting a block of data, divided into
18931912
pre-trigger and post-trigger samples. It uses the PicoSDK `RunBlock` function.
18941913
18951914
Args:
1896-
timebase (int): Timebase value determining sample interval (refer to PicoSDK guide).
1897-
samples (int): Total number of samples to capture.
1898-
pre_trig_percent (int, optional): Percentage of samples to capture before the trigger.
1899-
segment (int, optional): Memory segment index to use.
1915+
timebase (int): Timebase value determining sample interval (refer to PicoSDK guide).
1916+
samples (int): Total number of samples to capture.
1917+
pre_trig_percent (int | None, optional):
1918+
Percentage of samples to capture before the trigger. If None, defaults to
1919+
last called pre_trig_percent or 50.
1920+
segment (int, optional): Memory segment index to use.
19001921
19011922
Returns:
1902-
int: Estimated time (in milliseconds) the device will be busy capturing data.
1923+
int: Estimated time (in milliseconds) the device will be busy capturing data.
19031924
"""
1925+
# Check and add pre-trig to base dataclass
1926+
if pre_trig_percent is None:
1927+
pre_trig_percent = self.base_dataclass.last_pre_trig
1928+
self.base_dataclass.last_pre_trig = pre_trig_percent
19041929

19051930
pre_samples = int((samples * pre_trig_percent) / 100)
19061931
post_samples = int(samples - pre_samples)

0 commit comments

Comments
 (0)