Skip to content
Snippets Groups Projects
Commit 94642ad7 authored by Max Horn's avatar Max Horn
Browse files

Introduce new Protocol struct

This clarifies the code. It also reduces the code size
by another 1000 bytes or so.
parent 147b9577
No related branches found
No related tags found
No related merge requests found
...@@ -29,18 +29,17 @@ ...@@ -29,18 +29,17 @@
#include "RCSwitch.h" #include "RCSwitch.h"
enum { static const RCSwitch::Protocol proto[] = {
PROTOCOL1_SYNC_FACTOR = 31, { -1, { 0, 0 }, { 0, 0 }, { 0, 0 } }, // dummy protocol
{ 350, { 1, 31 }, { 1, 3 }, { 3, 1 } }, // protocol 1
PROTOCOL2_SYNC_FACTOR = 10, { 650, { 1, 10 }, { 1, 2 }, { 2, 1 } }, // protocol 2
{ 100, { 1, 71 }, { 4, 11 }, { 9, 6 } }, // protocol 3
PROTOCOL3_SYNC_FACTOR = 71, { 380, { 1, 6 }, { 1, 3 }, { 3, 1 } }, // protocol 4
PROTOCOL3_0_HIGH_CYCLES = 4, { 500, { 6, 14 }, { 1, 2 }, { 2, 1 } }, // protocol 5
PROTOCOL3_0_LOW_CYCLES = 11,
PROTOCOL3_1_HIGH_CYCLES = 9,
PROTOCOL3_1_LOW_CYCLES = 6
}; };
static const int numProto = sizeof(proto) / sizeof(proto[0]);
#if not defined( RCSwitchDisableReceiving ) #if not defined( RCSwitchDisableReceiving )
unsigned long RCSwitch::nReceivedValue = 0; unsigned long RCSwitch::nReceivedValue = 0;
unsigned int RCSwitch::nReceivedBitlength = 0; unsigned int RCSwitch::nReceivedBitlength = 0;
...@@ -56,7 +55,6 @@ unsigned int RCSwitch::timings[RCSWITCH_MAX_CHANGES]; ...@@ -56,7 +55,6 @@ unsigned int RCSwitch::timings[RCSWITCH_MAX_CHANGES];
RCSwitch::RCSwitch() { RCSwitch::RCSwitch() {
this->nTransmitterPin = -1; this->nTransmitterPin = -1;
this->setPulseLength(350);
this->setRepeatTransmit(10); this->setRepeatTransmit(10);
this->setProtocol(1); this->setProtocol(1);
#if not defined( RCSwitchDisableReceiving ) #if not defined( RCSwitchDisableReceiving )
...@@ -70,21 +68,23 @@ RCSwitch::RCSwitch() { ...@@ -70,21 +68,23 @@ RCSwitch::RCSwitch() {
* Sets the protocol to send. * Sets the protocol to send.
*/ */
void RCSwitch::setProtocol(int nProtocol) { void RCSwitch::setProtocol(int nProtocol) {
this->nProtocol = nProtocol; if (nProtocol <= 0 || nProtocol > numProto) {
switch (nProtocol) { this->nProtocol = 1; // TODO: trigger an error, e.g. "bad protocol" ???
case 1: this->setPulseLength(350); break; } else {
case 2: this->setPulseLength(650); break; this->nProtocol = nProtocol;
case 3: this->setPulseLength(100); break;
case 4: this->setPulseLength(380); break;
case 5: this->setPulseLength(500); break;
} }
this->setPulseLength(proto[nProtocol].pulseLength);
} }
/** /**
* Sets the protocol to send with pulse length in microseconds. * Sets the protocol to send with pulse length in microseconds.
*/ */
void RCSwitch::setProtocol(int nProtocol, int nPulseLength) { void RCSwitch::setProtocol(int nProtocol, int nPulseLength) {
this->nProtocol = nProtocol; if (nProtocol <= 0 || nProtocol > numProto) {
this->nProtocol = 1; // TODO: trigger an error, e.g. "bad protocol" ???
} else {
this->nProtocol = nProtocol;
}
this->setPulseLength(nPulseLength); this->setPulseLength(nPulseLength);
} }
...@@ -504,6 +504,11 @@ void RCSwitch::transmit(int nHighPulses, int nLowPulses) { ...@@ -504,6 +504,11 @@ void RCSwitch::transmit(int nHighPulses, int nLowPulses) {
#endif #endif
} }
} }
void RCSwitch::transmit(HighLow pulses) {
transmit(pulses.high, pulses.low);
}
/** /**
* Sends a "0" Bit * Sends a "0" Bit
* _ * _
...@@ -512,19 +517,7 @@ void RCSwitch::transmit(int nHighPulses, int nLowPulses) { ...@@ -512,19 +517,7 @@ void RCSwitch::transmit(int nHighPulses, int nLowPulses) {
* Waveform Protocol 2: | |__ * Waveform Protocol 2: | |__
*/ */
void RCSwitch::send0() { void RCSwitch::send0() {
switch (this->nProtocol) { this->transmit(proto[nProtocol].zero);
case 1:
case 4:
this->transmit(1,3);
break;
case 2:
case 5:
this->transmit(1,2);
break;
case 3:
this->transmit(PROTOCOL3_0_HIGH_CYCLES, PROTOCOL3_0_LOW_CYCLES);
break;
}
} }
/** /**
...@@ -535,19 +528,7 @@ void RCSwitch::send0() { ...@@ -535,19 +528,7 @@ void RCSwitch::send0() {
* Waveform Protocol 2: | |_ * Waveform Protocol 2: | |_
*/ */
void RCSwitch::send1() { void RCSwitch::send1() {
switch (this->nProtocol) { this->transmit(proto[nProtocol].one);
case 1:
case 4:
this->transmit(3,1);
break;
case 2:
case 5:
this->transmit(2,1);
break;
case 3:
this->transmit(PROTOCOL3_1_HIGH_CYCLES, PROTOCOL3_1_LOW_CYCLES);
break;
}
} }
...@@ -589,24 +570,7 @@ void RCSwitch::sendTF() { ...@@ -589,24 +570,7 @@ void RCSwitch::sendTF() {
* Waveform Protocol 2: | |__________ * Waveform Protocol 2: | |__________
*/ */
void RCSwitch::sendSync() { void RCSwitch::sendSync() {
this->transmit(proto[nProtocol].syncFactor);
switch (this->nProtocol) {
case 1:
this->transmit(1,PROTOCOL1_SYNC_FACTOR);
break;
case 2:
this->transmit(1,PROTOCOL2_SYNC_FACTOR);
break;
case 3:
this->transmit(1,PROTOCOL3_SYNC_FACTOR);
break;
case 4:
this->transmit(1,6);
break;
case 5:
this->transmit(6,14);
break;
}
} }
#if not defined( RCSwitchDisableReceiving ) #if not defined( RCSwitchDisableReceiving )
...@@ -670,84 +634,19 @@ static inline unsigned long diff(long A, long B) { ...@@ -670,84 +634,19 @@ static inline unsigned long diff(long A, long B) {
/** /**
* *
*/ */
bool RCSwitch::receiveProtocol1(unsigned int changeCount) { bool RCSwitch::receiveProtocol(const int p, unsigned int changeCount) {
unsigned long code = 0;
const unsigned long delay = RCSwitch::timings[0] / PROTOCOL1_SYNC_FACTOR;
const unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance / 100;
for (unsigned int i = 1; i < changeCount; i += 2) {
code <<= 1;
if (diff(RCSwitch::timings[i], delay) < delayTolerance &&
diff(RCSwitch::timings[i + 1], delay * 3) < delayTolerance) {
// zero
} else if (diff(RCSwitch::timings[i], delay * 3) < delayTolerance &&
diff(RCSwitch::timings[i + 1], delay) < delayTolerance) {
// one
code |= 1;
} else {
// Failed
return false;
}
}
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;
}
return true;
}
bool RCSwitch::receiveProtocol2(unsigned int changeCount) {
unsigned long code = 0;
const unsigned long delay = RCSwitch::timings[0] / PROTOCOL2_SYNC_FACTOR;
const unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance / 100;
for (unsigned int i = 1; i < changeCount; i += 2) {
code <<= 1;
if (diff(RCSwitch::timings[i], delay) < delayTolerance &&
diff(RCSwitch::timings[i + 1], delay * 2) < delayTolerance) {
// zero
} else if (diff(RCSwitch::timings[i], delay * 2) < delayTolerance &&
diff(RCSwitch::timings[i + 1], delay) < delayTolerance) {
// one
code |= 1;
} else {
// Failed
return false;
}
}
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 = 2;
}
return true;
}
/** Protocol 3 is used by BL35P02.
*
*/
bool RCSwitch::receiveProtocol3(unsigned int changeCount) {
unsigned long code = 0; unsigned long code = 0;
const unsigned long delay = RCSwitch::timings[0] / PROTOCOL3_SYNC_FACTOR; const unsigned long delay = RCSwitch::timings[0] / proto[p].syncFactor.low;
const unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance / 100; const unsigned long delayTolerance = delay * RCSwitch::nReceiveTolerance / 100;
for (unsigned int i = 1; i < changeCount; i += 2) { for (unsigned int i = 1; i < changeCount; i += 2) {
code <<= 1; code <<= 1;
if (diff(RCSwitch::timings[i], delay * PROTOCOL3_0_HIGH_CYCLES) < delayTolerance && if (diff(RCSwitch::timings[i], delay * proto[p].zero.high) < delayTolerance &&
diff(RCSwitch::timings[i + 1], delay * PROTOCOL3_0_LOW_CYCLES) < delayTolerance) { diff(RCSwitch::timings[i + 1], delay * proto[p].zero.low) < delayTolerance) {
// zero // zero
} else if (diff(RCSwitch::timings[i], delay * PROTOCOL3_1_HIGH_CYCLES) < delayTolerance && } else if (diff(RCSwitch::timings[i], delay * proto[p].one.high) < delayTolerance &&
diff(RCSwitch::timings[i + 1], delay * PROTOCOL3_1_LOW_CYCLES) < delayTolerance) { diff(RCSwitch::timings[i + 1], delay * proto[p].one.low) < delayTolerance) {
// one // one
code |= 1; code |= 1;
} else { } else {
...@@ -760,7 +659,7 @@ bool RCSwitch::receiveProtocol3(unsigned int changeCount) { ...@@ -760,7 +659,7 @@ bool RCSwitch::receiveProtocol3(unsigned int changeCount) {
RCSwitch::nReceivedValue = code; RCSwitch::nReceivedValue = code;
RCSwitch::nReceivedBitlength = changeCount / 2; RCSwitch::nReceivedBitlength = changeCount / 2;
RCSwitch::nReceivedDelay = delay; RCSwitch::nReceivedDelay = delay;
RCSwitch::nReceivedProtocol = 3; RCSwitch::nReceivedProtocol = p;
} }
return true; return true;
...@@ -781,9 +680,9 @@ void RCSwitch::handleInterrupt() { ...@@ -781,9 +680,9 @@ void RCSwitch::handleInterrupt() {
repeatCount++; repeatCount++;
changeCount--; changeCount--;
if (repeatCount == 2) { if (repeatCount == 2) {
if (receiveProtocol1(changeCount) == false){ if (receiveProtocol(1, changeCount) == false) {
if (receiveProtocol2(changeCount) == false){ if (receiveProtocol(2, changeCount) == false) {
if (receiveProtocol3(changeCount) == false){ if (receiveProtocol(3, changeCount) == false) {
//failed //failed
} }
} }
......
...@@ -88,9 +88,22 @@ class RCSwitch { ...@@ -88,9 +88,22 @@ class RCSwitch {
#if not defined( RCSwitchDisableReceiving ) #if not defined( RCSwitchDisableReceiving )
void setReceiveTolerance(int nPercent); void setReceiveTolerance(int nPercent);
#endif #endif
struct HighLow {
byte high;
byte low;
};
struct Protocol {
int pulseLength;
HighLow syncFactor;
HighLow zero;
HighLow one;
};
void setProtocol(int nProtocol); void setProtocol(int nProtocol);
void setProtocol(int nProtocol, int nPulseLength); void setProtocol(int nProtocol, int nPulseLength);
private: private:
char* getCodeWordB(int nGroupNumber, int nSwitchNumber, boolean bStatus); char* getCodeWordB(int nGroupNumber, int nSwitchNumber, boolean bStatus);
char* getCodeWordA(const char* sGroup, int nSwitchNumber, boolean bStatus); char* getCodeWordA(const char* sGroup, int nSwitchNumber, boolean bStatus);
...@@ -104,21 +117,20 @@ class RCSwitch { ...@@ -104,21 +117,20 @@ class RCSwitch {
void send1(); void send1();
void sendSync(); void sendSync();
void transmit(int nHighPulses, int nLowPulses); void transmit(int nHighPulses, int nLowPulses);
void transmit(HighLow pulses);
static char* dec2binWzerofill(unsigned long dec, unsigned int length); static char* dec2binWzerofill(unsigned long dec, unsigned int length);
static char* dec2binWcharfill(unsigned long dec, unsigned int length, char fill); static char* dec2binWcharfill(unsigned long dec, unsigned int length, char fill);
#if not defined( RCSwitchDisableReceiving ) #if not defined( RCSwitchDisableReceiving )
static void handleInterrupt(); static void handleInterrupt();
static bool receiveProtocol1(unsigned int changeCount); static bool receiveProtocol(const int p, unsigned int changeCount);
static bool receiveProtocol2(unsigned int changeCount);
static bool receiveProtocol3(unsigned int changeCount);
int nReceiverInterrupt; int nReceiverInterrupt;
#endif #endif
int nTransmitterPin; int nTransmitterPin;
int nPulseLength; int nPulseLength;
int nRepeatTransmit; int nRepeatTransmit;
char nProtocol; int nProtocol;
#if not defined( RCSwitchDisableReceiving ) #if not defined( RCSwitchDisableReceiving )
static int nReceiveTolerance; static int nReceiveTolerance;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment