timedelay.comp
1 component timedelay "The equivalent of a time-delay relay"; 2 3 pin in bit in; 4 pin out bit out """Follows the value of \\fBin\\fR after applying the delays 5 \\fBon-delay\\fR and \\fBoff-delay\\fR."""; 6 7 pin in float on-delay = 0.5 """The time, in seconds, for which \\fBin\\fR must be 8 \\fBtrue\\fR before \\fBout\\fR becomes \\fBtrue\\fR"""; 9 pin in float off-delay = 0.5 """The time, in seconds, for which \\fBin\\fR must be 10 \\fBfalse\\fR before \\fBout\\fR becomes \\fBfalse\\fR"""; 11 12 pin out float elapsed "Current value of the internal timer"; 13 variable double timer; 14 15 function _; 16 17 license "GPL"; 18 author "Jeff Epler, based on works by Stephen Wille Padnos and John Kasunich"; 19 ;; 20 hal_bit_t in_ = in; 21 if(in_ != out) { 22 timer += fperiod; 23 elapsed = timer; 24 if(in_) { 25 if(timer >= on_delay) { 26 out = 1; 27 timer = 0.0; 28 } 29 } else { 30 if(timer >= off_delay) { 31 out = 0; 32 timer = 0.0; 33 } 34 } 35 } else { 36 timer = 0.0; 37 }