1 #include "sircprotocol.h"
3 #include "pirrx51hardware.h"
5 #include "pirexception.h"
8 // Some global communications stuff:
10 extern bool commandInFlight;
11 extern QMutex commandIFMutex;
13 // The SIRC protocol should have the following attributes:
14 // A "zero" is encoded with a 600 usec pulse, 600 usec space.
15 // A "one" is encoded with a 1200 usec pulse, and 600 usec space.
16 // The header is a 2400 usec pulse, 600 usec space.
17 // There is no trailing pulse.
18 // When repeating a command, the entire train is re-broadcast every 45000 usec.
19 // The carrier frequency is 40 kHz, duty cycle is 1/3.
21 SIRCProtocol::SIRCProtocol(
32 setCarrierFrequency(40000);
37 void SIRCProtocol::startSendingCommand(
38 unsigned int threadableID,
41 // Exceptions here are problematic; I'll try to weed them out by putting the
42 // whole thing in a try/catch block:
45 // First, check if we are meant to be the recipient of this command:
46 if (threadableID != id) return;
50 KeycodeCollection::const_iterator i = keycodes.find(command);
52 // Do we even have this key defined?
53 if (i == keycodes.end())
55 QMutexLocker cifLocker(&commandIFMutex);
56 commandInFlight = false;
58 // std::string s = "Tried to send a non-existent command.\n";
59 // throw PIRException(s);
62 // construct the device:
63 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
66 int commandDuration = 0;
67 while (repeatCount < MAX_REPEAT_COUNT)
69 commandDuration = generateStandardCommand((*i).second, rx51device);
71 // Now, tell the device to send the whole command:
72 rx51device.sendCommandToDevice();
74 // sleep until the next repetition of command:
75 sleepUntilRepeat(commandDuration);
77 // Check whether we've reached the minimum required number of repetitons:
78 // if (repeatCount >= minimumRepetitions)
81 // Check whether we've been asked to stop:
82 if (checkRepeatFlag())
86 QMutexLocker cifLocker(&commandIFMutex);
87 commandInFlight = false;
96 QMutexLocker cifLocker(&commandIFMutex);
97 commandInFlight = false;
99 catch (PIRException e)
102 emit commandFailed(e.getError().c_str());
107 int SIRCProtocol::generateStandardCommand(
108 const PIRKeyBits &pkb,
109 PIRRX51Hardware &rx51device)
113 // First, the "header" pulse:
114 rx51device.addPair(headerPulse, headerSpace);
115 duration += (headerPulse + headerSpace);
117 // Next, push the data.
118 // These bits are sent in reverse order, and moreover, the codes are sent
119 // in reverse order as well:
120 duration += pushReverseBits(pkb.thirdCode, rx51device);
121 duration += pushReverseBits(pkb.secondCode, rx51device);
122 duration += pushReverseBits(pkb.firstCode, rx51device);