1 #include "daewooprotocol.h"
3 #include "pirrx51hardware.h"
5 #include "pirexception.h"
7 // Some global communications stuff:
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
12 // Daewoo is using a protocol similar to NEC, but with the distinction that
13 // it does not send inverted copies of the address and command, and it places
14 // a 550 usec pulse / 2000 usec space between the address and the command.
15 // Here are the fine details:
16 // A "zero" is encoded with a 550 usec pulse, 450 usec space.
17 // A "one" is encoded with a 550 usec pulse, and 1450 usec space.
18 // The header is a 8000 usec pulse, 4000 usec space.
19 // There is a mid-way marker of 550 usec pulse, 2000 usec space.
20 // The pulse train ends with a trailing 550 usec pulse.
21 // The entire train is resent for repeats.
22 // Each command runs for 60000 usec before another can be executed.
23 // The carrier frequency is 38 kHz.
25 DaewooProtocol::DaewooProtocol(
41 void DaewooProtocol::startSendingCommand(
42 unsigned int threadableID,
45 // Exceptions here are problematic; I'll try to weed them out by putting the
46 // whole thing in a try/catch block:
49 // First, check if we are meant to be the recipient of this command:
50 if (threadableID != id) return;
54 KeycodeCollection::const_iterator i = keycodes.find(command);
56 // Do we even have this key defined?
57 if (i == keycodes.end())
59 QMutexLocker cifLocker(&commandIFMutex);
60 commandInFlight = false;
62 // std::string s = "Tried to send a non-existent command.\n";
63 // throw PIRException(s);
66 // construct the device:
67 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
70 int commandDuration = 0;
71 while (repeatCount < MAX_REPEAT_COUNT)
73 commandDuration = generateStandardCommand((*i).second, rx51device);
75 // Now, tell the device to send the whole command:
76 rx51device.sendCommandToDevice();
78 // sleep until the next repetition of command:
79 sleepUntilRepeat(commandDuration);
81 // Check whether we've reached the minimum required number of repetitons:
82 if (repeatCount >= minimumRepetitions)
84 // Check whether we've been asked to stop:
85 if (checkRepeatFlag())
89 QMutexLocker cifLocker(&commandIFMutex);
90 commandInFlight = false;
99 QMutexLocker cifLocker(&commandIFMutex);
100 commandInFlight = false;
102 catch (PIRException e)
105 emit commandFailed(e.getError().c_str());
110 int DaewooProtocol::generateStandardCommand(
111 const PIRKeyBits &pkb,
112 PIRRX51Hardware &rx51device)
116 // First, the "header" pulse:
117 rx51device.addPair(headerPulse, headerSpace);
118 duration += (headerPulse + headerSpace);
121 duration += pushReverseBits(preData, rx51device);
123 // The Daewoo mid-train marker:
124 rx51device.addPair(midPulse, midSpace);
125 duration += (midPulse + midSpace);
128 duration += pushReverseBits(pkb.firstCode, rx51device);
130 // Finally add the "trail":
131 rx51device.addSingle(trailerPulse);
132 duration += trailerPulse;