-
Notifications
You must be signed in to change notification settings - Fork 0
/
Switch.cpp
237 lines (195 loc) · 5.14 KB
/
Switch.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* connectingStuff, Arduino Based Home Automation
* http://connectingstuff.net/blog/
*
* Copyright (C) 2012 [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "Switch.h"
#include "Kernel.h"
#ifdef XPL
extern xPL xpl;
#endif
Switch::Switch()
{
m_iLongPressDelay = DEFAULT_LONG_PRESS_DELAY;
m_type = SWITCH;
}
Switch::Switch(int _io, char* _name, int _maxModuleToManage, int _longPressDelay)
{
Init(_io, _name, _maxModuleToManage, _longPressDelay);
}
Switch::~Switch()
{
}
void Switch::Init(int _io, char* _name, int _maxModuleToManage, int _longPressDelay)
{
m_Input.Init(_io);
m_type = SWITCH;
m_iID = ConnectingStuff::GetCARDID()*100 + _io;
memcpy(m_name, _name, 8);
m_iNbClic = 0;
m_bLongPress = false;
//m_Input.SetInternalPullupResistor();
m_oAttachedModule = (ConfigModule*)malloc ( _maxModuleToManage * sizeof(ConfigModule) );
m_iAttachedModuleIndex = 0;
m_bAsConfigOnMoreThan1Clic = false;
}
/// Lie une sortie a une entree
/// nbClic : nb de "clic"
/// time : duree
/// onOff : la sortie doit etre activer, desactiver ou les deux
void Switch::LinkModule(Module* _module, uint8_t _nbClic, int _time, Statut _onOff)
{
ConfigModule config;
config.nbClic = _nbClic;
config.time = _time;
config.onOff = _onOff;
config.emitter = this;
config.receiver = _module;
m_oAttachedModule[m_iAttachedModuleIndex++] = config;
if(_nbClic > 1)
m_bAsConfigOnMoreThan1Clic = true;
}
SwitchState Switch::GetState()
{
SwitchState state;
if(m_Input.GetState() == HIGH) state = RELEASE;
else
{
if(m_bLongPress) state = LONGPRESS;
else state = PRESS;
char str[50];
sprintf(str,"Switch%d:%d", m_iID, state);
ConnectingStuff::Debug(str);
}
return state;
}
int Switch::GetLongPressTime()
{
if(m_bLongPress)
{
return (int)m_PressTimer.GetTotalTime();
}
else return 0;
}
uint8_t Switch::GetNbClic()
{
return m_iNbClic;
}
void Switch::Update()
{
// Recupre l'état de l'io
m_Input.Read();
// Un changement d'état est détecté
if(m_Input.StateChange())
{
// Un appuis sur l'interrupteur est détecté : on démarre le timer de pression
if(m_Input.GetState() == LOW)
{
m_PressTimer.Reset();
m_iNbClic = 0;
}
// l'interrupteur vient d'etre relaché
else
{
// Si un appuis long vient d'etre relaché
if(m_bLongPress == true)
{
// TODO Traitement appuis long
m_bLongPress = false;
}
// Sinon, on incrémente le nombre de pressions de l'interrupteur
// et on met a jour le timer du dernier relaché
else
{
SendStatus();
++m_iNbClic;
m_ReleaseTimer.Reset();
}
}
}
else
{
// L'interrupteur est en position relaché et
// le temps max entre deux appuis/relaché est écoulé (DEFAULT_LONG_PRESS_DELAY est la duree max entre deux appuis/relaché)
if(m_Input.GetState() == HIGH && m_ReleaseTimer.GetTotalTime() > DEFAULT_LONG_PRESS_DELAY)
{
// Un ou plusieurs appuis court on été détecté
if(m_iNbClic > 0)
{
// Traitement appuis court
for(uint8_t i = 0; i < m_iAttachedModuleIndex; i++)
{
//Serial.print(m_iNbClic);
//Serial.print(",");
//Serial.println(m_oAttachedModule[i].nbClic);
if(m_oAttachedModule[i].nbClic > 0 && m_iNbClic == m_oAttachedModule[i].nbClic)
{
RunModuleAction(m_oAttachedModule[i]);
}
}
}
m_iNbClic = 0;
}
// L'interrupteur est en position appuyé
if(m_Input.GetState() == LOW)
{
// Un appuis long est détecté
if(m_PressTimer.GetTotalTime() > DEFAULT_LONG_PRESS_DELAY)
{
m_bLongPress = true;
// Traitement appuis long
for(uint8_t i = 0; i < m_iAttachedModuleIndex; i++)
{
if(m_oAttachedModule[i].time > 0 && m_PressTimer.GetTotalTime() > m_oAttachedModule[i].time)
{
RunModuleAction(m_oAttachedModule[i]);
m_PressTimer.Reset();
}
}
}
}
}
GetState();
}
void Switch::RunModuleAction(ConfigModule _config)
{
_config.receiver->Action(&_config);
}
void Switch::Action(void*){}
char* Switch::ToJson()
{
char str[150];
sprintf(str,"{\"type\":\"switch\",\"id\":\"%d\",\"name\":\"%s\",\"pin\":\"%d\",\"value\":\"%d\"}", m_iID, m_name, m_iID, (int)GetState());
return str;
}
void Switch::SendStatus()
{
#ifdef XPL
xPL_Message msg;
msg.hop = 1;
msg.type = XPL_TRIG;
msg.SetTarget_P(PSTR("*"));
msg.SetSchema_P(PSTR("sensor"), PSTR("basic"));
char id[20];
sprintf(id,"%d",m_iID);
msg.AddCommand("device",id);
msg.AddCommand_P(PSTR("type"),PSTR("input"));
msg.AddCommand_P(PSTR("current"),PSTR("pulse"));
xpl.SendMessage(&msg);
#endif
}