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 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)
67 commandDuration = generateRepeatCommand((*i).second, rx51device);
71 commandDuration = generateStandardCommand((*i).second, rx51device);
74 // Now, tell the device to send the whole command:
75 rx51device.sendCommandToDevice();
77 // sleep until the next repetition of command:
78 sleepUntilRepeat(commandDuration);
80 // Check whether we've reached the minimum required number of repetitons:
81 if (repeatCount >= minimumRepetitions)
83 // Check whether we've been asked to stop:
84 if (checkRepeatFlag())
86 QMutexLocker cifLocker(&commandIFMutex);
87 commandInFlight = false;
95 catch (PIRException e)
98 emit commandFailed(e.getError().c_str());
101 QMutexLocker cifLocker(&commandIFMutex);
102 commandInFlight = false;
106 int KathreinProtocol::generateStandardCommand(
107 const PIRKeyBits &pkb,
108 PIRRX51Hardware &rx51device)
112 // First, the "header" pulse:
113 rx51device.addPair(headerPulse, headerSpace);
114 duration += (headerPulse + headerSpace);
116 // Kathrein protocol has four bits of address and eight bits of command.
117 // As in NEC protocol, the address and command are complemented.
118 // - "preData" should contain the 4-bit address
119 // - "firstCode" should contain the 8-bit command
120 duration += pushReverseBits(preData, rx51device);
121 duration += pushInvertedReverseBits(preData, rx51device);
122 duration += pushReverseBits(pkb.firstCode, rx51device);
123 duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
125 // Finally add the "trail":
126 rx51device.addSingle(trailerPulse);
127 duration += trailerPulse;
133 int KathreinProtocol::generateRepeatCommand(
134 const PIRKeyBits &pkb,
135 PIRRX51Hardware &rx51device)
139 // First, the "header" pulse:
140 rx51device.addPair(headerPulse, headerSpace);
141 duration += (headerPulse + headerSpace);
143 // The Kathrein repeat block contains the 8-bit command and nothing else:
144 duration += pushReverseBits(pkb.firstCode, rx51device);
146 // Finally add the "trail":
147 rx51device.addSingle(trailerPulse);
148 duration += trailerPulse;