forked from DanielOgorchock/ST_Anything
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPollingSensor.cpp
More file actions
113 lines (97 loc) · 2.62 KB
/
PollingSensor.cpp
File metadata and controls
113 lines (97 loc) · 2.62 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
//******************************************************************************************
// File: PollingSensor.cpp
// Authors: Dan G Ogorchock & Daniel J Ogorchock (Father and Son)
//
// Summary: st::PollingSensor is a generic class which inherits from st::Sensor. This is the
// parent class for the st::PS_Illuminace, st::PS_Water, and PS_TemperatureHumidity classes.
//
// In general, this file should not need to be modified.
//
// st::PollingSensor() constructor requires the following arguments
// - String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name
// - long interval - REQUIRED - the polling interval in seconds
// - long offset - REQUIRED - the polling interval offset in seconds - used to prevent all polling sensors from executing at the same time
//
// Change History:
//
// Date Who What
// ---- --- ----
// 2015-01-03 Dan & Daniel Original Creation
//
//
//******************************************************************************************
#include "PollingSensor.h"
#include "Constants.h"
#include "Everything.h"
namespace st
{
//private
bool PollingSensor::checkInterval()
{
//check for time overflow
if(millis()<m_nPreviousTime)
{
if (debug)
{
Serial.println(F("PollingSensor: millis() Overflow handled"));
}
m_nPreviousTime = 0;
}
if(m_nPreviousTime==0) //eliminates problem of there being a delay before first update() call
{
m_nPreviousTime=millis();
}
//calculate new delta time
m_nDeltaTime+=(millis()-m_nPreviousTime)-m_nOffset;
m_nOffset=0;
m_nPreviousTime=millis();
//determine interval has passed
if(m_nDeltaTime>=m_nInterval)
{
m_nDeltaTime=0;
return true;
}
else
{
return false;
}
}
//public
//constructor
PollingSensor::PollingSensor(const __FlashStringHelper *name, long interval, long offset):
Sensor(name),
m_nPreviousTime(0),
m_nDeltaTime(0),
m_nInterval(interval*1000),
m_nOffset(offset*1000)
{
}
//destructor
PollingSensor::~PollingSensor()
{
}
void PollingSensor::init()
{
getData();
}
void PollingSensor::refresh()
{
getData();
}
void PollingSensor::update()
{
if(checkInterval())
{
getData();
}
}
void PollingSensor::getData()
{
if(debug)
{
Everything::sendSmartString(getName() + F(" triggered"));
}
}
//debug flag to determine if debug print statements are executed (set value in your sketch)
bool PollingSensor::debug=false;
}