1 #include "kathreinprotocol.h"
3 #include "pirrx51hardware.h"
5 #include "pirexception.h"
7 // Some global communications stuff:
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
12 // The Kathrein protocol is similar to the NEC protocol:
13 // A "zero" is encoded with a 560 usec pulse, 560 usec space.
14 // A "one" is encoded with a 560 usec pulse, and 3*560 (1680) usec space.
15 // The header is a 9000 usec pulse, 4500 usec space.
16 // Commands end with a trailing 560 usec pulse.
17 // The repeat block is unique -- it has the normal header and trailer, and
18 // includes the command code, but no address, and no complement.
19 // Each command runs for 110000 usec before another can be executed.
20 // The normal carrier frequency is 38 kHz.
22 KathreinProtocol::KathreinProtocol(
36 void KathreinProtocol::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)
70 commandDuration = generateRepeatCommand((*i).second, rx51device);
74 commandDuration = generateStandardCommand((*i).second, rx51device);
77 // Now, tell the device to send the whole command:
78 rx51device.sendCommandToDevice();
80 // sleep until the next repetition of command:
81 sleepUntilRepeat(commandDuration);
83 // Check whether we've reached the minimum required number of repetitons:
84 if (repeatCount >= minimumRepetitions)
86 // Check whether we've been asked to stop:
87 if (checkRepeatFlag())
91 QMutexLocker cifLocker(&commandIFMutex);
92 commandInFlight = false;
101 QMutexLocker cifLocker(&commandIFMutex);
102 commandInFlight = false;
104 catch (PIRException e)
107 emit commandFailed(e.getError().c_str());
112 int KathreinProtocol::generateStandardCommand(
113 const PIRKeyBits &pkb,
114 PIRRX51Hardware &rx51device)
118 // First, the "header" pulse:
119 rx51device.addPair(headerPulse, headerSpace);
120 duration += (headerPulse + headerSpace);
122 // Kathrein protocol has four bits of address and eight bits of command.
123 // As in NEC protocol, the address and command are complemented.
124 // - "preData" should contain the 4-bit address
125 // - "firstCode" should contain the 8-bit command
126 duration += pushReverseBits(preData, rx51device);
127 duration += pushInvertedReverseBits(preData, rx51device);
128 duration += pushReverseBits(pkb.firstCode, rx51device);
129 duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
131 // Finally add the "trail":
132 rx51device.addSingle(trailerPulse);
133 duration += trailerPulse;
139 int KathreinProtocol::generateRepeatCommand(
140 const PIRKeyBits &pkb,
141 PIRRX51Hardware &rx51device)
145 // First, the "header" pulse:
146 rx51device.addPair(headerPulse, headerSpace);
147 duration += (headerPulse + headerSpace);
149 // The Kathrein repeat block contains the 8-bit command and nothing else:
150 duration += pushReverseBits(pkb.firstCode, rx51device);
152 // Finally add the "trail":
153 rx51device.addSingle(trailerPulse);
154 duration += trailerPulse;