-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathu_delay2.PAS
195 lines (147 loc) · 3.57 KB
/
u_delay2.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
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
unit u_delay2;
interface
uses dos;
var delay_calibration:longint;
function getclock:word;
function supergetclock:longint;
procedure delay2(delayms:word);
procedure calibratedelay(path:string);
implementation
var starting_h:word;
function getclock;
var
hh,mm,ss,ms:word;
begin
gettime(hh,mm,ss,ms);
if starting_h > hh then hh:=hh+24;
getclock := ((ms) DIV 100) + (ss*10) + (mm*600) + (hh*36000);
end;
function supergetclock;
var
hh,mm,ss,ms:word;
begin
gettime(hh,mm,ss,ms);
if starting_h > hh then hh:=hh+24;
supergetclock := ((ms) ) + (ss*1000) + (mm*60000) + (hh*3600000);
end;
(*
1 hour = 60 min = 3600 sec = 3600000 ms
3600000 = 3600000 = 3600000 = 3600000
procedure delay2;
var
hh,mm,ss,ms,timenow,delayend:word;
begin
gettime(hh,mm,ss,ms);
if starting_h > hh then hh:=hh+24;
hh:=hh - starting_h;
delayend := ((ms + delayms) DIV 100) +
(ss*10) + (mm*600) + (hh*36000);
repeat
gettime(hh,mm,ss,ms);
if starting_h > hh then hh:=hh+24;
hh:=hh - starting_h;
timenow := ((ms) DIV 100) +
(ss*10) + (mm*600) + (hh*36000);
until timenow>=delayend;
end;
*)
procedure delay2;
var w,w2,w3,
hh,mm,ss,ms,timenow,delayend:word;
begin
if (delayms<1000) and (delay_calibration<60000) then
begin
w2:=round(delay_calibration * (delayms/1000));
for w:=1 to w2 do w3:=round(0.01*w/2+sqrt(w));
end else
begin
gettime(hh,mm,ss,ms);
if starting_h > hh then hh:=hh+24;
hh:=hh - starting_h;
delayend := ((ms + delayms) DIV 100) +
(ss*10) + (mm*600) + (hh*36000);
repeat
gettime(hh,mm,ss,ms);
if starting_h > hh then hh:=hh+24;
hh:=hh - starting_h;
timenow := ((ms) DIV 100) +
(ss*10) + (mm*600) + (hh*36000);
until timenow>=delayend;
end;
end;
procedure initdelay;
var
hh,mm,ss,ms:word;
begin
gettime(hh,mm,ss,ms);
starting_h:=hh;
end;
procedure calibratedelay;
{calibrate 1/20sec delay}
var
w,w2:longint; w3:word;
c,c2,c3:longint;
f:file of longint;
cal:array[1..3] of longint;
i:byte;
begin
assign(f,path);
{$I-} reset(f);
if ioresult=0 then
begin
read(f,delay_calibration);
close(f);
end
else
begin
for i:=1 to 3 do
begin
repeat
c:=supergetclock;
w2:=6000;
for w:=1 to w2 do w3:=round(0.01*w/2+sqrt(w));
c2:=supergetclock;
c3:=c2-c;
until c3>0;
delay_calibration:=round(w2 * (1000 / c3) ); {1/50 of a sec}
{ writeln('Iterations: ',w2);
writeln('Time required: ',c3);
writeln('1 sec worth of iterations: ',delay_calibration); }
cal[i]:=delay_calibration;
end;
if cal[1]>cal[2] then begin; c:=cal[2]; cal[2]:=cal[1]; cal[1]:=c; end;
if cal[2]>cal[3] then begin; c:=cal[3]; cal[3]:=cal[2]; cal[2]:=c; end;
if cal[1]>cal[2] then begin; c:=cal[2]; cal[2]:=cal[1]; cal[1]:=c; end;
{write('Possible calibration values: ');
for i:=1 to 3 do write(cal[i],' ');
writeln;
writeln('Using: ',cal[2]);}
delay_calibration:=cal[2];
rewrite(f);
write(f,delay_calibration);
close(f);
end;
{
readln;
for c3:=1 to 16 do
begin
c:=supergetclock;
write('Testing 0.1 sec delay...');
w2:=delay_calibration DIV 10;
for w:=1 to w2 do w3:=round(0.01*w/2+sqrt(w));
c2:=supergetclock;
writeln('Actual delay ',c2-c,' ms');
end;
for c3:=1 to 6 do
begin
c:=supergetclock;
write('Testing 1 sec delay...');
w2:=delay_calibration;
for w:=1 to w2 do w3:=round(0.01*w/2+sqrt(w));
c2:=supergetclock;
writeln('Actual delay ',c2-c,' ms');
end; }
end;
begin
initdelay;
end.