%% Acquiring Data Using a Measurement Computing USB-1608FS Series Device
%
% This example shows how to acquire data using the MCC 
% Universal Library for .NET commands.
%
% NOTE: this example is provided AS IS.  It has not been tested or
%       validated as a product, for use in a deployed application or
%       system, or for use in hazardous environments.  You assume
%       all risks for use of the code or example.
%
% Copyright 2019 Measurement Computing.

clear all;
clc;

%% Load the UL .NET Library 
%'C:\Program Files (x86)\MeasurementComputing\DAQ\MCCDAQ.dll'
asm = NET.addAssembly('MccDaq');

%X-reference DaqDevDiscovery01.cs example
MccDaq.DaqDeviceManager.IgnoreInstaCal();    %DAQ Device Discovery

%Get a list of all boards discovered (includes USB, Ethernet, Bluetooth)
inventory = MccDaq.DaqDeviceManager.GetDaqDeviceInventory(MccDaq.DaqDeviceInterface.Any);

%create the object for the data board
daq = MccDaq.DaqDeviceManager.CreateDaqDevice(0, inventory(1));   

daq.FlashLED();
%FlashLED(daq);     %either call works

disp([char(daq.BoardName), ' With Unique ID of ',char(daq.Descriptor.UniqueID)]);

NumElements = 1000;

%declare variable for output array
ADData = NET.createArray('System.Double' , NumElements);
for x = 1 : NumElements
    ADData(x) = 0;
end

%netarray = NET.createArray('System.Int16', dataLength)  %note
memhandle3 = MccDaq.MccService.ScaledWinBufAllocEx(NumElements);

if(memhandle3 == System.IntPtr.Zero)
    stop;
end

myrange = MccDaq.Range;
rate = 500;
myscanoptions = MccDaq.ScanOptions.ScaleData;

[ev,myrate] = daq.AInScan(0,0,NumElements,rate,myrange,memhandle3,myscanoptions);

MccDaq.MccService.ScaledWinBufToArray(memhandle3, ADData, 1, 100);

for x = 1 : 5
    ADData(x)     
end

MccDaq.MccService.WinBufFreeEx(memhandle3);


%% Release resources associated with the specified board object
MccDaq.DaqDeviceManager.ReleaseDaqDevice(daq);
