1 #include "boseprotocol.h"
3 #include "pirrx51hardware.h"
5 #include "pirexception.h"
7 // Some global communications stuff:
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
12 // Here's what I've been able to scrounge up on the Bose protocol:
13 // A "zero" is encoded with a 500 usec pulse, 500 usec space.
14 // A "one" is encoded with a 500 usec pulse, and 3*500 (1500) usec space.
15 // The header is a 1000 usec pulse, 1500 usec space.
16 // Commands end with a trailing 500 usec pulse.
17 // When repeating, the entire command is resent.
18 // A gap of 50000 usec is placed between repeated commands.
20 BoseProtocol::BoseProtocol(
34 void BoseProtocol::startSendingCommand(
35 unsigned int threadableID,
38 // Exceptions here are problematic; I'll try to weed them out by putting the
39 // whole thing in a try/catch block:
42 // First, check if we are meant to be the recipient of this command:
43 if (threadableID != id) return;
47 KeycodeCollection::const_iterator i = keycodes.find(command);
49 // Do we even have this key defined?
50 if (i == keycodes.end())
52 QMutexLocker cifLocker(&commandIFMutex);
53 commandInFlight = false;
55 // std::string s = "Tried to send a non-existent command.\n";
56 // throw PIRException(s);
59 // construct the device:
60 PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
63 int commandDuration = 0;
64 while (repeatCount < MAX_REPEAT_COUNT)
66 commandDuration = generateStandardCommand((*i).second, rx51device);
68 // Now, tell the device to send the whole command:
69 rx51device.sendCommandToDevice();
71 // sleep until the next repetition of command:
72 sleepUntilRepeat(commandDuration);
74 // Check whether we've reached the minimum required number of repetitons:
75 if (repeatCount >= minimumRepetitions)
77 // Check whether we've been asked to stop:
78 if (checkRepeatFlag())
82 QMutexLocker cifLocker(&commandIFMutex);
83 commandInFlight = false;
92 QMutexLocker cifLocker(&commandIFMutex);
93 commandInFlight = false;
95 catch (PIRException e)
98 emit commandFailed(e.getError().c_str());
103 int BoseProtocol::generateStandardCommand(
104 const PIRKeyBits &pkb,
105 PIRRX51Hardware &rx51device)
109 // First, the "header" pulse:
110 rx51device.addPair(headerPulse, headerSpace);
111 duration += (headerPulse + headerSpace);
113 // The Bose protocol uses 1/2 of the NEC protocol; it has the command
114 // portion, but no device address portion. So, we only need to reverse
115 // the command, then invert and reverse the command:
116 duration += pushReverseBits(pkb.firstCode, rx51device);
117 duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
119 // Finally add the "trail":
120 rx51device.addSingle(trailerPulse);
121 duration += trailerPulse;