-
Notifications
You must be signed in to change notification settings - Fork 0
/
StateToggleTimer.h
67 lines (57 loc) · 1.5 KB
/
StateToggleTimer.h
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Manage a toggle state timer, for example flashing of an LED or a flashing indicator on a display
#ifndef _STATE_TOGGLE_TIMER
#define _STATE_TOGGLE_TIMER
class StateToggleTimer
{
public:
StateToggleTimer(const unsigned long duration)
{
m_bIsOn = false;
m_bPreviousState = false;
m_bHasStarted = false;
m_duration = duration;
m_timer = millis();
}
inline bool IsOn() { return m_bIsOn; }
inline bool HasStarted() { return m_bHasStarted; }
inline void SetDuration(const unsigned long duration) { m_duration = duration; }
void Start()
{
m_bIsOn = true;
m_bHasStarted = true;
m_timer = millis();
}
void Stop()
{
m_bIsOn = true;
m_bHasStarted = false;
}
void Update(const unsigned long now)
{
if (m_bHasStarted)
{
if ((now - m_timer) > m_duration)
{
m_timer = now;
m_bIsOn = !m_bIsOn;
}
}
else
{
m_bIsOn = true;
}
}
bool HasStateChanged()
{
bool bChanged = !(m_bPreviousState == m_bIsOn);
m_bPreviousState = m_bIsOn;
return bChanged;
}
private:
bool m_bIsOn; // Flashing changes this state between on/off
bool m_bPreviousState; // Keep track of the previous state
bool m_bHasStarted; // Is the flash system enabled?
unsigned long m_duration; // Duration of a flash
unsigned long m_timer; // Timer to control the flash
};
#endif