function void = plotandwriteexamplelegacy(comPort, captureDuration, fileName)
%PLOTANDWRITEXAMPLE - Demonstrate legacy features of ShimmerHandleClass
%
% PLOTANDWRITEEXAMPLELEGACY(COMPORT, CAPTUREDURATION, FILENAME) plots the 3
% accelerometer signals from the Shimmer paired with COMPORT. The function
% will stream data for a fixed duration of time defined by the constant
% CAPTUREDURATION. The function also writes the data in a tab ddelimited
% format to the file defined in FILENAME.
% NOTE: This example uses the method 'getuncalibrateddata' which is a
% legacy method from the beta release. The user is advised to use the
% replacement method 'getdata' as an alternative (see plotandwriteexample.m).
%
% SYNOPSIS: plotandwriteexamplelegacy(comPort, captureDuration, fileName)
%
% INPUT: comPort - String value defining the COM port number for Shimmer
% INPUT: captureDuration - Numerical value defining the period of time
% (in seconds) for which the function will stream
% data from the Shimmers.
% INPUT : fileName - String value defining the name of the file that data
% is written to in a comma delimited format.
% OUTPUT: none
%
% EXAMPLE: plotandwriteexamplelegacy('7', 30, 'testdata.dat')
%
% See also plotandwriteexample twoshimmerexamplelegacy twoshimmerexample ShimmerHandleClass
close all % close all previous figures
% Note: these constants are only relevant to this examplescript and are not used
% by the ShimmerHandle Class
NO_SAMPLES_IN_PLOT = 500; % Number of samples that will be displayed in the plot at any one time
DELAY_PERIOD = 0.2; % A delay period of time in seconds between data read operations
%%
shimmer = ShimmerHandleClass(comPort); % Define shimmer as a ShimmerHandle Class instance with comPort1
if (shimmer.connect) % TRUE if the shimmer connects
% Define settings for shimmer
shimmer.setsamplingrate(51.2); % Set the shimmer sampling rate to 51.2Hz
shimmer.setinternalboard('None'); % Set the shimmer internal daughter board to 'None'
shimmer.setenabledsensors('Accel',1); % Enable the shimmer 1 accelerometer
shimmer.setaccelrange(0); % Set the accelerometer range to 0 (+/- 1.5g)
iAccelXShimmer = shimmer.getsignalindex('Accelerometer X'); % Determine the column index of the Accelerometer X-axis signal
iAccelYShimmer = shimmer.getsignalindex('Accelerometer Y'); % Determine the column index of the Accelerometer Y-axis signal
iAccelZShimmer = shimmer.getsignalindex('Accelerometer Z'); % Determine the column index of the Accelerometer Z-axis signal
signalNameArray = shimmer.getenabledsignalnames; % Get the list of enabled signals
signalNamesString = char(signalNameArray(1,1)); % Create a single string, signalNamesString
for i= 2:length(signalNameArray) % which lists the names of the enabled
tabbedNextSignalName = [char(9), char(signalNameArray(1,i))]; % Add tab delimiter before signal name
signalNamesString = strcat(signalNamesString,tabbedNextSignalName); % Concatenate signal names delimited by a tab.
end
dlmwrite(fileName, signalNamesString, '%s'); % Write the signalNamesString as the first row of the file
if (shimmer.start) % TRUE if the shimmer starts streaming
plotDataBuffer = [];
newData = [];
h.figure1=figure('Name','Shimmer 1'); % Create a handle to figure for plotting data from shimmer
elapsedTime = 0; % Reset to 0
tic; % Start timer
while (elapsedTime < captureDuration)
pause(DELAY_PERIOD); % Pause for this period of time on each iteration to allow data to arrive in the buffer
newData = shimmer.getuncalibrateddata; % Read the latest uncalibrated data from shimmer data buffer
% NOTE: The method 'getuncalibrateddata' is a legacy method from the beta release.
% The user is advised to use the replacement method 'getdata' as an alternative.
% (see plotandwriteexample).
if ~isempty(newData) % TRUE if new data has arrived
dlmwrite(fileName, newData, '-append', 'delimiter', '\t'); % Append the new data to the file in a tab delimited format
plotDataBuffer = [plotDataBuffer; newData]; % Update the plotDataBuffer with the new data
if(length(plotDataBuffer) > NO_SAMPLES_IN_PLOT)
plotDataBuffer = plotDataBuffer((length(plotDataBuffer) - NO_SAMPLES_IN_PLOT):end, :); % Trim excess previous data from array for plotting purposes
end
accelDataShimmer = [plotDataBuffer(:,iAccelXShimmer), plotDataBuffer(:,iAccelYShimmer), plotDataBuffer(:,iAccelZShimmer)]; % Extract only the columns of accelerometer data
set(0,'CurrentFigure',h.figure1);
plot(accelDataShimmer); % Plot the accelerometer data
title('Shimmer 1 Accelerometer Data'); % Add title to the plot
axis([0 NO_SAMPLES_IN_PLOT 0 4095]); % Define min and max values for axis
legend('Accel X','Accel Y','Accel Z') % Add legend to plot
end
elapsedTime = elapsedTime + toc; % Stop timer and add to elapsed time
tic; % Start timer
end
elapsedTime = elapsedTime + toc; % Stop timer
shimmer.stop; % Stop data streaming
end
shimmer.disconnect; % Disconnect from shimmer
end
clear all; % Remove all variables from memory