-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathps3000.py
329 lines (262 loc) · 11.9 KB
/
ps3000.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"""
This is the low level driver file for a specific Picoscope.
By this, I mean if parameters want to get passed as strings, they should be
handled by PSBase
All functions here should take things as close to integers as possible, the
only exception here is for array parameters. Array parameters should be passed
in a pythonic way through numpy since the PSBase class should not be aware of
the specifics behind how the clib is called.
The functions should not have any default values as these should be handled
by PSBase.
"""
# pico-python is Copyright (c) 2013-2014 By:
# Colin O'Flynn <coflynn@newae.com>
# Mark Harfouche <mark.harfouche@gmail.com>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from __future__ import division
from __future__ import absolute_import
from __future__ import print_function
from __future__ import unicode_literals
# import math
import inspect
# to load the proper dll
import platform
# Do not import or use ill definied data types
# such as short int or long
# use the values specified in the h file
# float is always defined as 32 bits
# double is defined as 64 bits
from ctypes import byref, POINTER, create_string_buffer, c_float, \
c_int16, c_int32, c_uint32, c_void_p
from ctypes import c_int32 as c_enum
from picoscope.picobase import _PicoscopeBase
class PS3000(_PicoscopeBase):
"""The following are low-level functions for the PS3000."""
LIBNAME = "ps3000"
NUM_CHANNELS = 2
CHANNELS = {"A": 0, "B": 1, "MaxChannels": 2}
THRESHOLD_TYPE = {"Rising": 0,
"Falling": 1}
CHANNEL_RANGE = [
{"rangeV": 20E-3, "apivalue": 1, "rangeStr": "20 mV"},
{"rangeV": 50E-3, "apivalue": 2, "rangeStr": "50 mV"},
{"rangeV": 100E-3, "apivalue": 3, "rangeStr": "100 mV"},
{"rangeV": 200E-3, "apivalue": 4, "rangeStr": "200 mV"},
{"rangeV": 500E-3, "apivalue": 5, "rangeStr": "500 mV"},
{"rangeV": 1.0, "apivalue": 6, "rangeStr": "1 V"},
{"rangeV": 2.0, "apivalue": 7, "rangeStr": "2 V"},
{"rangeV": 5.0, "apivalue": 8, "rangeStr": "5 V"},
{"rangeV": 10.0, "apivalue": 9, "rangeStr": "10 V"},
{"rangeV": 20.0, "apivalue": 10, "rangeStr": "20 V"},
]
CHANNEL_COUPLINGS = {"DC": 1, "AC": 0}
# has_sig_gen = True
WAVE_TYPES = {"Sine": 0, "Square": 1, "Triangle": 2,
"RampUp": 3, "RampDown": 4, "DCVoltage": 5}
SWEEP_TYPES = {"Up": 0, "Down": 1, "UpDown": 2, "DownUp": 3}
TIME_UNITS = {"FS": 0, "PS": 1, "NS": 2, "US": 3, "MS": 4, "S": 5}
MAX_VALUE = 32767
MIN_VALUE = -32767
MAX_TIMEBASES = 19
UNIT_INFO_TYPES = {"DriverVersion": 0x0,
"USBVersion": 0x1,
"HardwareVersion": 0x2,
"VariantInfo": 0x3,
"BatchAndSerial": 0x4,
"CalDate": 0x5,
"ErrorCode": 0x6,
"KernelVersion": 0x7}
channelBuffersPtr = [c_void_p(), c_void_p()]
channelBuffersLen = [0, 0]
# SIGGEN_TRIGGER_TYPES = {"Rising": 0, "Falling": 1,
# "GateHigh": 2, "GateLow": 3}
# SIGGEN_TRIGGER_SOURCES = {"None": 0, "ScopeTrig": 1, "AuxIn": 2,
# "ExtIn": 3, "SoftTrig": 4, "TriggerRaw": 5}
def __init__(self, serialNumber=None, connect=True):
"""Load DLL etc."""
if platform.system() == 'Linux':
from ctypes import cdll
self.lib = cdll.LoadLibrary("lib" + self.LIBNAME + ".so")
elif platform.system() == 'Darwin':
from picoscope.darwin_utils import LoadLibraryDarwin
self.lib = LoadLibraryDarwin("lib" + self.LIBNAME + ".dylib")
else:
from ctypes import windll
from ctypes.util import find_library
self.lib = windll.LoadLibrary(
find_library(str(self.LIBNAME + ".dll"))
)
super(PS3000, self).__init__(serialNumber, connect)
def _lowLevelOpenUnit(self, sn):
if sn is not None:
raise ValueError("PS3000 Doesn't Support Open by Serial Number")
m = self.lib.ps3000_open_unit()
if m < 0:
raise IOError("Failed to Find PS3000 Unit. " +
"Should you be using PS3000a driver?")
self.handle = m
self.suggested_time_units = self.TIME_UNITS["NS"]
def _lowLevelCloseUnit(self):
m = self.lib.ps3000_close_unit(c_int16(self.handle))
self.checkResult(m)
def _lowLevelSetChannel(self, chNum, enabled, coupling, VRange, VOffset,
BWLimited):
m = self.lib.ps3000_set_channel(
c_int16(self.handle), c_enum(chNum),
c_int16(enabled), c_enum(coupling),
c_enum(VRange))
self.checkResult(m)
def _lowLevelStop(self):
m = self.lib.ps3000_stop(c_int16(self.handle))
self.checkResult(m)
def _lowLevelGetUnitInfo(self, info):
s = create_string_buffer(256)
m = self.lib.ps3000_get_unit_info(
c_int16(self.handle), byref(s), c_int16(len(s)), c_enum(info))
self.checkResult(m)
# should this bee ascii instead?
# I think they are equivalent...
return s.value.decode('utf-8')
def _lowLevelFlashLed(self, times):
m = self.lib.ps3000_flash_led(c_int16(self.handle))
self.checkResult(m)
def _lowLevelSetSimpleTrigger(self, enabled, trigsrc, threshold_adc,
direction, delay, timeout_ms):
# TODO: Fix 'auto' which is where trigger occurs in block.
# Delay is not used
m = self.lib.ps3000_set_trigger(
c_int16(self.handle), c_enum(trigsrc), c_int16(threshold_adc),
c_enum(direction), c_int16(delay), c_int16(timeout_ms))
self.checkResult(m)
def _lowLevelRunBlock(self, numPreTrigSamples, numPostTrigSamples,
timebase, oversample, segmentIndex, callback,
pParameter):
# NOT: Oversample is NOT used!
# Note: callback and pParameter are not available.
# TODO: Fix 'delay' which is where trigger occurs in block
if numPreTrigSamples > 0:
raise ValueError("numPreTrigSamples isn't supported on PS3000")
timeIndisposedMs = c_int32()
m = self.lib.ps3000_run_block(
c_int16(self.handle), c_uint32(numPostTrigSamples),
c_uint32(timebase), c_int16(1), byref(timeIndisposedMs))
self.checkResult(m)
return timeIndisposedMs.value
def _lowLevelIsReady(self):
ready = c_int16()
ready = self.lib.ps3000_ready(c_int16(self.handle))
if ready > 0:
return True
elif ready == 0:
return False
else:
raise IOError("ps3000_ready returned %d" % ready.value)
def _lowLevelGetTimebase(self, tb, noSamples, oversample, segmentIndex):
"""Return (timeIntervalSeconds, maxSamples)."""
maxSamples = c_int32()
time_interval = c_int32()
time_units = c_int16()
m = self.lib.ps3000_get_timebase(
c_int16(self.handle), c_int16(tb), c_uint32(noSamples),
byref(time_interval), byref(time_units), c_int16(1),
byref(maxSamples))
self.checkResult(m)
self.suggested_time_units = time_units.value
return (time_interval.value / 1.0E9, maxSamples.value)
def getTimeBaseNum(self, sampleTimeS):
"""ps3000 doesn't seem to have published formula like other scopes."""
time_interval = c_int32()
timebases = [None] * self.MAX_TIMEBASES
# Convert to nS
sampleTimenS = sampleTimeS * 1E9
tb = 0
while tb < self.MAX_TIMEBASES:
rv = self.lib.ps3000_get_timebase(
c_int16(self.handle), c_int16(tb), c_uint32(512),
byref(time_interval), c_void_p(), c_int16(1), c_void_p())
if rv != 0:
timebases[tb] = time_interval.value
tb += 1
# Figure out closest option
besterror = 1E99
bestindx = 0
for indx, val in enumerate(timebases):
if val is not None:
error = sampleTimenS - val
if abs(error) < besterror:
besterror = abs(error)
bestindx = indx
return bestindx
def getTimestepFromTimebase(self, timebase):
"""Return timestep from Timebase."""
time_interval = c_int32()
m = self.lib.ps3000_get_timebase(
c_int16(self.handle), c_int16(timebase), c_uint32(512),
byref(time_interval), c_void_p(), c_int16(1), c_void_p())
self.checkResult(m)
return (time_interval.value / 1.0E9)
def _lowLevelSetDataBuffer(self, channel, data, downSampleMode,
segmentIndex):
dataPtr = data.ctypes.data_as(POINTER(c_int16))
numSamples = len(data)
self.channelBuffersPtr[channel] = dataPtr
self.channelBuffersLen[channel] = numSamples
def _lowLevelClearDataBuffer(self, channel, segmentIndex):
self.channelBuffersPtr[channel] = c_void_p()
self.channelBuffersLen[channel] = 0
def _lowLevelGetValues(self, numSamples, startIndex, downSampleRatio,
downSampleMode, segmentIndex):
# TODO:
# Check overflow in channelBuffersLen against numSamples,
# but need to not raise error if channelBuffersPtr is void
overflow = c_int16()
rv = self.lib.ps3000_get_values(
c_int16(self.handle),
self.channelBuffersPtr[0],
self.channelBuffersPtr[1],
c_void_p(), c_void_p(),
byref(overflow), c_int32(numSamples))
self.checkResult(rv)
return (rv, overflow.value)
def _lowLevelSetSigGenBuiltInSimple(self, offsetVoltage, pkToPk, waveType,
frequency, shots, triggerType,
triggerSource, stopFreq, increment,
dwellTime, sweepType, numSweeps):
if stopFreq is None:
stopFreq = frequency
m = self.lib.ps3000_set_sig_gen_built_in(
c_int16(self.handle),
c_int32(int(offsetVoltage * 1000000)),
c_int32(int(pkToPk * 1000000)),
c_int16(waveType),
c_float(frequency), c_float(stopFreq),
c_float(increment), c_float(dwellTime), c_enum(sweepType),
c_uint32(numSweeps))
self.checkResult(m)
def checkResult(self, ec):
"""Check result of function calls, raise exception if not 0."""
# PS3000 differs from other drivers in that non-zero is good
if ec == 0:
raise IOError('Error calling %s' % (inspect.stack()[1][3]))
return 0