1 #include "thomsonprotocol.h"
3 #include "pirrx51hardware.h"
5 #include "pirexception.h"
7 // Some global communications stuff:
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
12 // This Thomson protocol is a very simple one:
13 // A "zero" is encoded with a 500 usec pulse, 2000 usec space.
14 // A "one" is encoded with a 500 usec pulse, and 4500 usec space.
15 // There is no header.
16 // Commands end with a trailing 500 usec pulse.
17 // When repeating, the full pulse train is re-sent.
18 // Each command runs for 80000 usec total.
19 // The carrier frequency should be 33 kHz, but I'm not so sure...
21 ThomsonProtocol::ThomsonProtocol(
36 void ThomsonProtocol::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())
85 QMutexLocker cifLocker(&commandIFMutex);
86 commandInFlight = false;
96 QMutexLocker cifLocker(&commandIFMutex);
97 commandInFlight = false;
99 catch (PIRException e)
102 emit commandFailed(e.getError().c_str());
107 int ThomsonProtocol::generateStandardCommand(
108 const PIRKeyBits &pkb,
109 PIRRX51Hardware &rx51device)
113 // First, four bits of address:
114 duration += pushBits(preData, rx51device);
116 // Next, the toggle bit:
117 if (keypressCount % 2)
119 rx51device.addPair(onePulse, oneSpace);
120 duration += (onePulse + oneSpace);
124 rx51device.addPair(zeroPulse, zeroSpace);
125 duration += (zeroPulse + zeroSpace);
128 // Next, seven bits of command:
129 duration += pushBits(pkb.firstCode, rx51device);
131 // Finally add the "trail":
132 rx51device.addSingle(trailerPulse);
133 duration += trailerPulse;