-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwitchNode.cpp
More file actions
51 lines (41 loc) · 1 KB
/
SwitchNode.cpp
File metadata and controls
51 lines (41 loc) · 1 KB
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 "SwitchNode.hpp"
SwitchNode::SwitchNode(const char* name)
:
m_name(name),
m_lastSwitchState(-1)
{
m_switchStateNode = new HomieNode(name, "status");
}
void SwitchNode::begin(long pin, long debounceInterval)
{
pinMode(pin, INPUT_PULLUP);
m_switch.attach(pin);
m_switch.interval(debounceInterval);
}
void SwitchNode::setup()
{
m_switchStateNode->setProperty("status").send("unknown");
}
void SwitchNode::loop()
{
m_switch.update();
int currentSwitchState = m_switch.read();
if(currentSwitchState != m_lastSwitchState)
{
if(HIGH == currentSwitchState)
{
Homie.getLogger() << m_name << ": Status is open" << endl;
m_switchStateNode->setProperty("status").send("open");
}
else
{
Homie.getLogger() << m_name << ": Status is closed" << endl;
m_switchStateNode->setProperty("status").send("closed");
}
m_lastSwitchState = currentSwitchState;
}
}
bool SwitchNode::isPressed() const
{
return (LOW == m_lastSwitchState) ? true : false;
}