forked from debapratimsaha/PacedStroopTest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPacedStroopTest.m
134 lines (119 loc) · 6.48 KB
/
PacedStroopTest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
function [timerObj,sPortObj] = PacedStroopTest(LogSerialData,Congruence)
% Top level file to run the Stroop Test and capture physiological data
%
% Description
% This is the top level file that runs Stroop Test while capturing the
% user inputs in a variable. Optionally, Physiological data is also
% captured using Wi-MicroDig bluetooth receiver. For the Stroop test,
% a figure appears every 3s for a total of "totalRunTime" seconds as
% defined in the "argument check" section of this file. The user has
% to speak/choose the color of the word irrespective of the word that
% they can see.
%
% One has to set the base folder in "dataDirPath" below where the
% test data will be saved. Default is "C:\Users\Public".
%
% Inputs
% LogSerialData : This flag sets the need to connect to a serial
% port to collect physio data while user is taking
% paced stroop test.
% 1= Try to Connect to Serial Port
% 0= DO NOT try to connect to serial port
% Congruence : [OPTIONAL][String]
% (as mentioned in parentheses)
% If this arguement is provided, based on this value
% Congruent (C) or Incongruent(IC) or Randomised(R)
% Stroop Test will run.
%
% Outputs
% timerObj : Timer Obj (may be used to stop the timer)
% serialObj : Serial Port Obj (may be used to free the serial port)
%
% Deliverables (saved on hard disk)
% # A data file containing the physiological data -- (ECG, EDA, Skin Temp etc.)
% # A data file containing Word, Color and User Input
% -------------------------------------------------------------------------
% Copyright (c) 30-Nov-2014
% Deba Pratim Saha,
% Electrical Engineer, M.S.,
% Ph.D. Student, ECE Virginia Tech.
% Email: [email protected]
% -------------------------------------------------------------------------
%% Arg Check and Global Specs definitions
totalRunTime = 180; % This is the total time for the test. Based on
% the Congruence string, Stroop test will run in
% Congruent, Incongruent or Randomised modes.
global congruenceFlagSet;
if nargin>3 || nargin==0
error('err:ArgChk','This function takes max of 2 inputs');
else if nargin==2 % Congruence not set by user
Congruence = 'IC'; % This value specifies that 3min stroop test will
% start with 1min each of IC --> C --> IC phases.
congruenceFlagSet = false; % Indicates that congruence flag was not passed by user
else if nargin==3 % Congruence set by user
congruenceFlagSet = true; % Indicates that congruence flag was passed by user
end
end
end
% data dir setting
global dataDirPath;
dataDirPath = ('C:\Users\Public\');
%% Initialise Wi-MicroDig ports and communication protocol
if LogSerialData
% Free previously used COM ports
delete(instrfind('Type', 'serial'));
% Create and Open serial port for Wi-MicroDig
sPortObj = serial('COM5');
sPortObj.BytesAvailableFcnCount = 9*5; % set the property so that BytesAvailableFcn is called when byte count = 9 [for 2 ports of data]
sPortObj.BytesAvailableFcnMode = 'byte'; % bytes-available event is generated when BytesAvailableFcnCount # of bytes are available
fopen(sPortObj);
serialDataDump = evalin('base','{}'); % create the variable in base workspace
% setup the digitizer
fwrite(sPortObj,[240 125 0 90 0 247]); % send host mode message to digitizer
fwrite(sPortObj,[240 125 0 34 247]); % send reset message to digitizer
fwrite(sPortObj,[240 125 0 3 0 5 247]); % send interval message to digitizer to set sampling interval to 5 ms for all enabled inputs
fwrite(sPortObj,[240 125 0 2 64 247]); % send resolution message to digitizer to set sampling resolution to 10-bit for input 1
fwrite(sPortObj,[240 125 0 2 65 247]); % send resolution message to digitizer to set sampling resolution to 10-bit for input 2
pause(3); % wait until the digitizer has sent host mode and reset confirmation messages
b = get(sPortObj, 'BytesAvailable'); % find out how many bytes are in the buffer
fread(sPortObj,b); % empty buffer by reading from it
else
% Initialise output var in case one doesn't need physio data
sPortObj = {};
end
%% Get test specific data
FigHandle = figure('Visible','Off'); % Create an invisible figure now to use UserData field
prompt = {'Enter User ID:','Enter Phase Number:','Location to save data:'};
dlgTitle = 'Test Data';
numLines = 1;
defs = {'xx','yyyy','G:\OneDrive\RESEARCH\Knapp Research Group\Projects\CBAR-Project\VirtualGroceryStore\data\userstudy-phase1\Stroop'};
answers = inputdlg(prompt,dlgTitle,numLines,defs);
if isempty(strcmp(answers,'{}'))
error('err:testStat','Please input the test statistics');
else
% Change directory path if provided by the user
if (~isempty(answers(3)))
dataDirPath = char(answers(3));
end
set(FigHandle,'UserData',struct('userID',answers(1),...
'PhaseNum',answers(2),...
'dataDirPath',dataDirPath,...
'setFigProps',1));
end
%% Start the actual test and data capturing
if LogSerialData
fwrite(sPortObj,[240 125 0 1 64 247]); % send stream message to digitizer to turn on continuous sampling for input 1
fwrite(sPortObj,[240 125 0 1 65 247]); % send stream message to digitizer to turn on continuous sampling for input 1
while get(sPortObj, 'BytesAvailable') < 82 end % wait for digitizer to send confirmation message (6 bytes)
fread(sPortObj,82);
sPortObj.BytesAvailableFcn = {@logSerialData}; % set the bytesAvailableFcn to the named callback function
end
timerObj = runStroopTestWtimerWresults(totalRunTime,sPortObj,Congruence,FigHandle); % Get timer obj which starts the Stroop Test UI
start(timerObj);
function logSerialData(obj,event)
sPacket = fread(obj,obj.BytesAvailableFcnCount);
serialDataDump = [serialDataDump;...
{sPacket,clock}];
assignin('base','serialDataDump',serialDataDump);
end
end