|
1 |
| -/* |
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 . |
7 |
| -
|
8 |
| -AUTHOR: Albert van Dalen |
9 |
| -WEBSITE: http://www.avdweb.nl/arduino/libraries/virtualdelay.html |
10 |
| -
|
11 |
| -HISTORY: |
12 |
| -1.0.0 10-1-2016 |
13 |
| -1.0.1 6-9-2017 elapsed(), added start() |
14 |
| -1.0.2 19-9-2017 DO_ONCE without class |
15 |
| -1.0.3 / 19-9-2017 fix rollover bug |
16 |
| -
|
17 |
| -start _____|_____________________ |
18 |
| - __________ |
19 |
| -running _____| |__________ |
20 |
| -
|
21 |
| -elapsed() ________________|__________ |
22 |
| -
|
23 |
| -set timeOut _____|_____________________ |
24 |
| -
|
25 |
| -*/ |
26 |
| - |
27 |
| -#include <Arduino.h> |
28 |
| -#include "avdweb_VirtualDelay.h" |
29 |
| - |
30 |
| -VirtualDelay::VirtualDelay(unsigned long (*timerFunctionPtr)()): |
31 |
| -timerFunctionPtr(timerFunctionPtr) |
32 |
| -{ |
33 |
| -} |
34 |
| - |
35 |
| -void VirtualDelay::start(signed long delay) // 0...2^31 |
36 |
| -{ if(!running) |
37 |
| - { running = 1; |
38 |
| - timeOut = (*timerFunctionPtr)() + abs(delay); |
39 |
| - } |
40 |
| -} |
41 |
| - |
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 |
| - |
| 1 | +/* |
| 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 . |
| 7 | +
|
| 8 | +AUTHOR: Albert van Dalen |
| 9 | +WEBSITE: http://www.avdweb.nl/arduino/libraries/virtualdelay.html |
| 10 | +
|
| 11 | +HISTORY: |
| 12 | +1.0.0 10-1-2016 |
| 13 | +1.0.1 6-9-2017 elapsed(), added start() |
| 14 | +1.0.2 19-9-2017 DO_ONCE without class |
| 15 | +1.0.3 / 19-9-2017 fix rollover bug |
| 16 | +
|
| 17 | +start _____|_____________________ |
| 18 | + __________ |
| 19 | +running _____| |__________ |
| 20 | +
|
| 21 | +elapsed() ________________|__________ |
| 22 | +
|
| 23 | +set timeOut _____|_____________________ |
| 24 | +
|
| 25 | +*/ |
| 26 | + |
| 27 | +#include <Arduino.h> |
| 28 | +#include "avdweb_VirtualDelay.h" |
| 29 | + |
| 30 | +VirtualDelay::VirtualDelay(unsigned long (*timerFunctionPtr)()): |
| 31 | +timerFunctionPtr(timerFunctionPtr) |
| 32 | +{ |
| 33 | +} |
| 34 | + |
| 35 | +void VirtualDelay::start(signed long delay) // 0...2^31 |
| 36 | +{ if(!running) |
| 37 | + { running = 1; |
| 38 | + timeOut = (*timerFunctionPtr)() + abs(delay); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 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 | 59 |
|
0 commit comments