1 #include "sircprotocol.h"
3 #include "pirexception.h"
7 // Some global communications stuff:
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
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.
20 SIRCProtocol::SIRCProtocol(
31 setCarrierFrequency(40000);
36 void SIRCProtocol::startSendingCommand(
37 unsigned int threadableID,
40 // Exceptions here are problematic; I'll try to weed them out by putting the
41 // whole thing in a try/catch block:
44 // First, check if we are meant to be the recipient of this command:
45 if (threadableID != id) return;
49 KeycodeCollection::const_iterator i = keycodes.find(command);
51 // Do we even have this key defined?
52 if (i == keycodes.end())
54 std::string s = "Tried to send a non-existent command.\n";
55 throw PIRException(s);
58 // construct the device:
59 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
62 int commandDuration = 0;
63 while (repeatCount < MAX_REPEAT_COUNT)
65 commandDuration = generateStandardCommand((*i).second, rx51device);
67 // Now, tell the device to send the whole command:
68 rx51device.sendCommandToDevice();
70 // sleep until the next repetition of command:
71 sleepUntilRepeat(commandDuration);
73 // Check whether we've reached the minimum required number of repetitons:
74 // if (repeatCount >= minimumRepetitions)
77 // Check whether we've been asked to stop:
78 if (checkRepeatFlag())
80 QMutexLocker cifLocker(&commandIFMutex);
81 commandInFlight = false;
89 catch (PIRException e)
92 emit commandFailed(e.getError().c_str());
95 QMutexLocker cifLocker(&commandIFMutex);
96 commandInFlight = false;
100 int SIRCProtocol::generateStandardCommand(
101 const PIRKeyBits &pkb,
102 PIRRX51Hardware &rx51device)
106 // First, the "header" pulse:
107 rx51device.addPair(headerPulse, headerSpace);
108 duration += (headerPulse + headerSpace);
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);