1 #include "protonprotocol.h"
3 #include "pirrx51hardware.h"
5 #include "pirexception.h"
7 // Some global communications stuff:
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
12 // The proton protocol seems fairly similar to the NEC protocol, although
13 // somewhat simplified.
14 // A "zero" is encoded with a 500 usec pulse, 500 usec space.
15 // A "one" is encoded with a 500 usec pulse, and 3*500 (1500) usec space.
16 // The header is a 8000 usec pulse, 4000 usec space.
17 // Commands end with a trailing 500 usec pulse.
18 // When repeating, the entire pulse-train is retransmitted.
19 // The duration of each full frame is 63000 usec.
20 // The normal carrier frequency is 38 kHz.
22 ProtonProtocol::ProtonProtocol(
36 void ProtonProtocol::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 QMutexLocker cifLocker(&commandIFMutex);
55 commandInFlight = false;
57 // std::string s = "Tried to send a non-existent command.\n";
58 // throw PIRException(s);
61 // construct the device:
62 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
65 int commandDuration = 0;
66 while (repeatCount < MAX_REPEAT_COUNT)
68 commandDuration = generateStandardCommand((*i).second, rx51device);
70 // Now, tell the device to send the whole command:
71 rx51device.sendCommandToDevice();
73 // sleep until the next repetition of command:
74 sleepUntilRepeat(commandDuration);
76 // Check whether we've reached the minimum required number of repetitons:
77 if (repeatCount >= minimumRepetitions)
79 // Check whether we've been asked to stop:
80 if (checkRepeatFlag())
84 QMutexLocker cifLocker(&commandIFMutex);
85 commandInFlight = false;
94 QMutexLocker cifLocker(&commandIFMutex);
95 commandInFlight = false;
97 catch (PIRException e)
100 emit commandFailed(e.getError().c_str());
105 int ProtonProtocol::generateStandardCommand(
106 const PIRKeyBits &pkb,
107 PIRRX51Hardware &rx51device)
111 // First, the "header" pulse:
112 rx51device.addPair(headerPulse, headerSpace);
113 duration += (headerPulse + headerSpace);
115 // The Proton protocol contains an 8 bit address and an 8 bit command,
116 // in LSB order. Between these two is a gap made up of a 500 usec pulse
117 // and a 4000 usec space.
118 // - "preData" should contain the address.
119 // - "firstCode" should contain the command.
121 duration += pushReverseBits(preData, rx51device);
123 rx51device.addPair(500, 4000);
126 duration += pushReverseBits(pkb.firstCode, rx51device);
128 // Finally add the "trail":
129 rx51device.addSingle(trailerPulse);
130 duration += trailerPulse;