-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSMBackDoor.cpp
executable file
·166 lines (137 loc) · 3.6 KB
/
SMBackDoor.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Module: Back Door lighting controller
*
* Controls the activation and timing of the back door sensor and light
* includes retrigger timing to keep light on when sensor keeps retriggering.
*
* (C) Sid Young 2014 */
#include "SMBackDoor.h"
#include "Controller.h"
/* -------- Constructors & Destructors ------------*/
SMBackDoor::SMBackDoor(Controller *pCtl)
{
Serial.println("SMBackDoor Constructor()");
init();
pThermalDetector1 = (Switch *) pCtl->FindInputDevice("TD1");
pThermalDetector2 = (Switch *) pCtl->FindInputDevice("TD2");
pThermalDetector3 = (Switch *) pCtl->FindInputDevice("TD3");
pFloodLight1 = (Relay *) pCtl->FindOutputDevice("FL1");
pFloodLight2 = (Relay *) pCtl->FindOutputDevice("FL2");
pGardenLights= (Relay *) pCtl->FindOutputDevice("GL1");
pBackDeckLight= (Relay *) pCtl->FindOutputDevice("BD1");
}
SMBackDoor::~SMBackDoor() {}
/*----------------------------------------
*
* init()
*
* setup the default values for everything
*/
void SMBackDoor::init()
{
Serial.println("SMBackDoor::init()");
_on_timer=0;
_off_timer=0;
_retrigger_timer=0;
_retrig_value=DEF_RETRIGGER_TIME;
_on_timer_value=DEF_ON_TIMER_TIME;
this->setState(OFF_STATE);
}
/*----------------------------------------
*
* run()
*
* Called constantly from controller's run() method
* apply logic when timers and state are appropriate.
*/
void SMBackDoor::run()
{
switch(this->getState())
{
// if we are in OFF state and input is HIGH,
// set ON_STATE true and turn ON_TIMER to MAX
// and retrigger to 0
case OFF_STATE:
this->pBackDeckLight->OFF();
if(pThermalDetector1->getState()==1)
{
Serial.println("Transition to ON state");
this->setState(ON_STATE);
this->_on_timer = this->_on_timer_value;
_retrigger_timer = 0;
}
break;
// if we are in ON state and INPUT goes low (switch off)
// and "on" timer >0 exit
case ON_STATE:
pBackDeckLight->ON();
if((pThermalDetector1->getState()== 0) && (_on_timer>0)) return;
// if we are in ON state and
// INPUT goes low and
// timer == 0 and
// retrigger ==0
// then start retrigger timer and
// set RETRIGGER STATE and
// turn light OFF
if((pThermalDetector1->getState()== 0) && (_on_timer==0) && (_retrigger_timer==0))
{
_retrigger_timer = _retrig_value;
Serial.println("Transition to RETRIGGER state");
this->setState(RETRIGGER);
return;
}
break;
// if we are in RETRIGGER and input = 0 and retrigger timer == 0 then OFF_STATE
case RETRIGGER:
if((_retrigger_timer==0)&&(pThermalDetector1->getState()== 0))
{
Serial.println("Transition to OFF state");
init();
return;
}
// if we are in RETRIGGER and sensor goes on then
// switch on (but for longer).
if((_retrigger_timer>0)&&(pThermalDetector1->getState()== 1))
{
_retrigger_timer = 0;
_on_timer_value +=60;
Serial.println("Transition to ON state - for longer");
this->setState(ON_STATE);
return;
}
break;
default:
Serial.println("Unknown RUN state");
this->setState(OFF_STATE);
break;
}
}
/*----------------------------------------
*
* Timer1ms()
*
* Called during a 1 ms timer hook from controller
*
*/
void SMBackDoor::Timer1ms() {}
/*----------------------------------------
*
* Timer1sec()
*
* Called during a 1 second timer hook from controller
*/
void SMBackDoor::Timer1sec()
{
if(this->getState()==OFF_STATE)
this->_off_timer++;
else
this->_off_timer=0;
if(this->_on_timer>0)
this->_on_timer--;
if(this->_retrigger_timer>0)
this->_retrigger_timer--;
}
int SMBackDoor::getState() { return this->_c_state;}
/*
* end of file
*/