summaryrefslogtreecommitdiff
path: root/src/hal/components/timedelay.comp
blob: cf26f724053bea2f07a9e78fae173a4c9e9115be (plain)
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
component timedelay "The equivalent of a time-delay relay";

pin in bit in;
pin out bit out """Follows the value of \\fBin\\fR after applying the delays
\\fBon-delay\\fR and \\fBoff-delay\\fR.""";

pin in float on-delay = 0.5 """The time, in seconds, for which \\fBin\\fR must be
\\fBtrue\\fR before \\fBout\\fR becomes \\fBtrue\\fR""";
pin in float off-delay = 0.5 """The time, in seconds, for which \\fBin\\fR must be
\\fBfalse\\fR before \\fBout\\fR becomes \\fBfalse\\fR""";

pin out float elapsed "Current value of the internal timer";
variable double timer;

function _;

license "GPL";
author "Jeff Epler, based on works by Stephen Wille Padnos and John Kasunich";
;;
hal_bit_t in_ = in;
if(in_ != out) {
    timer += fperiod;
    elapsed = timer;
    if(in_) {
        if(timer >= on_delay) {
            out = 1;
            timer = 0.0;
        }
    } else {
        if(timer >= off_delay) {
            out = 0;
            timer = 0.0;
        }
    }
} else {
    timer = 0.0;
}