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 std::string s = "Tried to send a non-existent command.\n";
56 throw PIRException(s);
59 // construct the device:
60 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
63 int commandDuration = 0;
64 while (repeatCount < MAX_REPEAT_COUNT)
66 commandDuration = generateStandardCommand((*i).second, rx51device);
68 // Now, tell the device to send the whole command:
69 rx51device.sendCommandToDevice();
71 // sleep until the next repetition of command:
72 sleepUntilRepeat(commandDuration);
74 // Check whether we've reached the minimum required number of repetitons:
75 // if (repeatCount >= minimumRepetitions)
78 // Check whether we've been asked to stop:
79 if (checkRepeatFlag())
81 QMutexLocker cifLocker(&commandIFMutex);
82 commandInFlight = false;
90 catch (PIRException e)
93 emit commandFailed(e.getError().c_str());
96 QMutexLocker cifLocker(&commandIFMutex);
97 commandInFlight = false;
101 int SIRCProtocol::generateStandardCommand(
102 const PIRKeyBits &pkb,
103 PIRRX51Hardware &rx51device)
107 // First, the "header" pulse:
108 rx51device.addPair(headerPulse, headerSpace);
109 duration += (headerPulse + headerSpace);
111 // Next, push the data.
112 // These bits are sent in reverse order, and moreover, the codes are sent
113 // in reverse order as well:
114 duration += pushReverseBits(pkb.thirdCode, rx51device);
115 duration += pushReverseBits(pkb.secondCode, rx51device);
116 duration += pushReverseBits(pkb.firstCode, rx51device);