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 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())
80 QMutexLocker cifLocker(&commandIFMutex);
81 commandInFlight = false;
89 catch (PIRException e)
92 emit commandFailed(e.getError().c_str());
96 QMutexLocker cifLocker(&commandIFMutex);
97 commandInFlight = false;
101 int ThomsonProtocol::generateStandardCommand(
102 const PIRKeyBits &pkb,
103 PIRRX51Hardware &rx51device)
107 // First, four bits of address:
108 duration += pushBits(preData, rx51device);
110 // Next, the toggle bit:
111 if (keypressCount % 2)
113 rx51device.addPair(onePulse, oneSpace);
114 duration += (onePulse + oneSpace);
118 rx51device.addPair(zeroPulse, zeroSpace);
119 duration += (zeroPulse + zeroSpace);
122 // Next, seven bits of command:
123 duration += pushBits(pkb.firstCode, rx51device);
125 // Finally add the "trail":
126 rx51device.addSingle(trailerPulse);
127 duration += trailerPulse;