-
Notifications
You must be signed in to change notification settings - Fork 0
/
Breaktime.m
99 lines (86 loc) · 3.46 KB
/
Breaktime.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
function [HW, totalTime] = Breaktime(HW, duration, textFunc, afterText, ...
optionalAfter, autoAdvance)
%BREAKTIME Displays a countdown timer with text on the screen
% Required input arguments:
% HW: Hardware parameter structure (see Parameters.m)
% duration: Duration of break, in seconds
% Optional input arguments:
% textFunc: Text to display during break, either as a string or
% as a function taking in the time left, i.e.
% [ text ] = @(timeLeft)
% Pass in empty array [] to leave the following default in place:
% @(t) sprintf('Please take a %i second break!', t);
% afterText: after break, text to display
% default: 'Done! Press a key when ready...'
% optionalAfter: time (in seconds) after which a keypress
% will interrupt the countdown. Default: Inf (never)
% autoAdvance: if true, return without waiting for
% a keypress when timer expires. Defaults to false.
% Output:
% HW: Hardware parameter structure (with any changes)
% totalTime: Length of break in seconds
%
% Example:
% [~, ~, HW] = Parameters();
% Breaktime(HW, 5);
% Breaktime(HW, 5, @(t) 'Blah blah!', 'Ended!');
% Breaktime(HW, 60, ...
% @(t) sprintf('Optional after 10 s! Now: %i',t), [], 10);
% Breaktime(HW, 5, ...
% @(t) sprintf('Autoadvance! Now: %i',t), [], [], true);
if nargin < 3 || isempty(textFunc)
textFunc = @(t) sprintf('Please take a %i second break!', t);
end
if ischar(textFunc) % given a string instead of a function
textFunc = @(t) textFunc;
end
if nargin < 4 || isempty(afterText)
afterText = 'Okay to continue! Press a key when ready...';
end
if nargin < 5 || isempty(optionalAfter)
optionalAfter = Inf;
end
if nargin < 6 || isempty(autoAdvance)
autoAdvance = false;
end
[didHWInit HW] = InitializeHardware(HW);
caughtException = [];
try
start = GetSecs();
timeLeft = round(start + duration - GetSecs());
while timeLeft > 0 && ...
~(timeLeft < (duration - optionalAfter) && KbCheck)
for i=0:1
HW = ScreenCustomStereo(...
HW, 'SelectStereoDrawBuffer', HW.winPtr, i);
Screen('FillRect', HW.winPtr, 0); % black background
finalText = textFunc(timeLeft);
DrawFormattedText(HW.winPtr, finalText, ...
'center', 'center', ...
HW.white, 50);
end
HW = ScreenCustomStereo(HW, 'Flip', HW.winPtr);
WaitSecs(1/HW.fps);
timeLeft = round(start + duration - GetSecs());
end
% Done with countdown, draw final text
for i=0:1
HW = ScreenCustomStereo(...
HW, 'SelectStereoDrawBuffer', HW.winPtr, i);
Screen('FillRect', HW.winPtr, 0); % black background
DrawFormattedText(HW.winPtr, afterText, ...
'center', 'center', HW.white, 50);
end
HW = ScreenCustomStereo(HW, 'Flip', HW.winPtr);
if ~autoAdvance, KbWait([], 3); end
totalTime = start - GetSecs();
catch e
caughtException = e;
end
if didHWInit
HW = CleanupHardware(HW);
end
if ~isempty(caughtException)
rethrow(caughtException);
end
end