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
| /*
Copyright (C) 2013 CurlyMo & *geanonimiseerd*
This file is part of pilight.
pilight 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 3 of the License, or (at your option) any later
version.
pilight 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 pilight. If not, see <http://www.gnu.org/licenses/>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "settings.h"
#include "log.h"
#include "protocol.h"
#include "binary.h"
#include "impuls.h"
void impulsCreateMessage(int id, int unit, int state) {
impuls.message = json_mkobject();
json_append_member(impuls.message, "id", json_mknumber(id));
json_append_member(impuls.message, "unit", json_mknumber(unit));
if(state == 1)
json_append_member(impuls.message, "state", json_mkstring("on"));
else
json_append_member(impuls.message, "state", json_mkstring("off"));
}
void impulsParseBinary(void) {
int unit = binToDec(impuls.binary, 0, 4);
int check = impuls.binary[10];
int state = impuls.binary[11];
int id = binToDec(impuls.binary, 5, 9);
if(check != state)
impulsCreateMessage(id, unit, state);
}
void impulsCreateLow(int s, int e) {
int i;
for(i=s;i<=e;i+=4) {
impuls.raw[i]=(PULSE_LENGTH);
impuls.raw[i+1]=(impuls.pulse*PULSE_LENGTH);
impuls.raw[i+2]=(impuls.pulse*PULSE_LENGTH);
impuls.raw[i+3]=(PULSE_LENGTH);
}
}
void impulsCreateMed(int s, int e) {
int i;
for(i=s;i<=e;i+=4) {
impuls.raw[i]=(impuls.pulse*PULSE_LENGTH);
impuls.raw[i+1]=(PULSE_LENGTH);
impuls.raw[i+2]=(impuls.pulse*PULSE_LENGTH);
impuls.raw[i+3]=(PULSE_LENGTH);
}
}
void impulsCreateHigh(int s, int e) {
int i;
for(i=s;i<=e;i+=4) {
impuls.raw[i]=(PULSE_LENGTH);
impuls.raw[i+1]=(impuls.pulse*PULSE_LENGTH);
impuls.raw[i+2]=(PULSE_LENGTH);
impuls.raw[i+3]=(impuls.pulse*PULSE_LENGTH);
}
}
void impulsClearCode(void) {
impulsCreateLow(0,47);
}
void impulsCreateUnit(int unit) {
int binary[255];
int length = 0;
int i=0, x=0;
length = decToBinRev(unit, binary);
for(i=0;i<=length;i++) {
if(binary[i]==1) {
x=i*4;
impulsCreateMed(x, x+3);
}
}
}
void impulsCreateId(int id) {
int binary[255];
int length = 0;
int i=0, x=0;
length = decToBinRev(id, binary);
for(i=0;i<=length;i++) {
if(binary[i]==1) {
x=i*4;
impulsCreateHigh(20+x, 20+x+3);
}
}
}
void impulsCreateState(int state) {
if(state == 0) {
impulsCreateHigh(40, 43);
} else {
impulsCreateHigh(44, 47);
}
impuls.raw[48]=(PULSE_LENGTH);
impuls.raw[49]=(impuls.footer*PULSE_LENGTH);
}
int impulsCreateCode(JsonNode *code) {
int id = -1;
int unit = -1;
int state = -1;
char *tmp;
if(json_find_string(code, "id", &tmp) == 0)
id=atoi(tmp);
if(json_find_string(code, "off", &tmp) == 0)
state=0;
else if(json_find_string(code, "on", &tmp) == 0)
state=1;
if(json_find_string(code, "unit", &tmp) == 0)
unit = atoi(tmp);
if(id == -1 || unit == -1 || state == -1) {
logprintf(LOG_ERR, "impuls: insufficient number of arguments");
return EXIT_FAILURE;
} else if(id > 31 || id < 0) {
logprintf(LOG_ERR, "impuls: invalid id range");
return EXIT_FAILURE;
} else if(unit > 31 || unit < 0) {
logprintf(LOG_ERR, "impuls: invalid unit range");
return EXIT_FAILURE;
} else {
impulsCreateMessage(id, unit, state);
impulsClearCode();
impulsCreateUnit(unit);
impulsCreateId(id);
impulsCreateState(state);
}
return EXIT_SUCCESS;
}
void impulsPrintHelp(void) {
printf("\t -t --on\t\t\tsend an on signal\n");
printf("\t -f --off\t\t\tsend an off signal\n");
printf("\t -u --unit=unit\t\t\tcontrol a device with this unit code\n");
printf("\t -i --id=id\t\t\tcontrol a device with this id\n");
}
void impulsInit(void) {
strcpy(impuls.id, "impuls");
protocol_add_device(&impuls, "impuls", "Impuls Switches");
protocol_add_device(&impuls, "selectremote", "SelectRemote Switches");
protocol_add_conflict(&impuls, "sartano");
impuls.type = SWITCH;
impuls.pulse = 3;
impuls.footer = 31;
impuls.length = 50;
impuls.message = malloc(sizeof(JsonNode));
impuls.bit = 0;
impuls.recording = 0;
options_add(&impuls.options, 't', "on", no_value, config_state, NULL);
options_add(&impuls.options, 'f', "off", no_value, config_state, NULL);
options_add(&impuls.options, 'u', "unit", has_value, config_id, "^(3[12]?|[01][0-9]|[0-9]{1})$");
options_add(&impuls.options, 'i', "id", has_value, config_id, "^(3[12]?|[01][0-9]|[0-9]{1})$");
impuls.parseBinary=&impulsParseBinary;
impuls.createCode=&impulsCreateCode;
impuls.printHelp=&impulsPrintHelp;
protocol_register(&impuls);
} |