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 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)
76 // Check whether we've been asked to stop:
77 if (checkRepeatFlag())
79 QMutexLocker cifLocker(&commandIFMutex);
80 commandInFlight = false;
88 catch (PIRException e)
91 emit commandFailed(e.getError().c_str());
94 QMutexLocker cifLocker(&commandIFMutex);
95 commandInFlight = false;
99 int ProtonProtocol::generateStandardCommand(
100 const PIRKeyBits &pkb,
101 PIRRX51Hardware &rx51device)
105 // First, the "header" pulse:
106 rx51device.addPair(headerPulse, headerSpace);
107 duration += (headerPulse + headerSpace);
109 // The Proton protocol contains an 8 bit address and an 8 bit command,
110 // in LSB order. Between these two is a gap made up of a 500 usec pulse
111 // and a 4000 usec space.
112 // - "preData" should contain the address.
113 // - "firstCode" should contain the command.
115 duration += pushReverseBits(preData, rx51device);
117 rx51device.addPair(500, 4000);
120 duration += pushReverseBits(pkb.firstCode, rx51device);
122 // Finally add the "trail":
123 rx51device.addSingle(trailerPulse);
124 duration += trailerPulse;