- Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDevice.cpp
More file actions
Latest commit
298 lines (217 loc) · 5.68 KB
/
Copy pathDevice.cpp
File metadata and controls
298 lines (217 loc) · 5.68 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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
Device.cpp - Handle the controlled device implementation
Saul bertuccio 4 feb 2017
Released into the public domain.
*/
#include"Device.h"
String Device::TYPES[Device::TYPE_NUM] = { "IRDA", "RF433" };
String Device::TYPES_DESCRIPTION[Device::TYPE_NUM] = { "Infrarossi", "Radio Freq. 433Mhz" };
Device::Device(const String &dev_name, const String &dev_type)
: dev_name(dev_name),
dev_type(dev_type)
{}
Device::~Device() {
while (first_key) {
Key * k = first_key->getNext();
delete first_key;
first_key = k;
}
}
voidDevice::setName(const String &new_name) { dev_name = new_name; }
voidDevice::setType(const String &new_type) { dev_type = new_type; }
String Device::getName() { return dev_name; }
String Device::getType() { return dev_type; }
boolean Device::isValidPropertyById(int id, const String &val) {
// IRDA
if (dev_type == TYPES[0])
return IrKey::validations[id](val);
// RF433
if (dev_type == TYPES[1])
return RfKey::validations[id](val);
returnfalse;
}
intDevice::getKeysPropertyNum() {
// IRDA
if (dev_type == TYPES[0])
return IrKey::props_num;
// RF433
if (dev_type == TYPES[1])
return RfKey::props_num;
}
String * Device::getKeysPropertyNames() {
int len = 0;
String * prop = NULL;
// IRDA
if (dev_type == TYPES[0])
returngetProperties(IrKey::props_names, IrKey::props_num);
// RF433
if (dev_type == TYPES[1])
returngetProperties(RfKey::props_names, RfKey::props_num);
}
Key * Device::getKeys() { return first_key; };
boolean Device::addKey(String * key_data) {
Key *key;
// Check name
if (key_data[0].length() > Key::MAX_KEY_NAME_LEN)
returnfalse;
// IRDA
if (dev_type == TYPES[0])
key = newIrKey( key_data[0], key_data[1] );
// RF433
if (dev_type == TYPES[1])
key = newRfKey( key_data[0], key_data[1].toInt(), key_data[2].toInt() );
if (!key)
returnfalse;
if ( first_key == NULL ) {
first_key = key;
last_key = key;
} else {
last_key->setNext(key);
last_key = key;
}
returntrue;
}
boolean Device::sendKeyData(const String & key_name) {
Key * k = findPreviousKeyByName(key_name);
if ( k == NULL) {
if (first_key != NULL && first_key->getName() == key_name) {
k = first_key;
} else {
returnfalse;
}
} else {
k = k->getNext();
}
if (dev_type == TYPES[0])
Device::sendIrKeyData(k);
// RF433
if (dev_type == TYPES[1]) {
Device::sendRfKeyData(k);
}
returntrue;
}
Key * Device::acquireKeyData() {
// IRDA
Key * k;
if (dev_type == TYPES[0])
k = Device::acquireIrKeyData();
// RF433
if (dev_type == TYPES[1])
k = Device::acquireRfKeyData();
return k;
}
boolean Device::removeKey(const String &key_name ) {
Key * prev_key = this->findPreviousKeyByName( key_name );
if ( prev_key == NULL) {
if ( first_key == NULL || first_key->getName() != key_name ) {
returnfalse;
}
prev_key = first_key;
first_key = prev_key->getNext();
delete prev_key;
if ( first_key == NULL ) {
last_key = NULL;
}
returntrue;
}
Key * next_key = prev_key->getNext();
prev_key->setNext(next_key->getNext());
if (next_key == last_key) {
last_key = prev_key;
}
delete next_key;
returntrue;
}
Key * Device::findPreviousKeyByName(const String &key_name) {
if (first_key == NULL)
returnNULL;
Key * key = first_key;
while (Key * next = key->getNext()) {
if ( next->getName() == key_name ) {
return key;
}
key = next;
}
returnNULL;
}
String * Device::getProperties(const String names[], int len) {
String * props = new String[len];
for (int i = 0; i < len; i++) {
props[i] = names[i];
}
return props;
}
voidDevice::sendIrKeyData(Key *key) {
static String hex_decode = "0123456789ABCDEF";
IrKey *k = static_cast<IrKey *>(key);
String code = k->getCode().substring(2);
code.toUpperCase();
if (code.length() % 2 != 0) {
Serial.println("Errore: Codice da inviare valido");
return;
}
SoftwareSerial sw(IR_RX_PIN, IR_TX_PIN, false, 16);
int i=0;
sw.begin(9600);
// Protocollo
sw.write(0xA1);
sw.write(0xF1);
while (i < code.length()) {
char n = hex_decode.indexOf(code[i++]) << 4;
n += hex_decode.indexOf(code[i++]);
sw.write(n);
}
}
voidDevice::sendRfKeyData(Key *key) {
RCSwitch sw = RCSwitch();
RfKey *k = static_cast<RfKey *>(key);
sw.enableTransmit(Device::RF_TX_PIN);
sw.send(k->getCode(), k->getLength());
sw.disableTransmit();
}
Key * Device::acquireIrKeyData() {
staticcharconst hex_encode[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
char code[11] = {'0', 'x', '\0'};
SoftwareSerial sw(IR_RX_PIN, IR_TX_PIN, false, 16);
sw.begin(9600);
boolean is_code_ok = false;
int attempt = 3;
for (int i=0; i < attempt; i++) {
delay(500);
int p = 2;
while (sw.available() > 0 && p < 15) {
char c = sw.read();
code[p++] = hex_encode[ ( c & 0xF0 ) >> 4 ];
code[p++] = hex_encode[ (c & 0x0F ) ];
delay(40);
}
sw.flush();
code[p] = '\0';
if (p>4) {
is_code_ok=true;
break;
}
}
IrKey * ir = NULL;
if (is_code_ok) {
ir = newIrKey("DUMMY", String(code));
}
return ir;
}
Key * Device::acquireRfKeyData() {
RCSwitch sw = RCSwitch();
RfKey * rf = NULL;
sw.enableReceive(Device::RF_RX_PIN);
int attempt = 3;
for (int i=0; i < attempt; i++) {
if (rf)
break;
delay(500);
if (sw.available()) {
rf = newRfKey("DUMMY", sw.getReceivedBitlength(), sw.getReceivedValue());
sw.resetAvailable();
}
}
sw.disableReceive();
return rf;
}