1
1
/*
2
2
Copyright (C) 2016 Albert van Dalen http://www.avdweb.nl
3
- This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
4
- as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
5
- This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
6
- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License at http://www.gnu.org/licenses .
3
+ This program is free software: you can redistribute it and/or modify it under
4
+ the terms of the GNU General Public License as published by the Free Software
5
+ Foundation, either version 3 of the License, or (at your option) any later
6
+ version. This program is distributed in the hope that it will be useful, but
7
+ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8
+ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License at
9
+ http://www.gnu.org/licenses .
7
10
8
11
AUTHOR: Albert van Dalen
9
12
WEBSITE: http://www.avdweb.nl/arduino/libraries/virtualdelay.html
@@ -20,40 +23,38 @@ running _____| |__________
20
23
21
24
elapsed() ________________|__________
22
25
23
- set timeOut _____|_____________________
26
+ set timeOut _____|_____________________
24
27
25
28
*/
26
29
27
- #include < Arduino.h>
28
30
#include " avdweb_VirtualDelay.h"
29
31
30
- VirtualDelay::VirtualDelay (unsigned long (*timerFunctionPtr)()):
31
- timerFunctionPtr(timerFunctionPtr)
32
- {
32
+ VirtualDelay::VirtualDelay (
33
+ unsigned long (*timerFunctionPtr)())
34
+ : timerFunctionPtr(timerFunctionPtr)
35
+ {
33
36
}
34
37
35
38
void VirtualDelay::start (signed long delay) // 0...2^31
36
- { if (!running)
37
- { running = 1 ;
38
- timeOut = (*timerFunctionPtr)() + abs (delay);
39
- }
39
+ {
40
+ if (!running) {
41
+ running = 1 ;
42
+ timeOut = (*timerFunctionPtr)() + abs (delay);
43
+ }
40
44
}
41
45
42
- bool VirtualDelay::elapsed ()
43
- { bool pulse = 0 ;
44
- if (running)
45
- { // if((signed long)(*timerFunctionPtr)() >= timeOut) // bug, not equal to:
46
- if ((signed long )((*timerFunctionPtr)() - timeOut) >= 0 ) // fix rollover bug: https://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover/12588#12588
47
- { running = 0 ;
48
- pulse = 1 ; // return 1 just one time
49
- }
50
- }
51
- return pulse;
52
- }
53
-
54
-
55
-
56
-
57
-
58
-
59
-
46
+ bool VirtualDelay::elapsed ()
47
+ {
48
+ if (running) {
49
+ // bug
50
+ // if((signed long)(*timerFunctionPtr)() >= timeOut)
51
+ // not the same as
52
+ if ((signed long )((*timerFunctionPtr)() - timeOut) >= 0 ) {
53
+ // fix rollover bug:
54
+ // https://arduino.stackexchange.com/questions/12587/how-can-i-handle-the-millis-rollover/12588#12588
55
+ running = 0 ;
56
+ return 1 ; // timer is elapsed
57
+ }
58
+ }
59
+ return 0 ; // still in delay timer
60
+ }
0 commit comments