-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicroSwitch.cpp
70 lines (59 loc) · 1.17 KB
/
MicroSwitch.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "MicroSwitch.h"
#define DEBOUNCE_TIME 50000
MicroSwitch::MicroSwitch(int pinId) : DigitalInput(pinId), m_debouncePending(false), m_lastUpdate(0), m_currentState(HIGH), m_pendingState(HIGH)
{
}
void MicroSwitch::Update(long updateInterval)
{
if (m_debouncePending)
{
m_lastUpdate += updateInterval;
}
else
{
m_lastUpdate = 0;
}
if (m_debouncePending){
if (m_lastUpdate >= DEBOUNCE_TIME){
m_debouncePending = false;
if (m_pendingState == DigitalInput::GetState())
{
m_currentState = m_pendingState;
if (m_currentState == HIGH)
{
m_pressed = true;
}
else
{
m_pressed = false;
}
}
else
{
m_pendingState = m_currentState;
m_pressed = false;
}
}
}
int state = DigitalInput::GetState();
if (state != m_pendingState){
if (!m_debouncePending){
m_debouncePending = true;
m_pendingState = state;
}
}
};
void MicroSwitch::Reset()
{
m_currentState = m_pendingState = DigitalInput::GetState();
};
int MicroSwitch::GetState()
{
return m_currentState;
}
bool MicroSwitch::JustPressed()
{
bool pressed = m_pressed;
m_pressed = false;
return pressed;
}