-
Notifications
You must be signed in to change notification settings - Fork 0
/
Light.cpp
155 lines (128 loc) · 2.87 KB
/
Light.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
/*
* 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 "Light.h"
#include "Kernel.h"
#ifdef XPL
extern xPL xpl;
#endif
OnOff::OnOff()
{
m_type = ONOFFM;
}
OnOff::OnOff(uint8_t _card, uint8_t _io, char* _name)
{
m_output.Init(_card, _io);
m_type = ONOFFM;
m_iID = m_output.GetID();
strcpy(m_name, _name);
}
OnOff::~OnOff()
{
}
Light::Light()
{
m_type = LIGHT;
}
Light::Light(uint8_t _card, uint8_t _io, char* _name)
{
m_output.Init(_card, _io);
m_type = LIGHT;
m_iID = m_output.GetID();
strcpy(m_name, _name);
}
Light::~Light()
{
}
void Light::SetAutoOff(int _time)
{
m_iAutoOffTime = _time;
m_OffTimer.Init();
}
void Light::Update()
{
if(m_OffTimer.GetTotalTime() > m_iAutoOffTime)
{
Set(LOW);
}
}
void Light::Action(void* _arg)
{
ConfigModule* config = (ConfigModule*)_arg;
switch(config->onOff)
{
case ONOFF:
Toogle();
break;
case OFF:
Set(LOW);
break;
case ON:
Set(HIGH);
break;
default:
break;
}
if(m_output.Read() > 0)
{
m_OffTimer.Reset();
}
}
void Light::Toogle()
{
m_output.Toogle();
SendStatus();
char str[50];
sprintf(str,"Light%d:%d", m_iID, (int)m_output.Read());
ConnectingStuff::Debug(str);
}
void Light::Set(uint8_t _state)
{
m_output.Set(_state);
SendStatus();
char str[50];
sprintf(str,"Light%d:%d", m_iID, (int)m_output.Read());
ConnectingStuff::Debug(str);
}
char* Light::ToJson()
{
char str[150];
sprintf(str,"{\"type\":\"light\",\"id\":\"%d\",\"name\":\"%s\",\"pin\":\"%d\",\"value\":\"%d\"}", m_iID, m_name, m_iID, (int)m_output.Read());
return str;
}
void Light::SendStatus()
{
#ifdef XPL
xPL_Message msg;
msg.hop = 1;
msg.type = XPL_TRIG;
msg.SetTarget_P(PSTR("*"));
msg.SetSchema_P(PSTR("lighting"), PSTR("device"));
msg.AddCommand_P(PSTR("network"),PSTR("1"));
msg.AddCommand_P(PSTR("device"),PSTR("11"));
msg.AddCommand_P(PSTR("channel"),PSTR("1"));
char buffer[20];
memcpy(buffer, m_output.Read() == 0 ? "off" : "on", 3);
msg.AddCommand("state",buffer);
sprintf(buffer, "%d", m_output.Read());
msg.AddCommand("level",buffer);
xpl.SendMessage(&msg);
#endif
}