-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSensorLogic.cpp
151 lines (123 loc) · 4.2 KB
/
SensorLogic.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <Arduino.h>
#include "configuration.h"
#include "ILogic.h"
#include "BasicLogic.h"
#include "SensorLogic.h"
#include "pgmString.h"
SensorLogic::SensorLogic(const Configuration::SupportedDevices _deviceId) :
BasicLogic(_deviceId),
m_analogInputPin(0),
m_lowThreshold(0),
m_highThreshold(0),
m_thresholdStatus(E_THRESHOLD_OFF) {}
SensorLogic::~SensorLogic() {}
bool SensorLogic::initializeInstnace(const byte _param1, const byte _param2, const Configuration::PrivateData& _privateData) {
bool _bReturnValue = false;
m_analogInputPin = getInputPin(_param1);
if (m_analogInputPin) {
pinMode(m_analogInputPin, INPUT);
m_lowThreshold = _privateData.m_privateData[0] * 256;
m_lowThreshold += _privateData.m_privateData[1];
m_highThreshold = _privateData.m_privateData[2] * 256;
m_highThreshold += _privateData.m_privateData[3];
_bReturnValue = true;
}
return _bReturnValue;
}
void SensorLogic::getPrivateData(Configuration::PrivateData& _privateData) {
_privateData.m_privateData[0] = m_lowThreshold / 256;
_privateData.m_privateData[1] = m_lowThreshold % 256;
_privateData.m_privateData[2]= m_highThreshold / 256;
_privateData.m_privateData[3]= m_highThreshold % 256;
}
ILogic::LogicAction SensorLogic::action(const byte _action, const byte _param, String& _sResponse) {
LogicAction _laReturnValue = E_ACTION_RESPONSE;
switch (_action) {
case 0: {
// Read sensor value
_sResponse += getPgmString(STR_SENSOR__STATE);
char _tmpBuffer[16];
sprintf(_tmpBuffer, "%d", analogRead(m_analogInputPin));
_sResponse += _tmpBuffer;
break;
}
case 1: {
// Display thresholds
_sResponse += getPgmString(STR_SENSOR__STATE);
char _tmpBuffer[32];
sprintf(_tmpBuffer, "L(%d) H(%d) S(%d)", m_lowThreshold, m_highThreshold, m_thresholdStatus);
_sResponse += _tmpBuffer;
break;
}
case 2: {
// Set active threshold
_sResponse += getPgmString(STR_SENSOR__SET_ACTIVE_THRESHOLD);
if (_param) {
// Activate threshold notification
// set the correct pending threshold notification
m_thresholdStatus = calculateCurrentStatus();
_sResponse += getPgmString(STR_ON);
} else {
// Disable threshold notification
m_thresholdStatus = E_THRESHOLD_OFF;
_sResponse += getPgmString(STR_OFF);
}
break;
}
case 10 ... 13: {
// Set lower threshold
m_lowThreshold = (_action % 10) * 256 + _param;
_sResponse += getPgmString(STR_SENSOR__SET_THRESHOLD);
char _tmpBuffer[16];
sprintf(_tmpBuffer, "L(%d)", m_lowThreshold);
_sResponse += _tmpBuffer;
break;
}
case 20 ... 23: {
// Set lower threshold
m_highThreshold = (_action % 10) * 256 + _param;
_sResponse += getPgmString(STR_SENSOR__SET_THRESHOLD);
char _tmpBuffer[16];
sprintf(_tmpBuffer, "H(%d)", m_highThreshold);
_sResponse += _tmpBuffer;
break;
}
default:
// Read switch state
_sResponse += getPgmString(STR_INVALID_ACTION);
}
return _laReturnValue;
}
ILogic::LogicAction SensorLogic::tick(String& _sResponse) {
LogicAction _eReturnValue = E_ACTION_OK;
switch (m_thresholdStatus) {
case E_THRESHOLD_OFF: {
break;
}
case E_THRESHOLD_LOW: {
if (analogRead(m_analogInputPin) <= m_lowThreshold) {
_sResponse += getPgmString(STR_LOW);
m_thresholdStatus = E_THRESHOLD_HIGH;
_eReturnValue = E_ACTION_RESPONSE;
}
break;
}
case E_THRESHOLD_HIGH: {
if (analogRead(m_analogInputPin) >= m_highThreshold) {
_sResponse += getPgmString(STR_HIGH);
m_thresholdStatus = E_THRESHOLD_LOW;
_eReturnValue = E_ACTION_RESPONSE;
}
break;
}
}
return _eReturnValue;
}
SensorLogic::E_THRESHOLD SensorLogic::calculateCurrentStatus() {
E_THRESHOLD _thresholdStatus = E_THRESHOLD_OFF;
if (analogRead(m_analogInputPin) < ((m_highThreshold + m_lowThreshold) / 2))
_thresholdStatus = E_THRESHOLD_HIGH;
else
_thresholdStatus = E_THRESHOLD_LOW;
return _thresholdStatus;
}