-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonLoad.m
More file actions
142 lines (127 loc) · 4.7 KB
/
ButtonLoad.m
File metadata and controls
142 lines (127 loc) · 4.7 KB
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
% use ButtonSetup to define keys file
function k = ButtonLoad(keyfile)
if nargin < 1
keyfile = GetKeyfileName;
end
if ~keyfile
error("No keyfile");
end
%reset devices
clear PsychHID;
clear KbCheck;
DisableKeysForKbCheck([]);
opts.Interpreter = 'tex';
devices_found = 0;
while ~devices_found
clear keys;
load(keyfile, 'keys');
if exist('keys','var')
k = keys;
[k.keyboard_index, k.keyboard_name] = GetDevice(k.keyboard_loc);
[k.trigger_index, k.trigger_name] = GetDevice(k.trigger_loc);
[k.left_index, k.left_name] = GetDevice(k.left_loc);
[k.right_index, k.right_name] = GetDevice(k.right_loc);
% convenience - they are only different in the mock
k.response_indices = unique([k.left_index, k.right_index]);
devices_found = length([k.keyboard_index, k.trigger_index, ...
k.left_index, k.right_index]) == 4;
else
devices_found = false;
end
if ~devices_found
message = '\fontsize{14}Unable to find devices.';
title = 'Warning';
opts.Default = ButtonText('Run Setup');
answer = questdlg(message, title, ...
ButtonText('Try another file'), ...
ButtonText('Ignore'), ...
ButtonText('Run Setup'), opts);
switch answer
case ButtonText('Try another file')
keyfile = GetKeyfileName;
case ButtonText('Run Setup')
keyfile = ButtonSetup;
case ButtonText('Ignore')
return;
end
else
message = ["\fontsize{14}Keyboard device: " + k.keyboard_name];
message = [message, "", "Trigger device: " + k.trigger_name];
message = [message, "", "Left response device: " + k.left_name];
message = [message, "", "Right response device: " + k.right_name];
message = [message, "", "Trigger key: " + PrettyName(k.trigger)];
message = [message, "", "Left buttons: " + ...
PrettyName(k.b0) + " " + ...
PrettyName(k.b1) + " " + ...
PrettyName(k.b2) + " " + ...
PrettyName(k.b3) + " " + ...
PrettyName(k.b4)];
message = [message, "", "Right buttons: " + ...
PrettyName(k.b5) + " " + ...
PrettyName(k.b6) + " " + ...
PrettyName(k.b7) + " " + ...
PrettyName(k.b8) + " " + ...
PrettyName(k.b9)];
message = [message, "", "Is this correct?"];
opts.Default = ButtonText('Yes');
answer = questdlg(message, 'Devices loaded', ...
ButtonText('Yes'), ...
ButtonText('No, try another file'), ...
ButtonText('No, run button setup'), opts);
switch answer
case ButtonText('No, try another file')
keyfile = GetKeyfileName;
devices_found = false;
case ButtonText('No, run button setup')
keyfile = ButtonSetup;
devices_found = false;
end
end
end
end
function kfile = GetKeyfileName
disp("Select keyfile");
[kfilename, kdir] = uigetfile(pwd, 'Select key file', '*.mat');
kfile = fullfile(kdir, kfilename);
if ~kfilename
opts.Interpreter = 'tex';
opts.Default = ButtonText('Yes');
answer = questdlg("\fontsize{14}Run button setup?", ...
'No keyfile', ...
ButtonText('Yes'), ButtonText('No'), ...
opts);
switch answer
case ButtonText('Yes')
kfile = ButtonSetup;
case ButtonText('No')
error("No keyfile");
end
end
end
function ConvertedText = ButtonText(input)
ConvertedText = "<html><font size='4'>" + input;
end
function newname = PrettyName(keycode)
newname = KbName(keycode);
if length(newname) == 2
newname = newname(1);
end
end
% under linux we don't have unique locationIDs
% instead we use interfaceID
function [index, name] = GetDevice(location)
index = [];
name = [];
if isunix && ~ismac
[indices, names, infos] = GetKeyboardIndices();
for i = 1:length(indices)
info = struct2table(infos{i}, 'AsArray', true);
if info.interfaceID == location
index = indices(i);
name = names(i);
end
end
else
[index, name] = GetKeyboardIndices([], [], location);
end
end