OS X: Gamepad bug?

The following code snippet fails to read the Gamepad ONLY for the first time through the
loop if I OMIT the initial call to Gamepad('GetNumButtons', 1); That is, if I just hard code
the number of buttons and make the first "contact" with Gamepad the call in which I set
buttons = [Gamepad('GetButton', 1, 1)... I get no reading of the gamepad the first time
through the loop, after that it is fine. If I include the prior call, as below, all works as
expected...

-m

numButtons = Gamepad('GetNumButtons', 1);
%for some reason Gamepad('GetButton...) does not work correctly unless you
%make a call to Gamepad prior to the first GetButton call
buttons = zeros(1, numButtons);

Screen('DrawTexture', w, fixation); % display the fixation
start_block_time = Screen('Flip', w);
start_block_time = start_block_time + trialtime - stimtime;
WaitSecs(trialtime - stimtime);

Screen('DrawTexture', w, fixationB);

%detect when the fixation turns blue

while (trial < trials)
vbl = Screen('Flip', w, start_block_time + (trialtime * trial) - slack);
while (GetSecs < (vbl + stimtime))
buttons=[Gamepad('GetButton', 1, 1) Gamepad('GetButton', 1, 2) Gamepad
('GetButton', 1, 3)];
checktime = GetSecs;
Nothing in the code that could cause this. Most likely initialization delay of PsychHID. Any
external Matlab function (M-Function, but especially MEX files like PsychHID, GetSecs or
Screen) has some extra delay at first invocation - Matlab needs to find and load it from the
filesystem, parse, compile or link it... Hardware related files like PsychHID perform some
init during first load.

Example: First invocaition of PsychHID on a fast MacBookPro after clear PsychHID: Over
200 milliseconds. Each successive invocation: 0.5 milliseconds --> 400 times slower on
first command.

So not a bug. But the general advice for time critical studies: Use each function once
before start of first trial if possible, do a WaitSecs, GetSecs, KbCheck, Screen at beginning
of your script when it doesn't matter.

More in the FAQ section on the Wiki about Performance tuning.

-mario


--- In psychtoolbox@yahoogroups.com, "Michael J. Tarr" <michael_tarr@...> wrote:
>
> The following code snippet fails to read the Gamepad ONLY for the first time through
the
> loop if I OMIT the initial call to Gamepad('GetNumButtons', 1); That is, if I just hard code
> the number of buttons and make the first "contact" with Gamepad the call in which I set
> buttons = [Gamepad('GetButton', 1, 1)... I get no reading of the gamepad the first time
> through the loop, after that it is fine. If I include the prior call, as below, all works as
> expected...
>
> -m
>
> numButtons = Gamepad('GetNumButtons', 1);
> %for some reason Gamepad('GetButton...) does not work correctly unless you
> %make a call to Gamepad prior to the first GetButton call
> buttons = zeros(1, numButtons);
>
> Screen('DrawTexture', w, fixation); % display the fixation
> start_block_time = Screen('Flip', w);
> start_block_time = start_block_time + trialtime - stimtime;
> WaitSecs(trialtime - stimtime);
>
> Screen('DrawTexture', w, fixationB);
>
> %detect when the fixation turns blue
>
> while (trial < trials)
> vbl = Screen('Flip', w, start_block_time + (trialtime * trial) - slack);
> while (GetSecs < (vbl + stimtime))
> buttons=[Gamepad('GetButton', 1, 1) Gamepad('GetButton', 1, 2) Gamepad
> ('GetButton', 1, 3)];
> checktime = GetSecs;
>