diff --git a/RCSwitch.cpp b/RCSwitch.cpp index 99d3cc1..c94d47f 100644 --- a/RCSwitch.cpp +++ b/RCSwitch.cpp @@ -103,6 +103,9 @@ volatile unsigned int RCSwitch::nReceivedBitlength = 0; volatile unsigned int RCSwitch::nReceivedDelay = 0; volatile unsigned int RCSwitch::nReceivedProtocol = 0; int RCSwitch::nReceiveTolerance = 60; +bool RCSwitch::receiveUsingProtocolTiming = false; +bool RCSwitch::receiveUnknownProtocol = false; + const unsigned int VAR_ISR_ATTR RCSwitch::nSeparationLimit = 4300; // separationLimit: minimum microseconds between received codes, closer codes are ignored. // according to discussion on issue #14 it might be more suitable to set the separation @@ -117,6 +120,8 @@ RCSwitch::RCSwitch() { #if not defined( RCSwitchDisableReceiving ) this->nReceiverInterrupt = -1; this->setReceiveTolerance(60); + this->setReceiveUsingProtocolTiming(false); + this->setReceiveUnknownProtocol(false); RCSwitch::nReceivedValue = 0; #endif } @@ -172,6 +177,20 @@ void RCSwitch::setRepeatTransmit(int nRepeatTransmit) { void RCSwitch::setReceiveTolerance(int nPercent) { RCSwitch::nReceiveTolerance = nPercent; } + +/** + * Set source of recive protocol delay + */ +void RCSwitch::setReceiveUsingProtocolTiming(bool useProtocolTiming) { + RCSwitch::receiveUsingProtocolTiming = useProtocolTiming; +} + +/** + * Enable receiving of unknown protocol data - for debugging purposes only + */ +void RCSwitch::setReceiveUnknownProtocol(bool showUnknownProtocol) { + RCSwitch::receiveUnknownProtocol = showUnknownProtocol; +} #endif @@ -620,7 +639,7 @@ bool RECEIVE_ATTR RCSwitch::receiveProtocol(const int p, unsigned int changeCoun unsigned long code = 0; //Assuming the longer pulse length is the pulse captured in timings[0] const unsigned int syncLengthInPulses = ((pro.syncFactor.low) > (pro.syncFactor.high)) ? (pro.syncFactor.low) : (pro.syncFactor.high); - const unsigned int delay = RCSwitch::timings[0] / syncLengthInPulses; + const unsigned int delay = RCSwitch::receiveUsingProtocolTiming ? pro.pulseLength : RCSwitch::timings[0] / syncLengthInPulses; const unsigned int delayTolerance = delay * RCSwitch::nReceiveTolerance / 100; /* For protocols that start low, the sync period looks like @@ -668,8 +687,24 @@ bool RECEIVE_ATTR RCSwitch::receiveProtocol(const int p, unsigned int changeCoun return false; } +void RECEIVE_ATTR RCSwitch::acceptUnknownProtocol(unsigned int changeCount) { + + if (changeCount > 7) { // ignore very short transmissions: no device sends them, so this must be noise + RCSwitch::nReceivedValue = -1; + RCSwitch::nReceivedBitlength = (changeCount - 1) / 2; + RCSwitch::nReceivedDelay = 0; + RCSwitch::nReceivedProtocol = -1; + } +} + void RECEIVE_ATTR RCSwitch::handleInterrupt() { + // The timings table should remain unchanged + // until previously received data is consumed + if (RCSwitch::receiveUnknownProtocol && RCSwitch::nReceivedValue != 0) { + return; + } + static unsigned int changeCount = 0; static unsigned long lastTime = 0; static unsigned int repeatCount = 0; @@ -694,6 +729,9 @@ void RECEIVE_ATTR RCSwitch::handleInterrupt() { break; } } + if (RCSwitch::receiveUnknownProtocol && RCSwitch::nReceivedValue == 0) { + acceptUnknownProtocol(changeCount); + } repeatCount = 0; } } diff --git a/RCSwitch.h b/RCSwitch.h index b7755e0..b528604 100644 --- a/RCSwitch.h +++ b/RCSwitch.h @@ -100,6 +100,8 @@ class RCSwitch { void setRepeatTransmit(int nRepeatTransmit); #if not defined( RCSwitchDisableReceiving ) void setReceiveTolerance(int nPercent); + void setReceiveUsingProtocolTiming(bool useProtocolTiming); + void setReceiveUnknownProtocol(bool showUnknownProtocol); #endif /** @@ -158,6 +160,7 @@ class RCSwitch { #if not defined( RCSwitchDisableReceiving ) static void handleInterrupt(); static bool receiveProtocol(const int p, unsigned int changeCount); + static void acceptUnknownProtocol(unsigned int changeCount); int nReceiverInterrupt; #endif int nTransmitterPin; @@ -167,6 +170,8 @@ class RCSwitch { #if not defined( RCSwitchDisableReceiving ) static int nReceiveTolerance; + static bool receiveUsingProtocolTiming; + static bool receiveUnknownProtocol; volatile static unsigned long nReceivedValue; volatile static unsigned int nReceivedBitlength; volatile static unsigned int nReceivedDelay; diff --git a/examples/ReceiveDemo_Advanced/ReceiveDemo_Advanced.ino b/examples/ReceiveDemo_Advanced/ReceiveDemo_Advanced.ino index 18380d3..bfb59b4 100644 --- a/examples/ReceiveDemo_Advanced/ReceiveDemo_Advanced.ino +++ b/examples/ReceiveDemo_Advanced/ReceiveDemo_Advanced.ino @@ -14,6 +14,12 @@ RCSwitch mySwitch = RCSwitch(); void setup() { Serial.begin(9600); mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2 + // for some transmitters it gives better results when you set it to true + // when it is false library is using initial synchronization pulse to determine timing for decoding signal + // when it is true library is using protocol definition to determine timing for decoding signal + mySwitch.setReceiveUsingProtocolTiming(false); + // this is for testing only - it will show timings of received data even, if it doesn't follow any protocol. + mySwitch.setReceiveUnknownProtocol(true); } void loop() {