HomeWaterLeaksDetection
PulseCounter.cpp
Go to the documentation of this file.
1
#include "
PulseCounter.h
"
2
3
#ifdef WEB_SERVER
4
String HTML_lastPulseTime(
const
PulseCounter
&
pulseCounter
);
5
String HTML_currentTime(
const
PulseCounter
&
pulseCounter
);
6
String HTML_currentDate(
const
PulseCounter
&
pulseCounter
);
7
#endif
8
9
PulseCounter::PulseCounter
(
int
inputPin) {
10
this->inputPin =
inputPin
;
11
12
// initialize the variables
13
lastPulseTime
= 0;
14
previousInputValue
= LOW;
15
pulseDown
= LOW;
16
17
#ifdef WEB_SERVER
18
// functions providing data to the webserver
19
htmlData
[12] = &
HTML_lastPulseTime
;
20
htmlData
[19] = &
HTML_currentTime
;
21
htmlData
[115] = &
HTML_currentDate
;
22
#endif
23
}
24
25
void
PulseCounter::update
() {
26
time_t
currentTime = millis();
27
int
currentInputValue = digitalRead(
inputPin
);
28
29
// if the signal has gone from HIGH to LOW, a pulse
30
// just occurred
31
if
(currentInputValue == LOW &&
previousInputValue
== HIGH) {
32
lastPulseTime
= currentTime;
33
pulseDown
= HIGH;
34
}
else
{
35
pulseDown
= LOW;
36
}
37
previousInputValue
= currentInputValue;
38
}
39
40
int
PulseCounter::isActive
()
const
{
41
return
pulseDown
;
42
}
43
44
time_t
PulseCounter::getLastPulseTime
()
const
{
45
return
lastPulseTime
;
46
}
47
48
#ifdef WEB_SERVER
49
const
String
PulseCounter::getHTMLData
(
const
int
id
)
const
{
50
auto
fce =
htmlData
.find(
id
);
51
if
(fce ==
htmlData
.end())
52
return
HTMLDataSource::UNDEFINED_DATA
;
53
return
fce->second(*
this
);
54
}
55
56
String HTML_lastPulseTime(
const
PulseCounter
&
pulseCounter
) {
57
time_t
difference = millis() -
pulseCounter
.
getLastPulseTime
();
58
return
FORMAT_TIME
(difference);
59
}
60
61
String HTML_currentTime(
const
PulseCounter
&
pulseCounter
) {
62
return
DateTime::getInstance
()->
getTimeStr
();
63
}
64
65
String HTML_currentDate(
const
PulseCounter
&
pulseCounter
) {
66
return
DateTime::getInstance
()->
getDateStr
();
67
}
68
#endif
PulseCounter::PulseCounter
PulseCounter(int inputPin)
Constructor of the class.
Definition:
PulseCounter.cpp:9
FORMAT_TIME
#define FORMAT_TIME(time)
Converts a time in milliseconds into a formatted string.
Definition:
DateTime.h:141
pulseCounter
PulseCounter pulseCounter(SENSOR_PIN)
PulseCounter::getLastPulseTime
time_t getLastPulseTime() const
Return the time when the last pulse was detected.
Definition:
PulseCounter.cpp:44
PulseCounter::inputPin
int inputPin
number of the input pin
Definition:
PulseCounter.h:32
PulseCounter::previousInputValue
int previousInputValue
previous state of the input pin
Definition:
PulseCounter.h:34
PulseCounter::update
void update()
Updates the class.
Definition:
PulseCounter.cpp:25
DateTime::getDateStr
String getDateStr() const
Returns the current day in a string format.
Definition:
DateTime.cpp:39
DateTime::getInstance
static DateTime * getInstance()
Returns the instance of the class.
Definition:
DateTime.cpp:26
HTMLDataSource::UNDEFINED_DATA
static const String UNDEFINED_DATA
string "UNDEFINED"
Definition:
HTMLDataSource.h:17
PulseCounter::HTML_lastPulseTime
friend String HTML_lastPulseTime(const PulseCounter &pulseCounter)
Associated function for returing the last pulse time.
PulseCounter::HTML_currentDate
friend String HTML_currentDate(const PulseCounter &pulseCounter)
Associated function for returing the current date.
time_t
unsigned long time_t
Referring to the data type unsigned long as time_t.
Definition:
DateTime.h:20
PulseCounter::lastPulseTime
time_t lastPulseTime
time when the last pulse was detected
Definition:
PulseCounter.h:33
PulseCounter::HTML_currentTime
friend String HTML_currentTime(const PulseCounter &pulseCounter)
Associated function for returing the current time.
PulseCounter
Definition:
PulseCounter.h:29
PulseCounter::isActive
int isActive() const
Returns information if a pulse has been detected.
Definition:
PulseCounter.cpp:40
PulseCounter.h
PulseCounter::pulseDown
int pulseDown
flag if a pulse just occurred
Definition:
PulseCounter.h:35
PulseCounter::getHTMLData
const String getHTMLData(const int id) const
Since this class is registered as a source of data for the HTML content, it needs return the appropri...
DateTime::getTimeStr
String getTimeStr() const
Returns the current time in a string format.
Definition:
DateTime.cpp:32
PulseCounter::htmlData
std::map< int, String(*)(const PulseCounter &pulseCounter)> htmlData
A map of different values (keys) and their associated functions which returns the appropriate values.
Definition:
PulseCounter.h:65
src
PulseCounter.cpp
Generated on Thu May 13 2021 08:42:38 for HomeWaterLeaksDetection by
1.8.18