def get_values(self, device, active_channels, num_samples, segment_index=0): # Initialise buffers to hold the data: results = { channel: numpy.empty(num_samples, numpy.dtype('int16')) for channel in active_channels } overflow = c_int16(0) if len(self._get_values.argtypes ) == 7 and self._get_timebase.argtypes[1] == c_int16: inputs = {k: None for k in 'ABCD'} for k, arr in results.items(): inputs[k] = arr.ctypes.data return_code = self._get_values(c_int16(device.handle), inputs['A'], inputs['B'], inputs['C'], inputs['D'], byref(overflow), c_int32(num_samples)) if return_code == 0: raise InvalidCaptureParameters() elif len(self._get_values.argtypes ) == 7 and self._get_timebase.argtypes[1] == c_uint32: # For this function pattern, we first call a function (self._set_data_buffer) to register each buffer. Then, # we can call self._get_values to actually populate them. for channel, array in results.items(): status = self._set_data_buffer( c_int16(device.handle), c_int32(self.PICO_CHANNEL[channel]), array.ctypes.data, c_int32(num_samples), c_uint32(segment_index), c_int32(self.PICO_RATIO_MODE['NONE'])) if status != self.PICO_STATUS['PICO_OK']: raise InvalidCaptureParameters( "set_data_buffer failed (%s)" % constants.pico_tag(status)) samples_collected = c_uint32(num_samples) status = self._get_values(c_int16(device.handle), c_uint32(0), byref(samples_collected), c_uint32(1), c_int32(self.PICO_RATIO_MODE['NONE']), c_uint32(segment_index), byref(overflow)) if status != self.PICO_STATUS['PICO_OK']: raise InvalidCaptureParameters("get_values failed (%s)" % constants.pico_tag(status)) overflow_warning = {} if overflow.value: for channel in results.keys(): if overflow.value & (1 >> self.PICO_CHANNEL[channel]): overflow_warning[channel] = True return results, overflow_warning
def _python_run_block(self, handle, pre_samples, post_samples, timebase_id, oversample, segment_index): time_indisposed = c_int32(0) if len(self._run_block.argtypes) == 5: return_code = self._run_block(c_int16(handle), c_int32(pre_samples + post_samples), c_int16(timebase_id), c_int16(oversample), byref(time_indisposed)) if return_code == 0: raise InvalidCaptureParameters() elif len(self._run_block.argtypes) == 9: status = self._run_block(c_int16(handle), c_int32(pre_samples), c_int32(post_samples), c_uint32(timebase_id), c_int16(oversample), byref(time_indisposed), c_uint32(segment_index), None, None) if status != self.PICO_STATUS['PICO_OK']: raise InvalidCaptureParameters("run_block failed (%s)" % constants.pico_tag(status)) else: raise NotImplementedError("not done other driver types yet") return float(time_indisposed.value) * 0.001
def _python_get_timebase(self, handle, timebase_id, no_of_samples, oversample, segment_index): # We use get_timebase on ps2000 and ps3000 and parse the nanoseconds-int into a float. # on other drivers, we use get_timebase2, which gives us a float in the first place. if len(self._get_timebase.argtypes) == 7 and self._get_timebase.argtypes[1] == c_int16: time_interval = c_int32(0) time_units = c_int16(0) max_samples = c_int32(0) return_code = self._get_timebase(c_int16(handle), c_int16(timebase_id), c_int32(no_of_samples), byref(time_interval), byref(time_units), c_int16(oversample), byref(max_samples)) if return_code == 0: raise InvalidTimebaseError() return TimebaseInfo(timebase_id, float(time_interval.value), time_units.value, max_samples.value, None) elif hasattr(self, '_get_timebase2') and ( len(self._get_timebase2.argtypes) == 7 and self._get_timebase2.argtypes[1] == c_uint32): time_interval = c_float(0.0) max_samples = c_int32(0) status = self._get_timebase2(c_int16(handle), c_uint32(timebase_id), c_int32(no_of_samples), byref(time_interval), c_int16(oversample), byref(max_samples), c_uint32(segment_index)) if status != self.PICO_STATUS['PICO_OK']: raise InvalidTimebaseError("get_timebase2 failed (%s)" % constants.pico_tag(status)) return TimebaseInfo(timebase_id, time_interval.value, None, max_samples.value, segment_index) else: raise NotImplementedError("not done other driver types yet")
def set_null_trigger(self, device): auto_trigger_after_millis = 1 if hasattr(self, '_set_trigger') and len( self._set_trigger.argtypes) == 6: PS2000_NONE = 5 return_code = self._set_trigger(c_int16(device.handle), c_int16(PS2000_NONE), c_int16(0), c_int16(0), c_int16(0), c_int16(auto_trigger_after_millis)) if return_code == 0: raise InvalidTriggerParameters() elif hasattr(self, '_set_simple_trigger') and len( self._set_simple_trigger.argtypes) == 7: enabled = False status = self._set_simple_trigger( c_int16(device.handle), c_int16(int(enabled)), c_int32(self.PICO_CHANNEL['A']), c_int16(0), c_int32(self.PICO_THRESHOLD_DIRECTION['NONE']), c_uint32(0), c_int16(auto_trigger_after_millis)) if status != self.PICO_STATUS['PICO_OK']: raise InvalidTriggerParameters( "set_simple_trigger failed (%s)" % constants.pico_tag(status)) else: raise NotImplementedError("not done other driver types yet")
def memory_segments(self, device, number_segments): if not hasattr(self, '_memory_segments'): raise DeviceCannotSegmentMemoryError() max_samples = c_int32(0) status = self._memory_segments(c_int16(device.handle), c_uint32(number_segments), byref(max_samples)) if status != self.PICO_STATUS['PICO_OK']: raise InvalidMemorySegmentsError("could not segment the device memory into (%s) segments (%s)" % ( number_segments, constants.pico_tag(status))) return max_samples
def stop(self, device): if self._stop.restype == c_int16: return_code = self._stop(c_int16(device.handle)) if isinstance(return_code, c_int16): if return_code == 0: raise InvalidCaptureParameters() else: status = self._stop(c_int16(device.handle)) if status != self.PICO_STATUS['PICO_OK']: raise InvalidCaptureParameters("stop failed (%s)" % constants.pico_tag(status))
def _python_set_channel(self, handle, channel_id, enabled, coupling_id, range_id, analog_offset): if len(self._set_channel.argtypes ) == 5 and self._set_channel.argtypes[1] == c_int16: if analog_offset is not None: raise ArgumentOutOfRangeError( "This device doesn't support analog offset") return_code = self._set_channel(c_int16(handle), c_int16(channel_id), c_int16(enabled), c_int16(coupling_id), c_int16(range_id)) if return_code == 0: raise ValidRangeEnumValueNotValidForThisDevice( "%sV is out of range for this device." % (self.PICO_VOLTAGE_RANGE[range_id])) elif len(self._set_channel.argtypes ) == 5 and self._set_channel.argtypes[1] == c_int32 or (len( self._set_channel.argtypes) == 6): status = self.PICO_STATUS['PICO_OK'] if len(self._set_channel.argtypes) == 6: if analog_offset is None: analog_offset = 0.0 status = self._set_channel(c_int16(handle), c_int32(channel_id), c_int16(enabled), c_int32(coupling_id), c_int32(range_id), c_float(analog_offset)) elif len(self._set_channel.argtypes ) == 5 and self._set_channel.argtypes[1] == c_int32: if analog_offset is not None: raise ArgumentOutOfRangeError( "This device doesn't support analog offset") status = self._set_channel(c_int16(handle), c_int32(channel_id), c_int16(enabled), c_int16(coupling_id), c_int32(range_id)) if status != self.PICO_STATUS['PICO_OK']: if status == self.PICO_STATUS['PICO_INVALID_VOLTAGE_RANGE']: raise ValidRangeEnumValueNotValidForThisDevice( "%sV is out of range for this device." % (self.PICO_VOLTAGE_RANGE[range_id])) if status == self.PICO_STATUS[ 'PICO_INVALID_CHANNEL'] and not enabled: # don't throw errors if the user tried to disable a missing channel. return raise ArgumentOutOfRangeError( "problem configuring channel (%s)" % constants.pico_tag(status)) else: raise NotImplementedError("not done other driver types yet")
def is_ready(self, device): """poll this function to find out when block mode is ready or has triggered. returns: True if data is ready, False otherwise.""" if hasattr(self, '_ready') and len(self._ready.argtypes) == 1: return_code = self._ready(c_int16(device.handle)) return bool(return_code) elif hasattr(self, '_is_ready') and len(self._is_ready.argtypes) == 2: is_ready = c_int16(0) status = self._is_ready(c_int16(device.handle), byref(is_ready)) if status != self.PICO_STATUS['PICO_OK']: raise InvalidCaptureParameters("is_ready failed (%s)" % constants.pico_tag(status)) return bool(is_ready.value) else: raise NotImplementedError("not done other driver types yet")
def _python_open_unit(self, serial=None, resolution=None): if serial is None: handle, status = self._python_open_any_unit(resolution) else: handle, status = self._python_open_specific_unit(serial, resolution) if handle < 1: message = ("Driver %s could find no device" % self.name) + ("s" if serial is None else (" matching %s" % serial)) if status is not None: message += " (%s)" % constants.pico_tag(status) raise DeviceNotFoundError(message) return handle