-
Notifications
You must be signed in to change notification settings - Fork 0
/
timerframe.pas
147 lines (126 loc) · 3.32 KB
/
timerframe.pas
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
unit TimerFrame;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, StdCtrls, ExtCtrls, uplaysound;
type
{ TFrameTimer }
TFrameTimer = class(TFrame)
BtnStart: TButton;
BtnStop: TButton;
Edt: TEdit;
PlySnd: Tplaysound;
Tmr: TTimer;
procedure BtnStartClick({%H-}Sender: TObject);
procedure BtnStopClick({%H-}Sender: TObject);
procedure TmrStartTimer({%H-}Sender: TObject);
procedure TmrStopTimer({%H-}Sender: TObject);
procedure TmrTimer({%H-}Sender: TObject);
private
FAlertAudioFile: String;
FAlertDone: Boolean;
FAlertTime: TTime;
FAnswerTime: TTime;
FAnswerTimer: Boolean;
FOnStart: TNotifyEvent;
FOnStop: TNotifyEvent;
FStartAudioFile: String;
FStartTime: TDateTime;
FStopAudioFile: String;
FStopDone: Boolean;
FStopTime: TTime;
procedure DoStart;
procedure DoStop;
procedure PlayAudio(const aAudioFile: String);
public
property StartAudioFile: String read FStartAudioFile write FStartAudioFile;
property AlertAudioFile: String read FAlertAudioFile write FAlertAudioFile;
property StopAudioFile: String read FStopAudioFile write FStopAudioFile;
property AlertTime: TTIme read FAlertTime;
property StopTime: TTime read FStopTime;
property StartTime: TDateTime read FStartTime write FStartTime;
property AnswerTimer: Boolean read FAnswerTimer write FAnswerTimer;
property OnStart: TNotifyEvent read FOnStart write FOnStart;
property OnStop: TNotifyEvent read FOnStop write FOnStop;
end;
implementation
{$R *.lfm}
function TimeToStopWatchStr(aTime: TTime): String;
begin
DateTimeToString(Result, 'nn:ss', aTime);
end;
{ TFrameTimer }
procedure TFrameTimer.BtnStartClick(Sender: TObject);
begin
Edt.Text:=TimeToStopWatchStr(0);;
FStartTime:=Now;
FStopTime:=1/SecsPerDay*60;
FAlertTime:=1/SecsPerDay*50;
FAnswerTime:=FStopTime+1/SecsPerDay*15;
FAlertDone:=False;
FStopDone:=False;
PlayAudio(FStartAudioFile);
Tmr.Enabled:=True;
end;
procedure TFrameTimer.BtnStopClick(Sender: TObject);
begin
Tmr.Enabled:=False;
PlySnd.StopSound;
end;
procedure TFrameTimer.TmrStartTimer(Sender: TObject);
begin
DoStart;
BtnStop.Enabled:=True;
BtnStart.Enabled:=False;
end;
procedure TFrameTimer.TmrStopTimer(Sender: TObject);
begin
BtnStart.Enabled:=True;
BtnStop.Enabled:=False;
DoStop;
end;
procedure TFrameTimer.TmrTimer(Sender: TObject);
var
aTime: TDateTime;
begin
aTime:=Now-FStartTime;
if (aTime>=FAlertTime) and not FAlertDone then
begin
FAlertDone:=True;
PlayAudio(FAlertAudioFile);
end;
if (aTime>=FStopTime) and not FStopDone then
begin
PlayAudio(FStopAudioFile);
FStopDone:=True;
if not AnswerTimer then
begin
Tmr.Enabled:=False;
aTime:=FStopTime;
end;
end;
if AnswerTimer and (aTime>=FAnswerTime) then
begin
PlayAudio(FStopAudioFile);
Tmr.Enabled:=False;
aTime:=FStopTime;
end;
Edt.Text:=TimeToStopWatchStr(aTime);
end;
procedure TFrameTimer.DoStart;
begin
if Assigned(FOnStart) then
FOnStart(Self);
end;
procedure TFrameTimer.DoStop;
begin
if Assigned(FOnStop) then
FOnStop(Self);
end;
procedure TFrameTimer.PlayAudio(const aAudioFile: String);
begin
PlySnd.PlayStyle:=psASync;
PlySnd.SoundFile:=aAudioFile;
PlySnd.Execute;
end;
end.