Skip to content

Instantly share code, notes, and snippets.

@ato
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ato/b912fdc8221c62ccf67f to your computer and use it in GitHub Desktop.
Save ato/b912fdc8221c62ccf67f to your computer and use it in GitHub Desktop.
RCSwitch receive bug
// "unsigned long" is 64-bit on x86_64 so changed to uint32_t to test this like it would be on a 32-bit platform
// commented out the delay logic so that it thinks it's just received all 1s
//
// output:
// $ g++ buggy.cpp -o buggy && ./buggy
// 7fffffff
#include <stdio.h>
#include <stdint.h>
uint32_t receiveProtocol1(unsigned int changeCount){
uint32_t /* unsigned long */ code = 0;
//unsigned long delay = RCSwitch::timings[0] / 31;
//unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance * 0.01;
for (int i = 1; i<changeCount ; i=i+2) {
if (0 /* RCSwitch::timings[i] > delay-delayTolerance && RCSwitch::timings[i] < delay+delayTolerance && RCSwitch::timings[i+1] > delay*3-delayTolerance && RCSwitch::timings[i+1] < delay*3+delayTolerance */) {
code = code << 1;
} else if (1 /*RCSwitch::timings[i] > delay*3-delayTolerance && RCSwitch::timings[i] < delay*3+delayTolerance && RCSwitch::timings[i+1] > delay-delayTolerance && RCSwitch::timings[i+1] < delay+delayTolerance*/) {
code+=1;
code = code << 1;
} else {
// Failed
i = changeCount;
code = 0;
}
}
code = code >> 1;
return code;
/*
if (changeCount > 6) { // ignore < 4bit values as there are no devices sending 4bit values => noise
RCSwitch::nReceivedValue = code;
RCSwitch::nReceivedBitlength = changeCount / 2;
RCSwitch::nReceivedDelay = delay;
RCSwitch::nReceivedProtocol = 1;
}
if (code == 0){
return false;
}else if (code != 0){
return true;
}
*/
}
int main() {
printf("%x\n", receiveProtocol1(65));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment