-
Notifications
You must be signed in to change notification settings - Fork 0
/
RFSendRemoteBtnCode.cpp
528 lines (463 loc) · 17.1 KB
/
RFSendRemoteBtnCode.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*
'codesend' hacked from 'send' by @justy
Usage: Voir en dessous
(Use RF_Sniffer.ino to check that RF signals are being produced by the RPi's transmitter)
Compiler :
g++ -c codesend.cpp -o codesend.o
g++ codesend.o RCSwitch.o -o codesend -lwiringPi
*/
#include "RCSwitch.h"
#include <stdlib.h>
#include <stdio.h>
#include <map>
#include <iostream>
#include <sstream>
#include <fstream>
#include <cstring>
#include "json/json.h"
using namespace std;
using namespace Json;
#define INVALIDE_REMOTE_PROP " Invalid remote properties for remote: "
std::map<std::string, std::string> commandArgs;
Json::Reader jsonReader;
Json::Value jsonRoot;
Json::Value jsonSignal;
string btnName;
double startlock_highlow[2];
double endlock_highlow[2];
double zeroEncoding_highlow[2];
double oneEncoding_highlow[2];
char remotecode[4096];
string remotecodestring;
/** Turns a decimal value to its binary representation */
char* decTobin(uint64_t dec)
{
static char bin[4096];
static char binToReturn[4096];
unsigned int i=0;
uint64_t entierDecDivisPar2;
int reste;
while (dec > 0)
{
entierDecDivisPar2 = dec/2;
reste = dec - 2 * entierDecDivisPar2;
if (reste == 0) bin[i++]='0';
else bin[i++]='1';
dec=entierDecDivisPar2;
}
for (unsigned int j = 0; j< i; j++) binToReturn[j] = bin[i-1 - j];
binToReturn[i] = '\0';
return binToReturn;
}
/** Turns a decimal value to its binary representation
*
* binary : b1 b2 b3 b4
* Padded binary : 0 0 0 0 0 b1 b2 b3 b4
*/
char* dec2binWzerofill(uint64_t dec, unsigned int bitLength)
{
static char paddedBinToReturn[4096];
char * decodedBinaryStart = RCSwitch::decTobin(dec);
int decodedBinLen = strlen(decodedBinaryStart);
if (decodedBinLen > bitLength) // Impossible de faire la traduction
{
for(int i=0;i<bitLength;i++)
paddedBinToReturn[i]='0';
paddedBinToReturn[bitLength] = '\0';
return paddedBinToReturn;
}
// copie des bits
for(int i=0;i<decodedBinLen;i++)
paddedBinToReturn[bitLength-decodedBinLen+i]=decodedBinaryStart[i];
// padding à gauche
for(int i=0;i<bitLength-decodedBinLen;i++)
paddedBinToReturn[i]='0';
paddedBinToReturn[bitLength]='\0';
return paddedBinToReturn;
}
/* ----------------------------------------------------------------------------------------------------------- */
int loglevel = 1;
// Fonction de trace
void trace(int level, const char * msg)
{
if (loglevel <= level) {cout << "[" << level << "] " << msg << endl;}
}
/* ---------------------------------------------------------------- */
string intToString(int mylong)
{
string mystring;
stringstream mystream;
mystream << mylong;
return mystream.str();
}
/* ---------------------------------------------------------------- */
int parseCommandLineArgs(int nbArgs, char ** args)
{
for (int i=1;i<nbArgs; i++)
{
if (args[i][0] == '-')
{
size_t pos = 0;
size_t lastpos = 1;
std::string key="";
std::string token;
std::string toparse = args[i];
std::string delimiter = "=";
pos = toparse.find(delimiter.c_str(), lastpos) ;
while ( pos != std::string::npos)
{
token = toparse.substr(lastpos, pos-1);
key = key + token;
lastpos = pos +delimiter.length();
pos = toparse.find(delimiter.c_str(), lastpos) ;
}
std::string value = toparse.substr(lastpos, toparse.length());
commandArgs[key]=value;
}
else // pas de '-' au début
{
std:string key = intToString(i);
commandArgs[key]=args[i];
}
}
return 0;
}
/* ---------------------------------------------------------------- */
void displayCommandLineArgs()
{
if (loglevel <= 0)
{
cout << "-----------------------------------------------------------------------" << endl;
cout << "- CommandLine Arguments " << endl;
for (std::map<std::string,std::string>::iterator it=commandArgs.begin(); it!=commandArgs.end(); ++it)
{
cout << it->first << " => " << it->second << endl;
}
cout << "-----------------------------------------------------------------------" << endl;
}
}
/* ---------------------------------------------------------------- */
void diplayRemotes()
{
Value::Members members = jsonRoot.getMemberNames();
for (std::vector<string>::iterator it = members.begin(); it != members.end(); ++it)
if (string::npos != (*it).find("remote_"))
cout << " " << *it << endl;
}
/* ---------------------------------------------------------------- */
void diplayButtonsForRemote(Value::Members & membersOfSignals)
{
for (std::vector<string>::iterator it = membersOfSignals.begin(); it != membersOfSignals.end(); ++it)
cout << " " << *it << endl;
}
/* ---------------------------------------------------------------- */
int main(int argc, char *argv[])
{
parseCommandLineArgs(argc, argv);
ifstream jsonFile;
string rfJsonFilePath = "./radioFrequenceSignalConfig.json";
int repeat = 10;
int protocol = 1;
int pin = 0;
double pulseLength = 0;
long signalBitNumber = 0;
int decimalcode = 0;
int nbbit = 24;
string remote = "";
bool isManchesterCoded = false;
string binarycode = "";
std::map<std::string,std::string>::iterator it;
it = commandArgs.find("lev");
if( commandArgs.find("lev") != commandArgs.end()) loglevel=atoi(it->second.c_str());
if( commandArgs.find("listremotes") == commandArgs.end() && commandArgs.find("getremotebuttons") == commandArgs.end() )
displayCommandLineArgs();
it = commandArgs.find("jsonconf");
if(it != commandArgs.end()) rfJsonFilePath=commandArgs["jsonconf"].c_str();
// chargement de la conf (fichier json contenant toutes les infos des télécommandes radiofréquence)
jsonFile.open(rfJsonFilePath.c_str());
bool parsedResult = jsonReader.parse(jsonFile, jsonRoot, false);
jsonFile.close();
if(!parsedResult) // Report failures and their locations in the document.
{
string str = "Echec d'analyse du fichier " + rfJsonFilePath + " : \n" + jsonReader.getFormatedErrorMessages() + "\n";
trace(3,str.c_str());
return 1;
}
it = commandArgs.find("listremotes");
if( commandArgs.find("listremotes") != commandArgs.end()) {diplayRemotes(); return 0;}
it = commandArgs.find("repeat");
if(it != commandArgs.end()) repeat=atoi(commandArgs["repeat"].c_str());
it = commandArgs.find("remote");
if(it != commandArgs.end()) remote=commandArgs["remote"];
else
{
trace(3,"Parameter remote must be set (-remote=<value>) with one of the following values");
diplayRemotes();
return 1;
}
// Parse the firt parameter to this command as an integer
if (wiringPiSetup () == -1) return 1;
RCSwitch mySwitch = RCSwitch();
mySwitch.enableTransmit(pin);
mySwitch.setRepeatTransmit(repeat);
mySwitch.setProtocol(protocol);
//-------- Getting protocol signal information from json document ---------//
Json::Value remoteNode = jsonRoot[remote];
if (remoteNode.isNull())
{
trace(3,"Unknown remote");
diplayRemotes();
return 2;
}
Json::Value protocolNode = remoteNode["protocol"];
if (protocolNode.isNull() || !protocolNode.isInt())
{
string str = INVALIDE_REMOTE_PROP + remote + " 'protocol' is not set or is not an integer";
trace(3, str.c_str());
return 3;
}
else {protocol = protocolNode.asInt();}
Json::Value pulseLengthNode = remoteNode["pulseLength"];
if (pulseLengthNode.isNull())
{
string str = INVALIDE_REMOTE_PROP + remote + " 'pulseLength' is not set or is not an integer";
trace(3, str.c_str());
return 4;
}
else pulseLength = pulseLengthNode.asInt();
Json::Value signalBitNumberNode = remoteNode["SignalBitNumber"];
if (signalBitNumberNode.isNull()) {}
else signalBitNumber = signalBitNumberNode.asInt();
Json::Value signalsNode = remoteNode["signals"];
if (signalsNode.isNull())
{
string str = INVALIDE_REMOTE_PROP + remote + " 'signals' is not or not correctly set";
trace(3, str.c_str());
return 5;
}
else
{
Value::Members membersOfSignals = signalsNode.getMemberNames();
it = commandArgs.find("getremotebuttons");
if( commandArgs.find("getremotebuttons") != commandArgs.end() )
{diplayButtonsForRemote(membersOfSignals); return 0;}
it = commandArgs.find("btn");
if(it == commandArgs.end())
{
trace(3, "Parameter 'btn' must be set (-btn=<value>) with one of the following values");
diplayButtonsForRemote(membersOfSignals);
return 6;
}
else
{
string btnName = commandArgs["btn"];
jsonSignal = signalsNode[btnName];
if (jsonSignal.isNull())
{
string str = INVALIDE_REMOTE_PROP + remote + " button:" + btnName + " Signal code is not set";
trace(3, str.c_str());
return 7;
}
else if (!jsonSignal.isArray() && (!jsonSignal.isInt()))
{
string str = INVALIDE_REMOTE_PROP + remote + " button:" + btnName + " Signal code is neither an array nor an integer";
trace(3, str.c_str());
return 8;
}
}
}
// Assertion sur signalBitNumberNode!=0 ou jsonSignal.isArray()
if (signalBitNumber == 0 && !jsonSignal.isArray() )
{
trace(3, "Propeties of remote are invalid : either 'signalBitNumberNode' must be set or each button signal must be an array of bit values (0,1) ");
return 9;
}
//------------------------ Getting protocole info for this remote -----------------//
string protocole = "protocole" + patch::to_string(protocol);
Json::Value protocoleInfoNode = jsonRoot[protocole];
if (protocoleInfoNode.isNull())
{
string str = INVALIDE_REMOTE_PROP + remote + " : remote protocol '" + protocole + "' doesn't exist in declared protocols ";
trace(3, str.c_str());
return 10;
}
Json::Value presendSignalCodeTransformationNode = remoteNode["PresendSignalCodeTransformation"];
if (!presendSignalCodeTransformationNode.isNull())
{
string presendSignalCodeTransformationValue = presendSignalCodeTransformationNode.asString();
if (presendSignalCodeTransformationValue.find("MANCHESTER") != string::npos)
{isManchesterCoded = true;}
}
else
{
Json::Value protocol_presendSignalCodeTransformationNode = protocoleInfoNode["PresendSignalCodeTransformation"];
if (!protocol_presendSignalCodeTransformationNode.isNull() && protocol_presendSignalCodeTransformationNode.asString().find("MANCHESTER") != string::npos)
{isManchesterCoded = true;}
}
//------------------ Getting Start lock HIGH-->LOW pulses ---------//
Json::Value startlock_highlowNode = remoteNode["startlock_highlow"];
if (!startlock_highlowNode.isNull()) // startlock_highlow est précisé dans les info du button de la remote
{
if (!startlock_highlowNode.isArray() || startlock_highlowNode.size() != 2)
{
string str = INVALIDE_REMOTE_PROP + remote + "' : startlock_highlow is not an array or is not well set";
trace(3, str.c_str());
return 10;
}
else
{
startlock_highlow[0] = startlock_highlowNode[0].asInt();
startlock_highlow[1] = startlock_highlowNode[1].asInt();
}
}
else // Retrouver le noeud protocole et en déduire startlock_highlow
{
Json::Value startlock_highlowNode = protocoleInfoNode["startlock_low_NbPulseLength"];
if (startlock_highlowNode.isNull())
{
string str = INVALIDE_REMOTE_PROP + remote + "' : startlock_highlow array is neither set in btn, nor in protocol";
trace(3, str.c_str());
return 11;
}
else if (startlock_highlowNode.isDouble())
{
startlock_highlow[0] = pulseLength;
startlock_highlow[1] = startlock_highlowNode.asDouble() * pulseLength;
}
else // must be an integer
{
string str = INVALIDE_REMOTE_PROP + remote + "' : startlock_highlow must be an integer";
trace(3, str.c_str());
return 12;
}
}
//------------------ Getting End lock HIGH-->LOW pulses ---------//
Json::Value endlock_highlowNode = remoteNode["endlock_highlow"];
if (!endlock_highlowNode.isNull())
{
if (!endlock_highlowNode.isArray() || endlock_highlowNode.size() != 2)
{
string str = INVALIDE_REMOTE_PROP + remote + "' : endlock_highlow is not an array or is not well set";
trace(3, str.c_str());
return 13;
}
else
{
endlock_highlow[0] = endlock_highlowNode[0].asInt();
endlock_highlow[1] = endlock_highlowNode[1].asInt();
}
}
else // Retrouver le noeud protocole et en déduire endlock_highlow
{
Json::Value endlock_highlowNode = protocoleInfoNode["endlock_low_NbPulseLength"];
if (endlock_highlowNode.isNull())
{
string str = INVALIDE_REMOTE_PROP + remote + "' : endlock_highlow array is neither set in btn, nor in protocol";
trace(3, str.c_str());
return 14;
}
else if (endlock_highlowNode.isDouble())
{
endlock_highlow[0] = pulseLength;
endlock_highlow[1] = endlock_highlowNode.asDouble() * pulseLength;
}
else // must be an integer
{
string str = INVALIDE_REMOTE_PROP + remote + "' : endlock_highlow must be an integer";
trace(3, str.c_str());
return 15;
}
}
//------------------ Getting zeroEncodingInNbPulse HIGH-->LOW pulses ---------//
Json::Value protocol_zeroEncodingInNbPulseNode = protocoleInfoNode["ZeroEncoding_highlow_NbPulseLength"];
Json::Value zeroEncodingInNbPulseNode = remoteNode["ZeroEncodingInNbPulse"];
if (!zeroEncodingInNbPulseNode.isNull() && zeroEncodingInNbPulseNode.isArray())
{
zeroEncoding_highlow[0] = zeroEncodingInNbPulseNode[0].asDouble();
zeroEncoding_highlow[1] = zeroEncodingInNbPulseNode[1].asDouble();
}
else if (!protocol_zeroEncodingInNbPulseNode.isNull() && protocol_zeroEncodingInNbPulseNode.isArray())
{
zeroEncoding_highlow[0] = protocol_zeroEncodingInNbPulseNode[0].asDouble() * pulseLength;
zeroEncoding_highlow[1] = protocol_zeroEncodingInNbPulseNode[1].asDouble() * pulseLength;
}
else
{
string str = INVALIDE_REMOTE_PROP + remote + " : 'zeroEncoding_highlow' parameter must be an array and must be set either in 'remote node' or in 'protocol' node";
trace(3, str.c_str());
return 16;
}
//------------------ Getting oneEncodingInNbPulse HIGH-->LOW pulses ---------//
Json::Value protocol_oneEncodingInNbPulseNode = protocoleInfoNode["OneEncoding_highlow_NbPulseLength"];
Json::Value oneEncodingInNbPulseNode = remoteNode["OneEncodingInNbPulse"];
if (!oneEncodingInNbPulseNode.isNull() && oneEncodingInNbPulseNode.isArray())
{
oneEncoding_highlow[0] = oneEncodingInNbPulseNode[0].asDouble();
oneEncoding_highlow[1] = oneEncodingInNbPulseNode[1].asDouble();
}
else if (!protocol_oneEncodingInNbPulseNode.isNull() && protocol_oneEncodingInNbPulseNode.isArray())
{
oneEncoding_highlow[0] = protocol_oneEncodingInNbPulseNode[0].asDouble() * pulseLength;
oneEncoding_highlow[1] = protocol_oneEncodingInNbPulseNode[1].asDouble() * pulseLength;
}
else
{
string str = INVALIDE_REMOTE_PROP + remote + " : 'oneEncoding_highlow' parameter must be an array and must be set in 'remote node' or in 'protocol' node";
trace(3, str.c_str());
return 17;
}
//---------------------------------------------- Traitement d'envoi du signal --------------------------------------------------//
if (jsonSignal.isArray())
{
remotecodestring = "";
for(unsigned int index=0; index<jsonSignal.size(); ++index)
remotecodestring.append(jsonSignal[index].asString());
strcpy(remotecode,remotecodestring.c_str());
}
else // c'est un entier
{
decimalcode = jsonSignal.asInt();
strcpy(remotecode,dec2binWzerofill(decimalcode,signalBitNumber));
}
if (isManchesterCoded) // Codage manchester si nécessaire
{
string binarycode = "";
for(int i=0;i<strlen(remotecode);i++)
{
if (remotecode[i]=='0') binarycode.append("01");
else if (remotecode[i]=='1') binarycode.append("10");
else
{
string str = " Invalid manchester coded signal : "; str += remotecode;
trace(3, str.c_str());
return 18;
}
}
strcpy(remotecode,binarycode.c_str());
}
// positionner les paramètres de mySwitch : pulseLength, protocole, etc ...
mySwitch.setRepeatTransmit(repeat);
mySwitch.setPulseLength(pulseLength);
mySwitch.setProtocol(protocol);
mySwitch.setSendZeroEncodingInNbPulse(zeroEncoding_highlow[0]/pulseLength, zeroEncoding_highlow[1]/pulseLength);
mySwitch.setSendOneEncodingInNbPulse(oneEncoding_highlow[0]/pulseLength, oneEncoding_highlow[1]/pulseLength);
mySwitch.setSendStartLockHighLowInNbPulse(startlock_highlow[0]/pulseLength, startlock_highlow[1]/pulseLength);
mySwitch.setSendEndLockHighLowInNbPulse(endlock_highlow[0]/pulseLength, endlock_highlow[1]/pulseLength);
cout << "> Sending raw code = '"<< remotecode << "' > " << endl;
if (loglevel <= 1)
{
cout << " Number of bit = " << strlen(remotecode) << endl;
cout << " Manchester = " << isManchesterCoded << endl;
cout << " Protocole = " << protocol << endl;
cout << " ZeroEncoding = " << zeroEncoding_highlow[0] << "," << zeroEncoding_highlow[1] << endl;
cout << " OneEncoding = " << oneEncoding_highlow[0] << "," << oneEncoding_highlow[1] << endl;
cout << " StartLock = " << startlock_highlow[0] << "," << startlock_highlow[1] << endl;
cout << " EndLock = " << endlock_highlow[0] << "," << endlock_highlow[1] << endl;
cout << " Repeat = " << repeat << endl;
}
if (loglevel <= 0)
cout << mySwitch.computeAndDisplaySignalToSend(remotecode);
mySwitch.send(remotecode);
cout << "< Sent <" << endl;
return 0;
}