-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseGamepad.m
197 lines (149 loc) · 8.92 KB
/
seGamepad.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
classdef seGamepad < handle
%seGamepad Class definition for SEGAMEPAD
% Creates a "gamepad" programming object in order to receive and
% record subject's responses.
properties
isPresent; % Flag specifying if a gamepad device is connected or disconnected
acceptedResponses; % (num) accepted gamepad-button numbers
end
properties(Hidden = true)
escape_keys = zeros(1,256); % Keyboard key combination to quit experiment
pause_keys = zeros(1,256); % Keyboard key combination to pause experiment
ptGamepadIndex; % Psychtoolbox index for gamepad devices (need for KbCheck)
end
properties(Constant = true, Hidden = true)
DEFAULT_WAIT_DURATION = 5.000; % (secs) Default duration to wait for a subject response
EVENT_CODE_PAUSE = 255;
EVENT_CODE_END = 255;
end
methods
function obj = seGamepad(varargin)
% Creates an SEGAMEPAD object to collect subject responses via
% an external USB Gamepad.
%
% Example:
% responseDuration = 5.000 % wait for 5.000 seconds
% acceptedResponses = [1 2 3 4] % accepted only buttons 1-4
% objGamepad = seGamepad(responseDuration, acceptedResponses)
%
switch nargin
case 0 % No inputs -- use default values
if ismac
obj.acceptedResponses = 01:10; % (num) Default accepted gamepad-button numbers
elseif ispc
obj.acceptedResponses = 01:04;
end
case 1
obj.acceptedResponses = varargin{1};
end
% Setup escape/pause keys
KbName('UnifyKeyNames'); % Ensure keynames work on Mac & PC
obj.escape_keys(KbName('LeftControl')) = 1; % Escape keys: Left-command + Left-alt/option + Left-control
obj.escape_keys(KbName('LeftAlt')) = 1; %
obj.escape_keys(KbName('LeftGUI')) = 1; %
obj.escape_keys(KbName('RightGUI')) = 1; %
obj.escape_keys(KbName('RightAlt')) = 1; %
obj.pause_keys(KbName('LeftControl')) = 1; % Pause keys: Left-Command + p
obj.pause_keys(KbName('LeftAlt')) = 1; %
obj.pause_keys(KbName('LeftGUI')) = 1; %
obj.pause_keys(KbName('space')) = 1; %
% Check computer for gamepad
if ismac
obj.ptGamepadIndex = GetGamepadIndices;
elseif ispc
obj.ptGamepadIndex = 0;
end
if(~isempty(obj.ptGamepadIndex))
obj.isPresent = true;
else
obj.isPresent = false;
end
end
function resp = waitForResponse(obj, varargin)
% Waits for an acceptable response from the gamepad object
%
% Example:
% objGamepad = seGamepad();
% objGamepad.waitForResponse(); % wait 3.000 seconds for a acceptable response
%
% objGamepad.waitForResponse(3.000, objDAQ); % sends out an event code after waiting 3.000 seconds for a response
%
startTime = GetSecs();
switch nargin
case 0
case 1
waitDuration = obj.DEFAULT_WAIT_DURATION;
opt_daq.isPresent = false;
case 2
waitDuration = varargin{1};
opt_daq.isPresent = false;
case 3
waitDuration = varargin{1};
opt_daq = varargin{2};
end
FlushEvents('keyDown');
resp.responseTime = NaN;
resp.keycode = NaN;
if(obj.isPresent)
exitLoop = false;
while not(exitLoop); % keep polling gamepad & keyboard for user response or experimentor pause/end key combination
% ------------------------------------ %
% First, check if time limit exceeded %
% ------------------------------------ %
currentDuration = GetSecs - startTime;
if currentDuration > waitDuration
exitLoop = true; % end waiting for response
else
% ------------------------------- %
% Poll gamepad for button presses %
% ------------------------------- %
if ismac
[keyIsDown, keyIsDownTimeStamp, keyCode] = KbCheck(obj.ptGamepadIndex);
elseif ispc
[~,~,~,btn] = WinJoystickMex(obj.ptGamepadIndex);
keyIsDownTimeStamp = GetSecs();
keyCode = find(btn, 1);
keyIsDown = ~isempty(keyCode);
end
[KEYBOARD_keyIsDown, ~,KEYBOARD_keyCode] = KbCheck();
% ---------------------------- %
% check for quit or pause keys %
% ---------------------------- %
if (KEYBOARD_keyIsDown && isequal(KEYBOARD_keyCode, obj.escape_keys))
fprintf('Escape key-combination activated.\nProgram stopped.\n');
opt_daq.sendEventCode(obj.EVENT_CODE_END); % send out event codes if we have a daq var is inputted
ShowCursor;
Screen('CloseAll'); % close psychtoolbox
return; % quit experiment
elseif (KEYBOARD_keyIsDown && isequal(KEYBOARD_keyCode, obj.pause_keys)) % pause experiment
fprintf('Experiment paused.\nHit any key to continue...\n');
opt_daq.sendEventCode(obj.EVENT_CODE_PAUSE); % send out event codes if we have a daq var is inputted
WaitSecs(1);
KbWait;
fprintf('Experiment Unpaused\n');
end
if(keyIsDown)
% ---------------------------- %
% Check for Accepted Responses %
% ---------------------------- %
foundResponses = intersect(KbName(KbName(keyCode)), obj.acceptedResponses);
if(~isempty(foundResponses))
for singleFoundResponse = foundResponses
if(opt_daq.isPresent)
opt_daq.sendEventCode(singleFoundResponse); % send out event codes if we have a daq var is inputted
end
% fprintf('Button pressed = %d\n', singleFoundResponse);
end
resp.responseTime = keyIsDownTimeStamp - startTime; % save response time
resp.keycode = foundResponses; % save found responses
exitLoop = true; % exit keyboard polling loop
end
end % check if gamepad key is pressed
end % check response time duration not exceeded
end % loop for gamepad key press
else
error('USB Gamepad is not connected'); % Throw an error if gamepad IS NOT present
end % gamepad present check
end % waitForResponse() method
end
end