Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recevier improvements - setReceiveUsingProtocolTiming() and setReceiveUnknownProtocol() #418

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion RCSwitch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand All @@ -694,6 +729,9 @@ void RECEIVE_ATTR RCSwitch::handleInterrupt() {
break;
}
}
if (RCSwitch::receiveUnknownProtocol && RCSwitch::nReceivedValue == 0) {
acceptUnknownProtocol(changeCount);
}
repeatCount = 0;
}
}
Expand Down
5 changes: 5 additions & 0 deletions RCSwitch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions examples/ReceiveDemo_Advanced/ReceiveDemo_Advanced.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down