Gui updated, many keyset changes
[pierogi] / protocols / sircprotocol.cpp
1 #include "sircprotocol.h"
2
3 #include "pirexception.h"
4 #include <string>
5 //#include <iostream>
6
7 // Some global communications stuff:
8 #include <QMutex>
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
11
12 // The SIRC protocol should have the following attributes:
13 // A "zero" is encoded with a 600 usec pulse, 600 usec space.
14 // A "one" is encoded with a 1200 usec pulse, and 600 usec space.
15 // The header is a 2400 usec pulse, 600 usec space.
16 // There is no trailing pulse.
17 // When repeating a command, the entire train is re-broadcast every 45000 usec.
18 // The carrier frequency is 40 kHz, duty cycle is 1/3.
19
20 SIRCProtocol::SIRCProtocol(
21   QObject *guiObject,
22   unsigned int index)
23   : SpaceProtocol(
24       guiObject, index,
25       600, 600,
26       1200, 600,
27       2400, 600,
28       0,
29       45000, true)
30 {
31   setCarrierFrequency(40000);
32   setDutyCycle(33);
33 }
34
35
36 void SIRCProtocol::startSendingCommand(
37   unsigned int threadableID,
38   PIRKeyName command)
39 {
40   // Exceptions here are problematic; I'll try to weed them out by putting the
41   // whole thing in a try/catch block:
42   try
43   {
44     // First, check if we are meant to be the recipient of this command:
45     if (threadableID != id) return;
46
47     clearRepeatFlag();
48
49     KeycodeCollection::const_iterator i = keycodes.find(command);
50
51     // Do we even have this key defined?
52     if (i == keycodes.end())
53     {
54       std::string s = "Tried to send a non-existent command.\n";
55       throw PIRException(s);
56     }
57
58     // construct the device:
59     PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
60
61     int repeatCount = 0;
62     int commandDuration = 0;
63     while (repeatCount < MAX_REPEAT_COUNT)
64     {
65       commandDuration = generateStandardCommand((*i).second, rx51device);
66
67       // Now, tell the device to send the whole command:
68       rx51device.sendCommandToDevice();
69
70       // sleep until the next repetition of command:
71       sleepUntilRepeat(commandDuration);
72
73       // Check whether we've reached the minimum required number of repetitons:
74 //      if (repeatCount >= minimumRepetitions)
75       if (repeatCount >= 3)
76       {
77         // Check whether we've been asked to stop:
78         if (checkRepeatFlag())
79         {
80           QMutexLocker cifLocker(&commandIFMutex);
81           commandInFlight = false;
82           return;
83         }
84       }
85
86       ++repeatCount;
87     }
88   }
89   catch (PIRException e)
90   {
91     // inform the gui:
92     emit commandFailed(e.getError().c_str());
93   }
94
95   QMutexLocker cifLocker(&commandIFMutex);
96   commandInFlight = false;
97 }
98
99
100 int SIRCProtocol::generateStandardCommand(
101   const PIRKeyBits &pkb,
102   PIRRX51Hardware &rx51device)
103 {
104   int duration = 0;
105
106   // First, the "header" pulse:
107   rx51device.addPair(headerPulse, headerSpace);
108   duration += (headerPulse + headerSpace);
109
110   // Next, push the data.
111   // These bits are sent in reverse order, and moreover, the codes are sent
112   // in reverse order as well:
113   duration += pushReverseBits(pkb.thirdCode, rx51device);
114   duration += pushReverseBits(pkb.secondCode, rx51device);
115   duration += pushReverseBits(pkb.firstCode, rx51device);
116
117   return duration;
118 }