Skip to content

Commit f74f892

Browse files
slo617hawa-lc4
authored andcommitted
add support for nexus protocol to rc-switch library (arendst#21886)
* add support for nexus protocol to RCSwitch library Nexus protocol is used by temperature and humidity sensor that operate at 433 MHz. It is used by various brands. * calc separation limit for RCSwitch library automatically
1 parent 83d7853 commit f74f892

File tree

3 files changed

+57
-10
lines changed

3 files changed

+57
-10
lines changed

lib/lib_rf/rc-switch/src/RCSwitch.cpp

+47-4
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,9 @@ static const RCSwitch::Protocol PROGMEM proto[] = {
151151
{ 340, 0, { 0, 0 }, 1, { 14, 4 }, { 1, 2 }, { 2, 1 }, false, 0 }, // 33 (Dooya Control DC2708L)
152152
{ 120, 0, { 0, 0 }, 1, { 1, 28 }, { 1, 3 }, { 3, 1 }, false, 0 }, // 34 DIGOO SD10 - so as to use this protocol RCSWITCH_SEPARATION_LIMIT must be set to 2600
153153
{ 20, 0, { 0, 0 }, 1, { 239, 78 }, {20, 35 }, {35, 20}, false, 10000},// 35 Dooya 5-Channel blinds remote DC1603
154-
{ 250, 0, { 0, 0 }, 1, { 18, 6 }, { 1, 3 }, { 3, 1 }, false, 0 }, // 36 Dooya remote DC2700AC for Dooya DT82TV curtains motor
155-
{ 200, 0, { 0, 0 }, 0, { 0, 0 }, { 1, 3 }, { 3, 1} , false, 20} // 37 DEWENWILS Power Strip
154+
{ 250, 0, { 0, 0 }, 1, { 18, 6 }, { 1, 3 }, { 3, 1 }, false, 0 }, // 36 Dooya remote DC2700AC for Dooya DT82TV curtains motor
155+
{ 200, 0, { 0, 0 }, 0, { 0, 0 }, { 1, 3 }, { 3, 1 }, false, 20 }, // 37 DEWENWILS Power Strip
156+
{ 500, 0, { 0, 0 }, 1, { 7, 1 }, { 2, 1 }, { 4, 1 }, true, 0 }, // 38 temperature and humidity sensor, various brands, nexus protocol, 36 bits + start impulse
156157
};
157158

158159
enum {
@@ -166,7 +167,7 @@ volatile unsigned int RCSwitch::nReceivedBitlength = 0;
166167
volatile unsigned int RCSwitch::nReceivedDelay = 0;
167168
volatile unsigned int RCSwitch::nReceivedProtocol = 0;
168169
int RCSwitch::nReceiveTolerance = 60;
169-
const unsigned int RCSwitch::nSeparationLimit = RCSWITCH_SEPARATION_LIMIT;
170+
unsigned int RCSwitch::nSeparationLimit = RCSWITCH_SEPARATION_LIMIT;
170171
unsigned int RCSwitch::timings[RCSWITCH_MAX_CHANGES];
171172
unsigned int RCSwitch::buftimings[4];
172173
#endif
@@ -238,8 +239,50 @@ void RCSwitch::setReceiveTolerance(int nPercent) {
238239
RCSwitch::nReceiveTolerance = nPercent;
239240
}
240241

241-
void RCSwitch::setReceiveProtocolMask(unsigned long long mask) {
242+
bool RCSwitch::setReceiveProtocolMask(unsigned long long mask) {
242243
RCSwitch::nReceiveProtocolMask = mask;
244+
return updateSeparationLimit();
245+
}
246+
247+
bool RCSwitch::updateSeparationLimit()
248+
{
249+
unsigned int longestPulseTime = std::numeric_limits<unsigned int>::max();
250+
unsigned int shortestPulseTime = 0;
251+
252+
unsigned long long thisMask = 1;
253+
for(unsigned int i = 0; i < numProto; i++) {
254+
if (RCSwitch::nReceiveProtocolMask & thisMask) {
255+
const unsigned int headerShortPulseCount = std::min(proto[i].Header.high, proto[i].Header.low);
256+
const unsigned int headerLongPulseCount = std::max(proto[i].Header.high, proto[i].Header.low);
257+
258+
// This must be the longest pulse-length of this protocol. nSeparationLimit must of this length or shorter.
259+
// This pulse will be used to detect the beginning of a transmission.
260+
const unsigned int headerLongPulseTime = proto[i].pulseLength * headerLongPulseCount;
261+
262+
// nSeparationLimit must be longer than any of the following pulses to avoid detecting a new transmission in the middle of a frame.
263+
unsigned int longestDataPulseCount = headerShortPulseCount;
264+
longestDataPulseCount = std::max<unsigned int>(longestDataPulseCount, proto[i].zero.high);
265+
longestDataPulseCount = std::max<unsigned int>(longestDataPulseCount, proto[i].zero.low);
266+
longestDataPulseCount = std::max<unsigned int>(longestDataPulseCount, proto[i].one.high);
267+
longestDataPulseCount = std::max<unsigned int>(longestDataPulseCount, proto[i].one.low);
268+
269+
const unsigned int longestDataPulseTime = proto[i].pulseLength * longestDataPulseCount;
270+
271+
longestPulseTime = std::min(longestPulseTime, headerLongPulseTime);
272+
shortestPulseTime = std::max(shortestPulseTime, longestDataPulseTime);
273+
}
274+
thisMask <<= 1;
275+
}
276+
277+
if (longestPulseTime <= shortestPulseTime) {
278+
// incompatible protocols enabled, fall back to default value
279+
nSeparationLimit = RCSWITCH_SEPARATION_LIMIT;
280+
return false;
281+
}
282+
283+
const unsigned int timeDiff = longestPulseTime - shortestPulseTime;
284+
nSeparationLimit = longestPulseTime - (timeDiff / 2);
285+
return true;
243286
}
244287
#endif
245288

lib/lib_rf/rc-switch/src/RCSwitch.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@
5757
#endif
5858

5959
// Number of maximum high/Low changes per packet.
60-
// We can handle up to (unsigned long) => 32 bit * 2 H/L changes per bit + 2 for sync
60+
// We can handle up to 36 bit * 2 H/L changes per bit + 2 for sync
6161
// Для keeloq нужно увеличить RCSWITCH_MAX_CHANGES до 23+1+66*2+1=157
62-
#define RCSWITCH_MAX_CHANGES 67 // default 67
62+
#define RCSWITCH_MAX_CHANGES 75 // default 75 - longest protocol that requires this buffer size is 38/nexus
6363

6464
// separationLimit: minimum microseconds between received codes, closer codes are ignored.
6565
// according to discussion on issue #14 it might be more suitable to set the separation
@@ -108,7 +108,7 @@ class RCSwitch {
108108
void setRepeatTransmit(int nRepeatTransmit);
109109
#if not defined( RCSwitchDisableReceiving )
110110
void setReceiveTolerance(int nPercent);
111-
void setReceiveProtocolMask(unsigned long long mask);
111+
bool setReceiveProtocolMask(unsigned long long mask);
112112
#endif
113113

114114
/**
@@ -171,6 +171,7 @@ class RCSwitch {
171171
#if not defined( RCSwitchDisableReceiving )
172172
static void handleInterrupt();
173173
static bool receiveProtocol(const int p, unsigned int changeCount);
174+
static bool updateSeparationLimit();
174175
int nReceiverInterrupt;
175176
#endif
176177
int nTransmitterPin;
@@ -184,7 +185,7 @@ class RCSwitch {
184185
volatile static unsigned int nReceivedBitlength;
185186
volatile static unsigned int nReceivedDelay;
186187
volatile static unsigned int nReceivedProtocol;
187-
const static unsigned int nSeparationLimit;
188+
static unsigned int nSeparationLimit;
188189
/*
189190
* timings[0] contains sync timing, followed by a number of bits
190191
*/

tasmota/tasmota_xdrv_driver/xdrv_17_rcswitch.ino

+5-2
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void RfInit(void) {
9292
if (!Settings->rf_protocol_mask) {
9393
Settings->rf_protocol_mask = (1ULL << mySwitch.getNumProtos()) -1;
9494
}
95-
mySwitch.setReceiveProtocolMask(Settings->rf_protocol_mask);
95+
(void)mySwitch.setReceiveProtocolMask(Settings->rf_protocol_mask);
9696
}
9797
}
9898

@@ -132,7 +132,10 @@ void CmndRfProtocol(void) {
132132
}
133133
}
134134
}
135-
mySwitch.setReceiveProtocolMask(Settings->rf_protocol_mask);
135+
const bool incompatibleProtocolsSelected = !mySwitch.setReceiveProtocolMask(Settings->rf_protocol_mask);
136+
if (incompatibleProtocolsSelected) {
137+
AddLog(LOG_LEVEL_INFO, PSTR("RFR: CmndRfProtocol:: Incompatible protocols selected, using default separation limit, some protocols may not work"));
138+
}
136139
// AddLog(LOG_LEVEL_INFO, PSTR("RFR: CmndRfProtocol:: Start responce"));
137140
Response_P(PSTR("{\"" D_CMND_RFPROTOCOL "\":\""));
138141
bool gotone = false;

0 commit comments

Comments
 (0)