-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcountdown_clock.m
134 lines (133 loc) · 3.02 KB
/
countdown_clock.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
function hf = countdown_clock(time,ptime);
%COUNTDOWN_CLOCK Counts down times less than an hour in seconds.
%
% COUNTDOWN_CLOCK(TIME) Opens a figure with a text uicontrol
% to display a minutes and seconds countdown clock. The clock
% counts down for TIME seconds. TIME must be less than 3600
% seconds (1 hour). The figure closes two (2) seconds after the
% countdown ends.
%
% COUNTDOWN_CLOCK(TIME,PTIME) Will close the figure PTIME
% seconds after the countdown.
%
% NOTES: 1. Input countdown time TIME must be in seconds and
% be less than an hour (3600 seconds).
%
% 13-Jul-2018 * Mack Gardner-Morse
%
%#######################################################################
%
% Check Input Countdown Time
%
if nargin<1
time = -720; % Countdown time in seconds
end
%
if nargin<2
ptime = 2; % Pause time after countdown in seconds
end
%
if abs(time)>3600
error(['ERROR in countdown_clock: Only counts down times less ', ...
'than an hour!']);
end
%
if time>0
time = -time;
end
%
if ptime<0
ptime = 2;
end
%
% Setup Figure and Text UIControl
%
hf = figure('Units','pixels','Position',[600 600 200 80], ...
'MenuBar','none','Name','Timer','NumberTitle','off', ...
'Resize','off','HandleVisibility','off', ...
'DeleteFcn',@del_timer);
%
hd = uicontrol(hf,'Style','text','Position',[10 10 180 60], ...
'BackgroundColor',[0.8 0.8 0.8],'FontSize',36, ...
'FontWeight','bold');
%
% Get and Set Initial Times
%
t0 = clock;
%
str = time_frmt(time);
set(hd,'String',str);
%
% Setup and Start Timer
%
ht = timer('TimerFcn',@new_time,'Period',1,'ExecutionMode', ...
'FixedRate','StopFcn',@end_time,'TasksToExecute', ...
floor(abs(time-1)));
start(ht);
%
return
%
% Delete Timer Function
%
function del_timer(varargin)
%
% Delete Timer When Figure is Closed
%
try
stop(ht);
delete(ht);
end
end
%
% Close Countdown Clock Function
%
function end_time(varargin)
%
% Stop and Delete Timer and Delete Figure
%
% delete(ht); % Delete timer
pause(ptime);
delete(hf); % Delete figure
end
%
% New Time Function
%
function new_time(varargin)
%
% Get New Time
%
etim = etime(clock,t0);
str = time_frmt(time+etim);
set(hd,'String',str);
end
%
% Time Format Function
%
function str = time_frmt(time)
%
% Format the Time String
%
time = abs(time);
if time>3600
hrs = floor(time/3600);
time = time-3600*hrs;
end
mins = floor(time/60);
secs = floor(time-60*mins);
%
m = sprintf('%1.0f:',mins);
%
if mins < 10
m = sprintf('0%1.0f:',mins);
end
%
s = sprintf('%1.0f',secs);
if secs < 10
s = sprintf('0%1.0f',secs);
end
%
str = [m s];
%
end
%
end