forked from DanielOgorchock/ST_Anything
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEX_Alarm.cpp
More file actions
191 lines (170 loc) · 5.33 KB
/
EX_Alarm.cpp
File metadata and controls
191 lines (170 loc) · 5.33 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//******************************************************************************************
// File: EX_Alarm.h
// Authors: Dan G Ogorchock & Daniel J Ogorchock (Father and Son)
//
// Summary: EX_Alarm is a class which implements the SmartThings "Alarm" device capability.
// It inherits from the st::PollingSensor class. The current version implements the Siren as
// a digital output (uses a relay to switch the Siren on or off)
//
// TODO: Possibly add Strobe capability in the future
//
// Create an instance of this class in your sketch's global variable section
// For Example: st::EX_Alarm executor2("alarm1", PIN_ALARM, LOW, true, PIN_STROBE);
//
// st::EX_Alarm() constructor requires the following arguments
// - String &name - REQUIRED - the name of the object - must match the Groovy ST_Anything DeviceType tile name
// - byte pin - REQUIRED - the Arduino Pin to be used as a digital output
// - bool startingState - OPTIONAL - the value desired for the initial state of the switch. LOW = "off", HIGH = "on"
// - bool invertLogic - OPTIONAL - determines whether the Arduino Digital Output should use inverted logic
// - byte pinStrobe - OPTOINAL - If supplied, will allow separate SIREN and STROBE outputs
//
// Change History:
//
// Date Who What
// ---- --- ----
// 2015-01-03 Dan & Daniel Original Creation
// 2017-04-20 Dan Ogorchock Add optional Strobe functionality
// 2017-04-26 Dan Ogorchock Improved Logic if Strobe pin not defined
// 2018-08-30 Dan Ogorchock Modified comment section above to comply with new Parent/Child Device Handler requirements
//
//
//******************************************************************************************
#include "EX_Alarm.h"
#include "Constants.h"
#include "Everything.h"
namespace st
{
//private
void EX_Alarm::writeStateToPin()
{
if (m_nCurrentAlarmState == both) {
digitalWrite(m_nPin, m_bInvertLogic ? LOW : HIGH);
if (m_bUseStrobe) { digitalWrite(m_nPinStrobe, m_bInvertLogic ? LOW : HIGH); }
}
else if(m_nCurrentAlarmState == siren) {
digitalWrite(m_nPin, m_bInvertLogic ? LOW : HIGH);
if (m_bUseStrobe) { digitalWrite(m_nPinStrobe, m_bInvertLogic ? HIGH : LOW); }
}
else if(m_nCurrentAlarmState == strobe) {
digitalWrite(m_nPin, m_bInvertLogic ? HIGH : LOW);
if (m_bUseStrobe) { digitalWrite(m_nPinStrobe, m_bInvertLogic ? LOW : HIGH); }
}
else if(m_nCurrentAlarmState == off) {
digitalWrite(m_nPin, m_bInvertLogic ? HIGH : LOW);
if (m_bUseStrobe) { digitalWrite(m_nPinStrobe, m_bInvertLogic ? HIGH : LOW); }
}
}
//public
//constructor
EX_Alarm::EX_Alarm(const __FlashStringHelper *name, byte pin, bool startingState, bool invertLogic, byte pinStrobe) :
Executor(name),
m_nPin(pin),
m_bInvertLogic(invertLogic),
m_nPinStrobe(pinStrobe)
{
m_nCurrentAlarmState = off;
m_bUseStrobe = false;
setPin(pin);
//If user has supplied a pin for Strobe Light
if (pinStrobe < 255) {
m_bUseStrobe = true;
setPin(pinStrobe);
}
//else
//{
// m_bUseStrobe = false;
//}
}
//destructor
EX_Alarm::~EX_Alarm()
{
}
//initialization routine
void EX_Alarm::init()
{
refresh(); //update ST Cloud with initial data
}
//called periodically to ensure state of the Alarm Siren is up to date in the SmartThings Cloud (in case an event is missed)
void EX_Alarm::refresh()
{
//if (debug) {
// Serial.print(F("EX_Alarm:: UseStrobe = "));
// Serial.println(m_bUseStrobe);
//}
if (m_nCurrentAlarmState == both) {
Everything::sendSmartString(getName() + F(" both"));
}
else if(m_nCurrentAlarmState == siren) {
Everything::sendSmartString(getName() + F(" siren"));
}
else if(m_nCurrentAlarmState == strobe) {
Everything::sendSmartString(getName() + F(" strobe"));
}
else if(m_nCurrentAlarmState == off) {
//else {
Everything::sendSmartString(getName() + F(" off"));
}
}
//SmartThings Shield data handler (receives command to turn "both" or "off" the Alarm (digital output)
void EX_Alarm::beSmart(const String &str)
{
String s = str.substring(str.indexOf(' ') + 1);
if (debug) {
Serial.print(F("EX_Alarm::beSmart s = "));
Serial.println(s);
}
//if (m_bUseStrobe) {
if (s == F("both")) {
if (m_bUseStrobe) {
m_nCurrentAlarmState = both;
}
else {
m_nCurrentAlarmState = siren;
if (debug) {
Serial.println(F("EX_Alarm::beSmart - Strobe Pin not defined. Defaulting to Siren!"));
}
}
}
else if(s == F("siren")) {
m_nCurrentAlarmState = siren;
}
else if(s == F("strobe")) {
if (m_bUseStrobe) {
m_nCurrentAlarmState = strobe;
}
else {
m_nCurrentAlarmState = siren;
if (debug) {
Serial.println(F("EX_Alarm::beSmart - Strobe Pin not defined. Defaulting to Siren!"));
}
}
}
else if(s == F("off")) {
m_nCurrentAlarmState = off;
}
//}
//else {
// if (s == F("both") {
// m_nCurrentAlarmState = both;
// }
// else if(s == F("siren")) {
// m_nCurrentAlarmState = siren;
// }
// else if(s == F("strobe")) {
// m_nCurrentAlarmState = siren;
// }
// else if(s == F("off")) {
// m_nCurrentAlarmState = off;
// }
//}
writeStateToPin();
refresh();
}
void EX_Alarm::setPin(byte pin)
{
//m_nPin = pin;
digitalWrite(pin, m_bInvertLogic ? HIGH : LOW);
pinMode(pin, OUTPUT);
writeStateToPin();
}
}