%testUSB245.m
%generate latency distribution for 10,000 consective FT_GetBitMode() read requests
%   from the FTDI UM245R USB interface using USB245.mex32   
%
%USB245.mex32 access via MATLAB:
%  usbObj = USB245();
%     Create persistent USB245 interface object and return handle to same.
%     Any 'old' copy of the USB245 should be purged if necessary (e.g.,
%     clear USB245).
%     returned: 
%         0 = Unsuccessful initiation. Either the driver is not functioning
%             properly; or, the USB device is not currently plugged-in.
%
%         NZ = Successful initialization; 64-bit handle to USB245 object.
%
%  status = USB245( usbObj );
%     Loads FT2DXX.DLL library functions and initializes UM245R device.
%     Configures lines D0-D3=inputs, lines D4-D7=outputs.
%     Returns 0 if successful.
%
%  data16 = USB245( usbObj, 0, 0 )
%     Reads current state of UM245R bidirectional I/O bus (D0-D7 lines).
%
%  status = USB245( usbObj, 1, data );
%     Writes contents of 'data' to UM245R bidirectional I/O bus.
%
%  Remove USB245 object when finished via 'clear USB245' or 'clear all'
%(fjs 8/24/2011)

clear all;
clc;

%Number of iterations
Ntrials = 10000;
%create USB245 interface
clear USB245;
USBhandle = USB245();
%load API library and initialize UM245R device
status = USB245( USBhandle );
if (status ~= 0 )
    clear USB245;
    display('Abort. Unsuccessful initialization of FTDI UM245R device');
    return;
end

%main test loop
%status = USB245(USBhandle, 1, 128); %turn-on LED
disp(['Collecting system latencies for ',num2str(Ntrials),' consecutive USB read data requests'])
display('Please stand-by.....');
latency=zeros(Ntrials,1);
drawnow;  %drain Windows GUI event queue
tic;
clock=toc;      %preload MATLAB performance time
data=USB245(USBhandle, 0, 0);
for i=1:Ntrials
    tic;
    data=USB245(USBhandle, 0, 0);
    clock=toc;
    latency(i) = clock * 1000; %save elapsed time (in msecs)
end
drawnow;   %drain Windows GUI event queue
%status = USB245(USBhandle, 1, 0); %turn-off LED

%report summary statistics to MATLAB console
mean_latency = mean(latency);
max_latency = max(latency);
min_latency = min(latency);
std_latency = std(latency);
disp('BENCHMARK RESULTS FOR FTDI USB I/O TEST     (N=10,000)');
disp(['Mean latency = ',num2str(mean_latency),' msec'])
disp(['Std latency = ',num2str(std_latency),' msec'])
disp(['Max observed = ',num2str(max_latency),' msec']);
disp(['Min observed = ',num2str(min_latency),' msec']);

%display distribution of latency data (histogram)
xmax = 10;
if max_latency > 10
    xmax = max_latency;
end
x=0:xmax;
figure(1), clf;
hist(latency,x);
hold on;
xlabel('Read USB Latency (msec)');
ylabel('Frequency');
title(['USB Read Time Distribution (N=',num2str(Ntrials),')']);
hold off;

%display latency time series to reveal possible bottlenecks
clear x;
x=[1:Ntrials];
figure(2), clf;
plot(x,latency);
hold on;
ymax = ceil(max(latency));
axis([0 Ntrials 0 ymax]);
xlabel('Sample Number');
ylabel('Latency (msec)');
title('USB Read Time Series Pictogram');
hold off;

%bring figure 1 to the front
figure(1)
