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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
function MultiTouchPinchDemo(dev, screenId, debug)
% MultiTouchPinchDemo([dev][, screenId=max][, debug=0]) - A basic demo for multi-touch touchscreens.
%
% Run it. Pressing the ESCape key, or comleting ten demo trials will stop it.
%
% This shows implementation of simple code for "two finger pinch" detection.
% The demo draws a line segment at random location and expects the user to put
% to finger on the touch screen to "squeeze" the line segment between their finger.
% It will complete a trial if both fingers are moved close enough and the line
% segment is between both fingers.
%
% Motivated by the line-intersection task from:
% https://psychtoolbox.discourse.group/t/help-with-multitouch/4953
%
% The demo will try to use the first available touchscreen, or if
% there isn't any touchscreen, the first available touchpad. You
% can also select a specific touch device by passing in its 'dev'
% device handle. Use of touchpads usually needs special configuration.
% See "help TouchInput" for more info.
%
% You can select a specific screen to display on - usually the screenId
% of the touch screen display surface - with the optional 'screenId' parameter,
% or it will select the default maximum screenId if omitted.
%
% This demo currently works on Linux + X11 display system, not on Linux + Wayland.
% It also works on MS-Windows 10 and later.
%
% For background info on capabilities and setup see "help TouchInput".
%
% History:
% 27-Apr-2023 mk Written. Derived from MultiTouchMinimalDemo.
% Setup useful PTB defaults:
PsychDefaultSetup(2);
if nargin < 1
dev = [];
end
if nargin < 2 || isempty(screenId)
screenId = max(Screen('Screens'));
end
if nargin < 3 || isempty(debug)
debug = 0;
end
% If no user-specified 'dev' was given, try to auto-select:
if isempty(dev)
% Get first touchscreen:
dev = min(GetTouchDeviceIndices([], 1));
end
if isempty(dev)
% Get first touchpad if no touchscreen found:
dev = min(GetTouchDeviceIndices([], 0));
end
if isempty(dev) || ~ismember(dev, GetTouchDeviceIndices)
fprintf('No touch input device found, or invalid dev given. Bye.\n');
return;
else
fprintf('Touch device properties:\n');
info = GetTouchDeviceInfo(dev);
disp(info);
end
% Open a default onscreen window with black background color and 0-1 color range:
[w, rect] = PsychImaging('OpenWindow', screenId, 0);
HideCursor(w);
% Get maximum supported dot diameter for smooth dots:
[~, maxSmoothPointSize] = Screen('DrawDots', w);
% Select good diameter for touch point blobs, but no more than what 'DrawDots' supports:
baseSize = min(RectWidth(rect) / 20, maxSmoothPointSize);
try
% Only ESCape allows to exit the demo:
RestrictKeysForKbCheck(KbName('ESCAPE'));
% Create and start touch queue for window and device:
TouchQueueCreate(w, dev);
TouchQueueStart(dev);
% Wait for the go!
KbReleaseWait;
% Ten trials:
for trial=1:10
% touch tracks active touch points:
touch = {};
touches = [];
intersectPoint = [];
% Select randomly located line segment:
l1 = [rand() * 200 + 200, rand() * 800 + 200];
l2 = [rand() * 800 + 200, rand() * 800 + 200];
% Loop for this trial: Run until keypress or line bisected.
while ~KbCheck && (isempty(intersectPoint) || ~isempty(touches))
% Process all currently pending touch events:
while TouchEventAvail(dev)
% Process next touch event 'evt':
evt = TouchEventGet(dev, w);
if evt.Type < 2
% Not a touch point, but something else:
continue;
end
% Touch blob id - Unique in the session:
id = evt.Keycode;
% In the list of known touches? Give idx for referencing it:
idx = find(touches == id);
% New touch point and not yet both two wanted touches registered?
if evt.Type == 2 && length(touches) < 2 && isempty(idx)
% Add to end of list:
touches(end+1) = id;
idx = length(touches);
touch{idx}.col = rand(3, 1);
touch{idx}.x = evt.MappedX;
touch{idx}.y = evt.MappedY;
touch{idx}.t = evt.Time;
end
% Moving known touch point?
if evt.Type == 3 && ~isempty(idx)
touch{idx}.x = evt.MappedX;
touch{idx}.y = evt.MappedY;
touch{idx}.t = evt.Time;
end
% Known touch released?
if evt.Type == 4 && ~isempty(idx)
% Remove from list:
touches = touches(touches ~= id);
% Remove from tracking array, rearrange items if needed:
if idx == 1 && length(touch) > 1
touch{1} = touch{2};
end
touch{2} = [];
end
end
% Draw all known active touch points:
for i = 1:length(touches)
Screen('DrawDots', w, [touch{i}.x, touch{i}.y], 3 * baseSize, touch{i}.col, [], 1, 1);
end
% Visualize "cut line" between the two fingers:
Screen('DrawLine', w, [0 1 0], l1(1), l1(2), l2(1), l2(2), 2);
% Exactly two touches as wanted?
if length(touches) == 2
% Find and draw mid-point, compute distance between fingers:
t1 = [touch{1}.x, touch{1}.y];
t2 = [touch{2}.x, touch{2}.y];
distance = norm(t1 - t2);
midpoint = mean([t1; t2]);
% Show mid-point and connecting line between fingers, also print
% distance between fingers in pixels:
Screen('DrawDots', w, [midpoint(1), midpoint(2)], 1 * baseSize, [0 1 0], [], 1, 1);
Screen('DrawLine', w, [1 0 0], t1(1), t1(2), t2(1), t2(2), 2);
Screen('DrawText', w, num2str(distance), midpoint(1), midpoint(2), [1 1 0]);
% Pinched tight enough?
if distance < 600 && isempty(intersectPoint)
% The following code snippet for line segment intersection is
% derived from copy-pasted bits of the linexline.m function from:
% https://github.com/preethamam/Line2Line-IntersectionPoint
%
% The code is is licensed under the BSD 2-Clause License, and
% Copyright (c) 2022, Preetham Manjunatha.
% SPDX license identifier "BSD-2"
% See: https://spdx.org/licenses/BSD-2-Clause.html
x1=t1(1);
x2=t2(1);
x3=l1(1);
x4=l2(1);
y1=t1(2);
y2=t2(2);
y3=l1(2);
y4=l2(2);
% Find line intersection parameters:
u = ((x1-x3)*(y1-y2) - (y1-y3)*(x1-x2)) / ((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4));
t = ((x1-x3)*(y3-y4) - (y1-y3)*(x3-x4)) / ((x1-x2)*(y3-y4)-(y1-y2)*(x3-x4));
% Intersection actually on the lines?
if (u >= 0 && u <= 1.0) && (t >= 0 && t <= 1.0)
% Yes. Compute and assign intersection point:
xi = ((x3 + u * (x4-x3)) + (x1 + t * (x2-x1))) / 2;
yi = ((y3 + u * (y4-y3)) + (y1 + t * (y2-y1))) / 2;
intersectPoint = [xi,yi];
end
end
end
% Valid intersection point?
if ~isempty(intersectPoint)
% Show it on line, instruct subject to release touches:
Screen('DrawDots', w, intersectPoint, 1 * baseSize, [0 1 1], [], 1, 1);
DrawFormattedText(w, 'Well done! Lift fingers for next trial!', 'center', 'center', 1);
% TODO: Store timestamps, distance, intersectPoint, other info
% with results for this trial:
else
DrawFormattedText(w, 'Pinch the line between two fingers!', 'center', 'center', 1);
end
% Done repainting - Show it:
Screen('Flip', w);
% This little bit here will provoke stimulus onset timing failures on
% MS Windows at debug level 1 for debugging if something is not quite right.
if debug && IsWin
WaitSecs(0.025);
[~, ~, ~, visualtimingmaybesane] = GetMouse(w)
end
% Next touch processing -> redraw -> flip cycle:
end
% End of trial.
% TODO store data...
% Next trial:
end
TouchQueueStop(dev);
TouchQueueRelease(dev);
RestrictKeysForKbCheck([]);
ShowCursor(w);
sca;
catch
TouchQueueRelease(dev);
RestrictKeysForKbCheck([]);
sca;
psychrethrow(psychlasterror);
end
|