%% Fetch Waveform through NI-SCOPE MATLAB Instrument Driver in Simulation Mode
%
% This example shows how to acquire digital waveform from two channels of a
% National Instruments(R) NI-SCOPE driver in the simulation mode.


%% Introduction
% Instrument Control Toolbox(TM) supports communication with instruments
% through high-level drivers.  In this example you can acquire digital
% waveforms from a National Instruments(R) NI-SCOPE driver in the
% simulation mode.
%
% Copyright 2010-2020 The MathWorks, Inc.


%% Requirements
% This example requires a Microsoft(R) Windows(R) system and NI-SCOPE
% package 3.6 or higher. Make sure the Measurement & Automation Explorer
% recognizes the NI-SCOPE driver before you use this example.


%% Verify NI-SCOPE Installation
% Use the |instrhwinfo| command to check if the NI-SCOPE software package
% is installed correctly. If installed correctly, NI-SCOPE is listed as 
% one of the modules installed on the Windows machine. This example uses 
% libraries installed with it.

driversInfo = instrhwinfo ('ivi');
disp(driversInfo.Modules');


%% Create a MATLAB Instrument object 
% Use the |icdevice| function to create an instrument object from the MDD
% you generated, and establish a connection to the scope using that object.
%
% |icdevice| function takes two or more input arguments. The MDD file name,
% the resource name for the scope and optionally, device-specific
% parameters that can be set.
%
% You can get the resource name for the scope from NI Measurement and
% Automation Explorer. For example: A resource name of |PXI1Slot6| in NI
% MAX would be |PXI1Slot6|. You can remove the |optionstring| argument
% and the corresponding string parameter if you have the actual hardware.
%
% You can establish a connection to the scope using the |connect| command.

ictObj = icdevice('niscope.mdd', 'PXI1Slot6', 'optionstring', 'simulate=true');
connect(ictObj);
disp(ictObj);

%% Configure the Scope
% For the purpose of this example, the scope is configured using auto setup
% which automatically sets the vertical range, sample rate, trigger level
% and a few other settings.
%
% Use the MATLAB Instrument Driver Editor (midedit) to view other
% properties and functions that allow you to configure a device. The tool
% shows all the properties and functions that the NI-SCOPE software package
% supports.

configuration = ictObj.Configuration;
invoke(configuration, 'autosetup');

%% Configure the vertical range for each channel
% ConfigureVertical function configures the most commonly configured
% attributes of the digitizer vertical subsystem, such as the range,
% offset, coupling, probe attenuation and the channel. We will be fetching
% data from channels '0' and '1' and therefore configuring these channels.
% Refer to the NI-Scope documentation for further information.

Range = 10;
Offset = 0;
Coupling = 1; 
ProbeAttenuation = 1;

% Configure Channel 0
invoke(ictObj.Configurationfunctionsvertical, 'configurevertical', '0', Range, Offset, Coupling, ProbeAttenuation, true);

% Configure Channel 1
invoke(ictObj.Configurationfunctionsvertical, 'configurevertical', '1', Range, Offset, Coupling, ProbeAttenuation, true);

%% Prepare the waveform information
% Waveform information is a structure containing the timing information,
% number of samples, gain, and offset scaling factors for acquiring a
% waveform from the scope. Memory will be preallocated for the waveform
% information structure for each channel.

numChannels = 2;
channelList = '0,1';
numSamples = 1024;

for i = 1:numChannels
    waveformInfo(i).absoluteInitialX = 0;
    waveformInfo(i).relativeInitialX = 0;
    waveformInfo(i).xIncrement = 0;
    waveformInfo(i).actualSamples = 0;
    waveformInfo(i).offset = 0;
    waveformInfo(i).gain = 0;
    waveformInfo(i).reserved1 = 0;
    waveformInfo(i).reserved2 = 0;
end

%% Fetch waveform
% Once you configure the scope with the required settings, use an
% appropriate function call to acquire the waveform from channels |0| and
% |1|. Preallocate waveformArray to store data from all requested channels.
% Prior to fetching data, a waveform acquisition has to be initiated. After
% fetching the data, extract the waveform corresponding to each channel.

waveformArray = zeros(numChannels * numSamples, 1);
TimeOut = 10; % seconds
invoke(ictObj.Acquisition, 'initiateacquisition');
[waveformArray, waveformInfo] = invoke(ictObj.Acquisition, 'fetch', channelList,...
    TimeOut, numSamples, waveformArray, waveformInfo);

waveformArray = reshape(waveformArray, numSamples, numChannels);

%% Plot the waveform
plot(waveformArray);

%% Clean up
% Delete the MATLAB Instrument Object.

disconnect(ictObj);
delete(ictObj);
clear ictObj;
