forked from DanielOgorchock/ST_Anything
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterruptSensor.cpp
More file actions
122 lines (109 loc) · 3.25 KB
/
InterruptSensor.cpp
File metadata and controls
122 lines (109 loc) · 3.25 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
//******************************************************************************************
// File: InterruptSensor.cpp
// Authors: Dan G Ogorchock & Daniel J Ogorchock (Father and Son)
//
// Summary: st::InterruptSensor is a generic class which inherits from st::Sensor. This is the
// parent class for the st::IS_Motion, IS_Contact, and IS_DoorControl classes.
// In general, this file should not need to be modified.
//
// Change History:
//
// Date Who What
// ---- --- ----
// 2015-01-03 Dan & Daniel Original Creation
// 2015-03-17 Dan Added optional "numReqCounts" constructor argument/capability
//
//
//******************************************************************************************
#include "InterruptSensor.h"
#include "Constants.h"
#include "Everything.h"
namespace st
{
//private
//Checks to see if the pin has changed state. If so calls appropriate function.
void InterruptSensor::checkIfTriggered()
{
if (digitalRead(m_nInterruptPin) == m_bInterruptState && !m_bStatus) //new interrupt
{
m_nCurrentDownCount = m_nRequiredCounts;
m_nCurrentUpCount++;
if (m_nCurrentUpCount >= m_nRequiredCounts)
{
m_bStatus = true;
m_bInitRequired = false;
runInterrupt();
}
}
else if ((digitalRead(m_nInterruptPin) != m_bInterruptState && m_bStatus) || m_bInitRequired) //interrupt has ended OR Init called us
{
m_nCurrentUpCount = 0;
m_nCurrentDownCount--;
if (m_nCurrentDownCount <= 0)
{
m_bStatus = false;
m_bInitRequired = false;
runInterruptEnded();
}
}
}
//public
//constructor
InterruptSensor::InterruptSensor(const __FlashStringHelper *name, byte pin, bool iState, bool pullup, long numReqCounts) :
Sensor(name),
m_bInterruptState(iState),
m_bStatus(false),
m_bPullup(pullup),
m_bInitRequired(true),
m_nRequiredCounts(numReqCounts),
m_nCurrentUpCount(0),
m_nCurrentDownCount(numReqCounts)
{
setInterruptPin(pin);
}
//destructor
InterruptSensor::~InterruptSensor()
{
}
//initialization function
void InterruptSensor::init()
{
checkIfTriggered();
}
//update function
void InterruptSensor::update()
{
checkIfTriggered();
}
//handles start of an interrupt - all derived classes should implement this virtual function
void InterruptSensor::runInterrupt()
{
if(debug)
{
Everything::sendSmartString(getName()+F(" triggered ") + (m_bInterruptState?F("HIGH"):F("LOW)")));
}
}
//handles the end of an interrupt - all derived classes should implement this virtual function
void InterruptSensor::runInterruptEnded()
{
if(debug)
{
Everything::sendSmartString(getName()+F(" ended ") + (m_bInterruptState?F("LOW)"):F("HIGH)")));
}
}
//sets the pin to be monitored, and set the Arduino pinMode based on constructor data
void InterruptSensor::setInterruptPin(byte pin)
{
m_nInterruptPin=pin;
if(!m_bPullup)
{
pinMode(m_nInterruptPin, INPUT);
}
else
{
pinMode(m_nInterruptPin, INPUT_PULLUP);
}
}
//debug flag to determine if debug print statements are executed (set value in your sketch)
bool InterruptSensor::debug=false;
}