63e165852c08bf6be43c17b5f3ddc1a0bb0b2f29
[pierogi] / protocols / boseprotocol.cpp
1 #include "boseprotocol.h"
2
3 #include "pirrx51hardware.h"
4
5 #include "pirexception.h"
6
7 // Some global communications stuff:
8 #include <QMutex>
9 extern bool commandInFlight;
10 extern QMutex commandIFMutex;
11
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.
19
20 BoseProtocol::BoseProtocol(
21   QObject *guiObject,
22   unsigned int index)
23   : SpaceProtocol(
24       guiObject, index,
25       500, 500,
26       500, 1500,
27       1000, 1500,
28       500,
29       50000, false)
30 {
31 }
32
33
34 void BoseProtocol::startSendingCommand(
35   unsigned int threadableID,
36   PIRKeyName command)
37 {
38   // Exceptions here are problematic; I'll try to weed them out by putting the
39   // whole thing in a try/catch block:
40   try
41   {
42     // First, check if we are meant to be the recipient of this command:
43     if (threadableID != id) return;
44
45     clearRepeatFlag();
46
47     KeycodeCollection::const_iterator i = keycodes.find(command);
48
49     // Do we even have this key defined?
50     if (i == keycodes.end())
51     {
52       std::string s = "Tried to send a non-existent command.\n";
53       throw PIRException(s);
54     }
55
56     // construct the device:
57     PIRRX51Hardware rx51device(carrierFrequency, dutyCycle);
58
59     int repeatCount = 0;
60     int commandDuration = 0;
61     while (repeatCount < MAX_REPEAT_COUNT)
62     {
63       commandDuration = generateStandardCommand((*i).second, rx51device);
64
65       // Now, tell the device to send the whole command:
66       rx51device.sendCommandToDevice();
67
68       // sleep until the next repetition of command:
69       sleepUntilRepeat(commandDuration);
70
71       // Check whether we've reached the minimum required number of repetitons:
72       if (repeatCount >= minimumRepetitions)
73       {
74         // Check whether we've been asked to stop:
75         if (checkRepeatFlag())
76         {
77           QMutexLocker cifLocker(&commandIFMutex);
78           commandInFlight = false;
79           return;
80         }
81       }
82
83       ++repeatCount;
84     }
85   }
86   catch (PIRException e)
87   {
88     // inform the gui:
89     emit commandFailed(e.getError().c_str());
90   }
91
92   QMutexLocker cifLocker(&commandIFMutex);
93   commandInFlight = false;
94 }
95
96
97 int BoseProtocol::generateStandardCommand(
98   const PIRKeyBits &pkb,
99   PIRRX51Hardware &rx51device)
100 {
101   int duration = 0;
102
103   // First, the "header" pulse:
104   rx51device.addPair(headerPulse, headerSpace);
105   duration += (headerPulse + headerSpace);
106
107   // The Bose protocol uses 1/2 of the NEC protocol; it has the command
108   // portion, but no device address portion.  So, we only need to reverse
109   // the command, then invert and reverse the command:
110   duration += pushReverseBits(pkb.firstCode, rx51device);
111   duration += pushInvertedReverseBits(pkb.firstCode, rx51device);
112
113   // Finally add the "trail":
114   rx51device.addSingle(trailerPulse);
115   duration += trailerPulse;
116
117   return duration;
118 }
119