-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstimer.cpp
50 lines (44 loc) · 1.11 KB
/
stimer.cpp
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
#include <Arduino.h>
#include "stimer.h"
STimer::STimer()
{
this->reset_ms = 0xFFFFFFFF;
}
void STimer::reset()
{
this->reset_ms = millis();
}
void STimer::reset_with_carry( unsigned long timeout_ms )
{
// this may overflow, but that is ok!
this->reset_ms = reset_ms + timeout_ms ;
}
bool STimer::check(unsigned long timeout)
{
unsigned long target_time = this->reset_ms + timeout;
unsigned long current_time = millis();
// has the current time overflown:
if ( current_time < this->reset_ms )
{
// did the target time overflowed
if ( this->reset_ms < target_time )
{ // no, it did not -> we are way over.
return true;
}
else
{ // yes its overflown as well, normal functionality.
return ( current_time >= target_time );
}
}
else
{ // timer has not overflown, how about the target?
if ( this->reset_ms < target_time )
{ // no overflow, here either. Normal business
return ( current_time >= target_time );
}
else
{ // the target is overflown, so must we.
return false;
}
}
}