Improved to "half-way usable" (version 0.5)
[pierogi] / pirprotocol.cpp
index f3b8ff1..ddacbfd 100644 (file)
 extern bool stopRepeatingFlag;
 extern QMutex stopRepeatingMutex;
 
+// Total of all running commands
+extern bool commandInFlight;
+extern QMutex commandIFMutex;
+
 // From what I understand (mostly from reading LIRC config files), NEC
 // protocol based remotes mostly use a frequency of 38000 units and a
 // duty cycle of 50%.  They'll be set to these defaults here, and overridden
@@ -50,19 +54,80 @@ PIRProtocol::PIRProtocol(
 void PIRProtocol::addKey(
   PIRKeyName key,
   unsigned long command,
-  unsigned int bits)
+  unsigned int size)
 {
-  appendToBitSeq(keycodes[key], command, bits);
+  // First, if key already exists, clear it out:
+  KeycodeCollection::iterator i = keycodes.find(key);
+  if (i != keycodes.end())
+  {
+    i->second.clear();
+  }
+
+  appendToBitSeq(keycodes[key], command, size);
 }
 
 
-/*
-void PIRProtocol::setIndex(
-  unsigned int i)
+void PIRProtocol::addSIRCKey(
+  PIRKeyName key,
+  unsigned int addressData,
+  unsigned int size,
+  unsigned int commandData)
 {
-  id = i;
+  // First, if key already exists, clear it out:
+  KeycodeCollection::iterator i = keycodes.find(key);
+  if (i != keycodes.end())
+  {
+    i->second.clear();
+  }
+
+  // First, append the address data:
+  appendToBitSeq(keycodes[key], addressData, size);
+
+  // Next, the command data.  The size is always 7 bits:
+  appendToBitSeq(keycodes[key], commandData, 7);
+}
+
+
+void PIRProtocol::addSIRC20Key(
+  PIRKeyName key,
+  unsigned int secondaryAddressData,
+  unsigned int primaryAddressData,
+  unsigned int commandData)
+{
+  // First, if key already exists, clear it out:
+  KeycodeCollection::iterator i = keycodes.find(key);
+  if (i != keycodes.end())
+  {
+    i->second.clear();
+  }
+
+  // First, append the secondary address data:
+  appendToBitSeq(keycodes[key], secondaryAddressData, 8);
+
+  // Next, the primary address data:
+  appendToBitSeq(keycodes[key], primaryAddressData, 5);
+
+  // Next, the command data.  The size is always 7 bits:
+  appendToBitSeq(keycodes[key], commandData, 7);
+}
+
+
+void PIRProtocol::addSharpKey(
+  PIRKeyName key,
+  unsigned int addressData,
+  unsigned int commandData)
+{
+  // First, if key already exists, clear it out:
+  KeycodeCollection::iterator i = keycodes.find(key);
+  if (i != keycodes.end())
+  {
+    i->second.clear();
+  }
+
+  // Sharp commands are all 5 bit address, 8 bit command:
+  appendToBitSeq(keycodes[key], addressData, 5);
+  appendToBitSeq(keycodes[key], commandData, 8);
 }
-*/
 
 
 void PIRProtocol::setCarrierFrequency(
@@ -86,6 +151,34 @@ void PIRProtocol::setMinimumRepetitions(
 }
 
 
+void PIRProtocol::setPreData(
+  unsigned long data,
+  unsigned int bits)
+{
+  // If the container is not empty, first clear it out:
+  if (!preData.empty())
+  {
+    preData.clear();
+  }
+
+  appendToBitSeq(preData, data, bits);
+}
+
+
+void PIRProtocol::setPostData(
+  unsigned long data,
+  unsigned int bits)
+{
+  // If the container is not empty, first clear it out:
+  if (!postData.empty())
+  {
+    postData.clear();
+  }
+
+  appendToBitSeq(postData, data, bits);
+}
+
+
 bool PIRProtocol::isCommandSupported(
   PIRKeyName command)
 {
@@ -96,9 +189,9 @@ bool PIRProtocol::isCommandSupported(
 void PIRProtocol::appendToBitSeq(
   CommandSequence &sequence,
   unsigned int bits,
-  int significantBits)
+  int size)
 {
-  if (significantBits == 0)
+  if (size == 0)
   {
     // This is bad, but just return silently for now...
     return;
@@ -106,7 +199,7 @@ void PIRProtocol::appendToBitSeq(
 
   // For each bit in the char, append a 1 or a 0 into the sequence.
   // Starting with the largest bit, move forward one bit at a time:
-  unsigned int currentBit = 1 << (significantBits - 1);
+  unsigned int currentBit = 1 << (size - 1);
 
   do
   {
@@ -166,11 +259,19 @@ void PIRProtocol::sleepUntilRepeat(
     microseconds = gap - PIEROGI_OVERHEAD_HACK;
   }
 
+/*
   // Don't even bother sleeping if there's only a few microseconds:
   if (microseconds < 1000)
   {
     return;
   }
+*/
+  // For now, I'm going to enforce a minimum sleep of 10 ms, so that we
+  // don't get runaway commands:
+  if (microseconds < 10000)
+  {
+    microseconds = 10000;
+  }
 
   timespec sleeptime;
   sleeptime.tv_sec = 0;