Skip to content

Commit 2bf7975

Browse files
committed
adaptable baseTime for protocols
1 parent 51022cd commit 2bf7975

File tree

4 files changed

+126
-104
lines changed

4 files changed

+126
-104
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ The following examples sketches are available:
5757
* The [Intertechno](./examples/intertechno/README.md) example shows how to register and use
5858
the 2 protocols used by devices from intertechno.
5959

60-
* The [TempSensor](./examples/TempSensor/README.md) example shows how to receive temperature+humidity from a cresta protoco based sensor.
60+
* The [TempSensor](./examples/TempSensor/README.md) example shows how to receive temperature+humidity
61+
from a cresta protocol based sensor.
6162

6263
* The [necIR](./examples/necIR/README.md) example shows how to receive and send the Infrared NEC protocol.
6364

64-
* The [Scanner](./examples/scanner/README.md) example ... ???
65+
* The [Scanner](./examples/scanner/README.md) example can be used to collect code timings for further analysis.
6566

6667
## Protocol definitions
6768

@@ -180,5 +181,6 @@ col.send("it2 s_##___#____#_#__###_____#____#__x");
180181

181182
* [About RF Protocols](/docs/rf433.md)
182183
* [Standard protocols](/docs/SC5272_protocol.md)
184+
* [EV1527 protocol](/docs/ev1527_protocol.md)
183185
* [intertechno protocols](/docs/intertechno_protocol.md)
184186
* [Cresta protocol for sensors](/docs/cresta_protocol.md)

src/SignalParser.cpp

+78-61
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
/**
22
* @file SignalParser.cpp
3-
*
3+
*
44
* This file is part of the RFCodes library that implements receiving an sending
55
* RF and IR protocols.
6-
*
6+
*
77
* @copyright Copyright (c) by Matthias Hertel, https://www.mathertel.de.
88
*
99
* This work is licensed under a BSD 3-Clause style license,
1010
* https://www.mathertel.de/License.aspx.
11-
*
11+
*
1212
* @brief
1313
* This signal parser recognizes patterns in timing code sequences that are
1414
* defined by declarative tables.
15-
*
15+
*
1616
* Change History see SignalParser.h
1717
*/
1818

@@ -25,8 +25,7 @@
2525

2626

2727
/** find protocol by name */
28-
SignalParser::Protocol *SignalParser::_findProt(char *name)
29-
{
28+
SignalParser::Protocol *SignalParser::_findProt(char *name) {
3029
Protocol *p = nullptr;
3130

3231
for (int n = 0; n < _protocolCount; n++) {
@@ -35,12 +34,11 @@ SignalParser::Protocol *SignalParser::_findProt(char *name)
3534
break;
3635
}
3736
return (p);
38-
} // _findProt()
37+
} // _findProt()
3938

4039

4140
/** find code by name */
42-
SignalParser::Code *SignalParser::_findCode(Protocol *p, char codeName)
43-
{
41+
SignalParser::Code *SignalParser::_findCode(Protocol *p, char codeName) {
4442
Code *c = p->codes;
4543
int cnt = p->codeLength;
4644

@@ -51,50 +49,48 @@ SignalParser::Code *SignalParser::_findCode(Protocol *p, char codeName)
5149
cnt--;
5250
}
5351
return (cnt ? c : nullptr);
54-
} // _findCode()
52+
} // _findCode()
5553

5654

5755
/** reset all codes in a protocol */
58-
void SignalParser::_resetCodes(Protocol *p)
59-
{
56+
void SignalParser::_resetCodes(Protocol *p) {
6057
Code *c = p->codes;
6158
int cCnt = p->codeLength;
6259
while (c && cCnt) {
6360
c->valid = true;
6461
c->cnt = 0;
62+
c->total = 0;
6563

6664
c++;
6765
cCnt--;
6866
}
69-
} // _resetCodes()
67+
} // _resetCodes()
7068

7169

7270
/** reset the whole protocol to start capturing from scratch. */
73-
void SignalParser::_resetProtocol(Protocol *p)
74-
{
71+
void SignalParser::_resetProtocol(Protocol *p) {
7572
TRACE_MSG(" reset prot: %s", p->name);
7673
p->seqLen = 0;
7774
p->seq[0] = NUL;
7875
_resetCodes(p);
79-
} // _resetProtocol()
76+
_recalcProtocol(p, p->baseTime);
77+
} // _resetProtocol()
8078

8179

8280
/** use the callback function when registered using format <protocolname> <sequence> */
83-
void SignalParser::_useCallback(Protocol *p)
84-
{
81+
void SignalParser::_useCallback(Protocol *p) {
8582
if (p && _callbackFunc) {
8683
String code;
8784
code += p->name;
8885
code += ' ';
8986
code += p->seq;
9087
_callbackFunc(code.c_str());
9188
}
92-
} // _useCallback()
89+
} // _useCallback()
9390

9491

9592
/** check if the duration fits for the protocol */
96-
void SignalParser::_parseProtocol(Protocol *p, CodeTime duration)
97-
{
93+
void SignalParser::_parseProtocol(Protocol *p, CodeTime duration) {
9894
Code *c = p->codes;
9995
int cCnt = p->codeLength;
10096
bool anyValid = false;
@@ -106,7 +102,7 @@ void SignalParser::_parseProtocol(Protocol *p, CodeTime duration)
106102
// check if timing fits into this code
107103
int8_t i = c->cnt;
108104
CodeType type = c->type;
109-
bool matched = false; // until found that the new duration fits
105+
bool matched = false; // until found that the new duration fits
110106

111107
TRACE_MSG("check: %c", c->name);
112108

@@ -129,8 +125,10 @@ void SignalParser::_parseProtocol(Protocol *p, CodeTime duration)
129125
}
130126

131127
} else {
132-
matched = true; // this code matches
133-
} // if
128+
matched = true; // this code matches
129+
c->total += duration;
130+
131+
} // if
134132

135133
// write back to code
136134
c->valid = matched;
@@ -150,11 +148,21 @@ void SignalParser::_parseProtocol(Protocol *p, CodeTime duration)
150148

151149
if (i == c->timeLength) {
152150
// all timings received so add code-character.
151+
if (p->seqLen == 0) {
152+
TRACE_MSG("start: %s %d", p->name, c->total);
153+
int allTimes = 0;
154+
for (int tl = 0; tl < c->timeLength; tl++) {
155+
allTimes += c->time[tl];
156+
}
157+
_recalcProtocol(p, c->total / allTimes);
158+
}
159+
153160
p->seq[p->seqLen++] = c->name;
154161
p->seq[p->seqLen] = NUL;
155162
// DEBUG_ESP_PORT.print(c->name);
156163
TRACE_MSG(" add '%s'", p->seq);
157-
_resetCodes(p); // reset all codes but not the protocol
164+
165+
_resetCodes(p); // reset all codes but not the protocol
158166

159167
if ((type == END) && (p->seqLen < p->minCodeLen)) {
160168
// End packet found but sequence was not started early enough
@@ -171,10 +179,10 @@ void SignalParser::_parseProtocol(Protocol *p, CodeTime duration)
171179
_useCallback(p);
172180
_resetProtocol(p);
173181
}
174-
break; // no more code checking in this protocol
175-
} // if
182+
break; // no more code checking in this protocol
183+
} // if
176184
}
177-
} // if (c->valid)
185+
} // if (c->valid)
178186

179187
if (retryCandidate) {
180188
// only loop once
@@ -184,50 +192,46 @@ void SignalParser::_parseProtocol(Protocol *p, CodeTime duration)
184192
c++;
185193
cCnt--;
186194
}
187-
} // while
195+
} // while
188196

189197
if (!anyValid) {
190198
TRACE_MSG(" no codes.");
191199
_resetProtocol(p);
192200
}
193-
} // _parseProtocol()
201+
} // _parseProtocol()
194202

195203

196204
// ===== public functions =====
197205

198206

199207
/** attach a callback function that will get passed any new code. */
200-
void SignalParser::attachCallback(CallbackFunction newFunction)
201-
{
208+
void SignalParser::attachCallback(CallbackFunction newFunction) {
202209
_callbackFunc = newFunction;
203-
} // attachCallback()
210+
} // attachCallback()
204211

205212

206213
// return the number of send repeats that should occure.
207-
int SignalParser::getSendRepeat(char *name)
208-
{
214+
int SignalParser::getSendRepeat(char *name) {
209215
Protocol *p = _findProt(name);
210216
return (p ? p->sendRepeat : 0);
211217
}
212218

213219
/** parse a single duration.
214220
* @param duration check if this duration fits to any definitions.
215221
*/
216-
void SignalParser::parse(CodeTime duration)
217-
{
222+
void SignalParser::parse(CodeTime duration) {
218223
TRACE_MSG("(%d)", duration);
219224

220225
for (int n = 0; n < _protocolCount; n++) {
221226
_parseProtocol(_protocol[n], duration);
222227
}
223-
} // parse()
228+
} // parse()
224229

225230

226231
/** compose the timings of a sequence by using the code table.
227232
* @param sequence textual representation using "<protocolname> <codes>".
228233
*/
229-
void SignalParser::compose(const char *sequence, CodeTime *timings, int len)
230-
{
234+
void SignalParser::compose(const char *sequence, CodeTime *timings, int len) {
231235
char protname[PROTNAME_LEN];
232236

233237
char *s = strchr(sequence, ' ');
@@ -244,27 +248,47 @@ void SignalParser::compose(const char *sequence, CodeTime *timings, int len)
244248
}
245249
Protocol *p = _findProt(protname);
246250

247-
s++; // to start of code characters
251+
s++; // to start of code characters
248252

249253
if (p && timings) {
250254
while (*s && len) {
251255
Code *c = _findCode(p, *s);
252256
if (c) {
253257
for (int i = 0; i < c->timeLength; i++) {
254258
*timings++ = (c->minTime[i] + c->maxTime[i]) / 2;
255-
} // for
259+
} // for
256260
}
257261
s++;
258262
len--;
259263
}
260264
*timings = 0;
261-
} // if
265+
} // if
266+
}
267+
} // compose()
268+
269+
270+
void SignalParser::_recalcProtocol(Protocol *protocol, CodeTime baseTime) {
271+
// calc min and max and codesLength
272+
if (protocol->baseTime != baseTime) {
273+
TRACE_MSG("recalc %d", baseTime);
262274
}
263-
} // compose()
275+
276+
for (int cl = 0; cl < protocol->codeLength; cl++) {
277+
Code *c = &(protocol->codes[cl]);
278+
279+
for (int tl = 0; tl < c->timeLength; tl++) {
280+
CodeTime t = baseTime * c->time[tl];
281+
int radius = (t * protocol->tolerance) / 100;
282+
c->minTime[tl] = t - radius;
283+
c->maxTime[tl] = t + radius;
284+
}
285+
}
286+
} // _recalcProtocol()
287+
264288

265289
/** Load a protocol to be used. */
266-
void SignalParser::load(Protocol *protocol)
267-
{
290+
// @param otherBaseTime not in use yet.
291+
void SignalParser::load(Protocol *protocol, CodeTime otherBaseTime) {
268292
if (protocol) {
269293
TRACE_MSG("loading protocol %s", protocol->name);
270294

@@ -282,36 +306,29 @@ void SignalParser::load(Protocol *protocol)
282306

283307
CodeTime baseTime = protocol->baseTime;
284308

285-
// calc min and max and codesLength
309+
// calc c->timeLength and p->codeLength
286310
int cl = 0;
287311
while ((cl < MAX_CODELENGTH) && (protocol->codes[cl].name)) {
288312
Code *c = &(protocol->codes[cl]);
289313

290-
// calculate # of durations and absolute timing boundaries
291314
int tl = 0;
292315
while ((tl < MAX_TIMELENGTH) && (c->time[tl])) {
293-
CodeTime t = baseTime * c->time[tl];
294-
int radius = (t * protocol->tolerance) / 100;
295-
TRACE_MSG("== %d %d %d %d ", baseTime, c->time[tl], t, radius);
296-
297-
c->minTime[tl] = t - radius;
298-
c->maxTime[tl] = t + radius;
299316
tl++;
300-
} // while
317+
} // while
301318
c->timeLength = tl;
302-
303319
cl++;
304-
} // while
305-
protocol->codeLength = cl; // no need to specify codeLength
320+
} // while
321+
protocol->codeLength = cl; // no need to specify codeLength
306322

323+
_recalcProtocol(protocol, baseTime);
307324
_resetProtocol(protocol);
308325

309326
for (int n = 0; n < _protocolCount; n++) {
310327
TRACE_MSG(" reg[%d] = %08x", n, _protocol[n]);
311-
} // for
328+
} // for
312329

313330

314-
} // if
315-
} // load()
331+
} // if
332+
} // load()
316333

317334
// End.

0 commit comments

Comments
 (0)