-
Notifications
You must be signed in to change notification settings - Fork 0
/
pirprotocol.cpp
577 lines (488 loc) · 12.6 KB
/
pirprotocol.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//
// pirprotocol.cpp
//
// Copyright 2012 - 2015 by John Pietrzak ([email protected])
//
// This file is part of Pierogi.
//
// Pierogi 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.
//
// Pierogi 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 "pirprotocol.h"
//#include <QMutex>
//#include <QMetaType>
#include <time.h>
#include <errno.h>
//#include <QString>
// A flag for communicating with the main thread:
extern bool stopRepeatingFlag;
//extern QMutex stopRepeatingMutex;
// Check if a command is running:
//extern bool commandInFlight;
//extern QMutex commandIFMutex;
// From what I understand (mostly from reading LIRC config files), NEC
// protocol based remotes mostly use a frequency of 38000 units and a
// duty cycle of 33%. They'll be set to these defaults here, and overridden
// as needed by child classes.
PIRProtocol::PIRProtocol(unsigned int index,
unsigned int gSpace,
bool iclflag)
: carrierFrequency(38000),
dutyCycle(33),
isConstantLength(iclflag),
gap(gSpace),
minimumRepetitions(0),
id(index)
{
// qRegisterMetaType<PIRKeyName>("PIRKeyName");
// qRegisterMetaType<PIRACStateInfo>("PIRACStateInfo");
// QObject::connect(
// guiObject,
// SIGNAL(buttonPressed(unsigned int, PIRKeyName)),
// this,
// SLOT(startSendingCommand(unsigned int, PIRKeyName)),
// Qt::QueuedConnection);
// QObject::connect(
// guiObject,
// SIGNAL(buttonPressed(PIRACStateInfo, unsigned int, PIRKeyName)),
// this,
// SLOT(startSendingStateInfo(PIRACStateInfo, unsigned int, PIRKeyName)),
// Qt::QueuedConnection);
}
unsigned int PIRProtocol::getCarrierFrequency() const
{
return carrierFrequency;
}
void PIRProtocol::setCarrierFrequency(
unsigned int cf)
{
carrierFrequency = cf;
}
unsigned int PIRProtocol::getDutyCycle() const
{
return dutyCycle;
}
void PIRProtocol::setDutyCycle(
unsigned int dc)
{
dutyCycle = dc;
}
void PIRProtocol::addKey(
PIRKeyName key,
unsigned long command,
unsigned int size)
{
// First, if key already exists, clear it out:
PIRKeyBits *pkb = 0;
KeycodeCollection::iterator i = keycodes.find(key);
if (i != keycodes.end())
{
pkb = &(i->second);
pkb->firstCode.clear();
}
else
{
pkb = &(keycodes[key]);
}
appendToBitSeq(pkb->firstCode, command, size);
}
void PIRProtocol::addSIRCKey(
PIRKeyName key,
unsigned int addressData,
unsigned int size,
unsigned int commandData)
{
// First, if key already exists, clear it out:
PIRKeyBits *pkb = 0;
KeycodeCollection::iterator i = keycodes.find(key);
if (i != keycodes.end())
{
pkb = &(i->second);
pkb->firstCode.clear();
pkb->secondCode.clear();
pkb->thirdCode.clear();
}
else
{
pkb = &(keycodes[key]);
}
// First, append the address data:
appendToBitSeq(pkb->firstCode, addressData, size);
// Next, the command data. The size is always 7 bits:
appendToBitSeq(pkb->secondCode, commandData, 7);
}
void PIRProtocol::addSIRC20Key(
PIRKeyName key,
unsigned int secondaryAddressData,
unsigned int primaryAddressData,
unsigned int commandData)
{
// First, if key already exists, clear it out:
PIRKeyBits *pkb = 0;
KeycodeCollection::iterator i = keycodes.find(key);
if (i != keycodes.end())
{
pkb = &(i->second);
pkb->firstCode.clear();
pkb->secondCode.clear();
pkb->thirdCode.clear();
}
else
{
pkb = &(keycodes[key]);
}
// First, append the secondary address data:
appendToBitSeq(pkb->firstCode, secondaryAddressData, 8);
// Next, the primary address data:
appendToBitSeq(pkb->secondCode, primaryAddressData, 5);
// Next, the command data. The size is always 7 bits:
appendToBitSeq(pkb->thirdCode, commandData, 7);
}
void PIRProtocol::addSharpKey(
PIRKeyName key,
unsigned int addressData,
unsigned int commandData)
{
// First, if key already exists, clear it out:
PIRKeyBits *pkb = 0;
KeycodeCollection::iterator i = keycodes.find(key);
if (i != keycodes.end())
{
pkb = &(i->second);
pkb->firstCode.clear();
pkb->secondCode.clear();
}
else
{
pkb = &(keycodes[key]);
}
// Sharp commands are all 5 bit address, 8 bit command:
appendToBitSeq(pkb->firstCode, addressData, 5);
appendToBitSeq(pkb->secondCode, commandData, 8);
}
void PIRProtocol::addNECKey(
PIRKeyName key,
unsigned int addressData,
unsigned int commandData)
{
PIRKeyBits *pkb = 0;
KeycodeCollection::iterator i = keycodes.find(key);
if (i != keycodes.end())
{
pkb = &(i->second);
pkb->firstCode.clear();
pkb->secondCode.clear();
pkb->thirdCode.clear();
pkb->fourthCode.clear();
}
else
{
pkb = &(keycodes[key]);
}
// NEC commands should always be 8 bits address, 8 bits command:
appendToBitSeq(pkb->firstCode, addressData, 8);
appendToBitSeq(pkb->secondCode, commandData, 8);
}
void PIRProtocol::addPanOldKey(
PIRKeyName key,
unsigned int addressData,
unsigned int commandData)
{
PIRKeyBits *pkb = 0;
KeycodeCollection::iterator i = keycodes.find(key);
if (i != keycodes.end())
{
pkb = &(i->second);
pkb->firstCode.clear();
pkb->secondCode.clear();
pkb->thirdCode.clear();
pkb->fourthCode.clear();
}
else
{
pkb = &(keycodes[key]);
}
// The "Old Panasonic" commands have 5 bits address, 6 bits command:
appendToBitSeq(pkb->firstCode, addressData, 5);
appendToBitSeq(pkb->secondCode, commandData, 6);
}
// Most Pioneer keys use the NEC key format, but some are pairs of
// NEC keys sent together:
void PIRProtocol::addPioneerKey(
PIRKeyName key,
unsigned int firstAddress,
unsigned int firstCommand,
unsigned int secondAddress,
unsigned int secondCommand)
{
PIRKeyBits *pkb = 0;
KeycodeCollection::iterator i = keycodes.find(key);
if (i != keycodes.end())
{
pkb = &(i->second);
pkb->firstCode.clear();
pkb->secondCode.clear();
pkb->thirdCode.clear();
pkb->fourthCode.clear();
}
else
{
pkb = &(keycodes[key]);
}
// All four codes should be 8 bits in length:
appendToBitSeq(pkb->firstCode, firstAddress, 8);
appendToBitSeq(pkb->secondCode, firstCommand, 8);
appendToBitSeq(pkb->thirdCode, secondAddress, 8);
appendToBitSeq(pkb->fourthCode, secondCommand, 8);
}
/*
void PIRProtocol::addRCAKey(
PIRKeyName key,
unsigned int addressData,
unsigned int commandData)
{
PIRKeyBits *pkb = 0;
KeycodeCollection::iterator i = keycodes.find(key);
if (i != keycodes.end())
{
pkb = &(i->second);
pkb->firstcode.clear();
pkb->secondCode.clear();
}
else
{
pkb = &(keycodes[key]);
}
// Address is 4 bits, command is 8 bits:
appendToBitSeq(pkb->firstCode, addressData, 4);
appendToBitSeq(pkb->secondCode, commandData, 8);
}
*/
void PIRProtocol::addKaseikyoKey(
PIRKeyName key,
unsigned int addressData,
unsigned int commandData)
{
PIRKeyBits *pkb = 0;
KeycodeCollection::iterator i = keycodes.find(key);
if (i != keycodes.end())
{
pkb = &(i->second);
pkb->firstCode.clear();
pkb->secondCode.clear();
}
else
{
pkb = &(keycodes[key]);
}
appendToBitSeq(pkb->firstCode, addressData, 12);
appendToBitSeq(pkb->secondCode, commandData, 8);
}
void PIRProtocol::addPanasonicKey(
PIRKeyName key,
unsigned int deviceData,
unsigned int subdeviceData,
unsigned int commandData)
{
PIRKeyBits *pkb = 0;
KeycodeCollection::iterator i = keycodes.find(key);
if (i != keycodes.end())
{
pkb = &(i->second);
pkb->firstCode.clear();
pkb->secondCode.clear();
pkb->thirdCode.clear();
}
else
{
pkb = &(keycodes[key]);
}
appendToBitSeq(pkb->firstCode, deviceData, 8);
appendToBitSeq(pkb->secondCode, subdeviceData, 8);
appendToBitSeq(pkb->thirdCode, commandData, 8);
}
void PIRProtocol::addDishKey(
PIRKeyName key,
unsigned int firstCommand,
unsigned int secondCommand)
{
PIRKeyBits *pkb = 0;
KeycodeCollection::iterator i = keycodes.find(key);
if (i != keycodes.end())
{
pkb = &(i->second);
pkb->firstCode.clear();
pkb->secondCode.clear();
}
else
{
pkb = &(keycodes[key]);
}
appendToBitSeq(pkb->firstCode, firstCommand, 6);
appendToBitSeq(pkb->secondCode, secondCommand, 5);
}
void PIRProtocol::addXMPKey(
PIRKeyName key,
unsigned int firstCommand,
unsigned int secondCommand)
{
PIRKeyBits *pkb = 0;
KeycodeCollection::iterator i = keycodes.find(key);
if (i != keycodes.end())
{
pkb = &(i->second);
pkb->firstCode.clear();
pkb->secondCode.clear();
}
else
{
pkb = &(keycodes[key]);
}
appendToBitSeq(pkb->firstCode, firstCommand, 8);
appendToBitSeq(pkb->secondCode, secondCommand, 8);
}
void PIRProtocol::setMinimumRepetitions(
unsigned int minrep)
{
minimumRepetitions = minrep;
}
void PIRProtocol::setPreData(
unsigned long data,
unsigned int bits)
{
// If the container is not empty, first clear it out:
if (!preData.empty())
{
preData.clear();
}
appendToBitSeq(preData, data, bits);
}
void PIRProtocol::setPostData(
unsigned long data,
unsigned int bits)
{
// If the container is not empty, first clear it out:
if (!postData.empty())
{
postData.clear();
}
appendToBitSeq(postData, data, bits);
}
//void PIRProtocol::startSendingStateInfo(
// PIRACStateInfo state,
// unsigned int threadableID,
// PIRKeyName command)
//{
// // This method can be ignored by most non-air-conditioner protocols.
//}
bool PIRProtocol::isCommandSupported(
PIRKeyName command)
{
return (keycodes.find(command) != keycodes.end());
}
void PIRProtocol::appendToBitSeq(
CommandSequence &sequence,
unsigned int bits,
int size)
{
if (size == 0)
{
// This is bad, but just return silently for now...
return;
}
// For each bit in the char, append a 1 or a 0 into the sequence.
// Starting with the largest bit, move forward one bit at a time:
unsigned int currentBit = 1 << (size - 1);
do
{
if (bits & currentBit)
{
sequence.push_back(1);
}
else
{
sequence.push_back(0);
}
currentBit = currentBit >> 1;
}
while (currentBit > 0);
}
void PIRProtocol::clearRepeatFlag()
{
// QMutexLocker locker(&stopRepeatingMutex);
stopRepeatingFlag = false;
}
bool PIRProtocol::checkRepeatFlag()
{
// QMutexLocker locker(&stopRepeatingMutex);
return stopRepeatingFlag;
}
// Note that the following routine blindly sleeps for the amount of time
// specified by the LIRC config file. The extra overhead of processing
// each command will mean that repeated commands will overshoot the config
// time by some amount. We could improve accuracy by waiting a little less
// than the specified time, if we could get a good handle on how long the
// overhead is delaying the command...
#define PIEROGI_OVERHEAD_HACK 13260
void PIRProtocol::sleepUntilRepeat(
int commandDuration)
{
int microseconds;
// If the LIRC config file specifies the flag "CONST_LENGTH", that means
// the "gap" value is the exact amount of time to wait between kicking off
// each command. If not, then the "gap" needs to be added on to the total
// time of the previous command to see how long to sleep.
if (isConstantLength)
{
microseconds = (gap - commandDuration) - PIEROGI_OVERHEAD_HACK;
}
else
{
microseconds = gap - PIEROGI_OVERHEAD_HACK;
}
/*
// Don't even bother sleeping if there's only a few microseconds:
if (microseconds < 1000)
{
return;
}
*/
// For now, I'm going to enforce a minimum sleep of 10 ms, so that we
// don't get runaway commands:
if (microseconds < 10000)
{
microseconds = 10000;
}
timespec sleeptime;
sleeptime.tv_sec = 0;
sleeptime.tv_nsec = microseconds * 1000;
timespec remainingtime;
// std::cout << "About to sleep." << std::endl;
if (nanosleep(&sleeptime, &remainingtime) == -1)
{
std::string errStr = "Problem while sleeping.\nTrying to sleep for: ";
std::cout << errStr << std::endl;
// errStr += std::string(microseconds);
// errStr += "\nNanosleep returned error: ";
// errStr += strerror(errno);
// emit errorMessage(errStr);
}
}
void PIRProtocol::setGapSize(
int gapSize,
bool iclFlag)
{
gap = gapSize;
isConstantLength = iclFlag;
}