-
Notifications
You must be signed in to change notification settings - Fork 0
/
signalDecoder4.h
214 lines (179 loc) · 7.44 KB
/
signalDecoder4.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* Pattern Decoder Library V4
* Library to decode radio signals based on patternd detection
* 2014-2015 N.Butzek, S.Butzek
* 2015-2017 S.Butzek
* 2020 Ralf9
* This library contains different classes to perform detecting of digital signals
* typical for home automation. The focus for the moment is on different sensors
* like weather sensors (temperature, humidity Logilink, TCM, Oregon Scientific, ...),
* remote controlled power switches (Intertechno, TCM, ARCtech, ...) which use
* encoder chips like PT2262 and EV1527-type and manchester encoder to send
* information in the 433MHz or 868 Mhz Band.
*
* The classes in this library follow the approach to detect a recurring pattern in the
* recived signal. For Manchester there is a class which decodes the signal.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _SIGNALDECODER_h
#define _SIGNALDECODER_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
//#include "WProgram.h"
#endif
#define CMP_CC1101
#define DEBUG 1
//#define LAN_WIZ 1 // die Ausgabe ueber Ethernet funktioniert nur, wenn dies hier nochmals definiert wird
#include "output.h"
#include "bitstore.h"
#include "FastDelegate.h"
#define maxNumPattern 16
#define defMaxMsgSize 1500 //2000
#define defMsMaxMsgSize 254
#define minMessageLen 40
#define syncMinFact 6
#define syncMaxFact 45 // 39
#define syncMaxMicros 17000
#define maxPulse 32001 // Magic Pulse Length
#define SERIAL_DELIMITER char(';')
#define MSG_START char(0x2) // this is a non printable Char
#define MSG_END char(0x3) // this is a non printable Char
//#define DEBUGDETECT 1
//#define DEBUGDETECT 255 // Very verbose output
//#define MCDEBUGDECODE 2 // debug mc-decoder
//#define MCDEBUGDETECT 5 // debug isManchester
//#define DEBUGGLEICH 1
//#define DEBUGDECODE 1 // debug processMessage
//#define DEBUGDoDETECT 3 // debug compress_pattern
//#define DEBUGMUREPEAT 1 // debug isMuMessageRepeat
enum status { searching, clockfound, syncfound, detecting };
class SignalDetectorClass
{
friend class ManchesterpatternDecoder;
public:
SignalDetectorClass() : first(buffer), last(NULL), message(4) {
buffer[0] = 0; reset(); mcMinBitLen = 17;
MsMoveCount = 0;
MuMoveCount = 0;
};
void reset();
bool decode(const int16_t* pulse);
const status getState();
typedef fastdelegate::FastDelegate0<uint8_t> FuncRetuint8t;
void setRSSICallback(FuncRetuint8t callbackfunction) { _rssiCallback = callbackfunction; }
//private:
int8_t clock; // index to clock in pattern
bool MUenabled;
bool MCenabled;
bool MSenabled;
bool MSeqEnabled;
bool MredEnabled; // 1 = compress printMsgRaw
//bool MuNoOverflow;
bool MdebEnabled; // 1 = print message debug info enabled
//bool MfiltEnabled; // fuer Nachrichten Filter reserviert
uint8_t MsMoveCountmax;
uint8_t MsMoveCount;
uint8_t MuMoveCount;
uint16_t maxMuPrint;
uint16_t maxMsgSize;
uint16_t MuSplitThresh;
uint8_t cMaxNumPattern;
int16_t cMaxPulse;
bool printMsgSuccess;
uint16_t histo[maxNumPattern];
//uint8_t message[defMaxMsgSize];
BitStore<defMaxMsgSize/2> message; // A store using 4 bit for every value stored.
char buf[350] = {};
#ifdef DEBUGGLEICH
uint8_t bMoveFlag; // nur zu debugzwecke
#endif
int16_t firstLast;
int16_t lastPulse;
uint16_t messageLen; // Todo, kann durch message.valcount ersetzt werden
uint8_t mstart; // Holds starting point for message
uint16_t mend; // Holds end point for message if detected
bool success; // True if a valid coding was found
bool m_truncated; // Identify if message has been truncated
bool m_overflow;
void bufferMove(const uint16_t start);
uint16_t tol; // calculated tolerance for signal
status state; // holds the status of the detector
int16_t buffer[2]; // Internal buffer to store two pules length
int16_t* first; // Pointer to first buffer entry
int16_t* last; // Pointer to last buffer entry
float tolFact; //
int16_t pattern[maxNumPattern]; // 1d array to store the pattern
int16_t ms0pattern[10];
uint8_t patternLen; // counter for length of pattern
uint8_t msPatternLen;
uint8_t pattern_pos;
int8_t sync; // index to sync in pattern if it exists
bool mcDetected; // MC Signal alread detected flag
bool mcValid;
bool mcRepeat; // ist true wenn evtl noch eine Wiederholung folgen kann
uint8_t mcMinBitLen; // min bit Length
uint8_t rssiValue; // Holds the RSSI value retrieved via a rssi callback
uint8_t idxDat;
uint8_t msEqCnt;
bool MsEqSkip;
FuncRetuint8t _rssiCallback=NULL; // Holds the pointer to a callback Function
void addData(const uint8_t value);
void addPattern();
inline void updPattern(const uint8_t ppos);
void doDetect();
void processMessage(const uint8_t p_valid);
bool compress_pattern();
void calcHisto(const uint8_t startpos = 0, uint16_t endpos = 0);
bool getClock(); // Searches a clock in a given signal
bool getSync(); // Searches clock and sync in given Signal
//int8_t printMsgRaw(uint8_t m_start, const uint_t m_end, const String *preamble = NULL, const String *postamble = NULL);
void printMsgStr(const String *first, const String *second, const String *third);
const bool inTol(const int16_t val, const int16_t set, const int16_t tolerance); // checks if a value is in tolerance range
void printOut();
int8_t findpatt(const int16_t val); // Finds a pattern in our pattern store. returns -1 if te pattern is not found
bool checkMBuffer();
bool isMuMessageRepeat();
};
class ManchesterpatternDecoder
{
public:
ManchesterpatternDecoder(SignalDetectorClass *ref_dec) : ManchesterBits(1), longlow(-1), longhigh(-1), shorthigh(-1), shortlow(-1) { pdec = ref_dec; reset(); };
~ManchesterpatternDecoder();
const bool doDecode();
void setMinBitLen(const uint8_t len);
void getMessageHexStr(String *message);
void printMessageHexStr();
void printBufMessageHexStr();
const bool isManchester();
void reset();
#ifndef UNITTEST
//private:
#endif
BitStore<50> ManchesterBits; // A store using 1 bit for every value stored. It's used for storing the Manchester bit data in a efficent way
SignalDetectorClass *pdec;
int8_t longlow;
int8_t longhigh;
int8_t shortlow;
int8_t shorthigh;
int16_t clock; // Manchester calculated clock
int8_t minbitlen;
uint8_t mc_sync_pos;
bool mc_start_found = false;
const bool isLong(const uint8_t pulse_idx);
const bool isShort(const uint8_t pulse_idx);
unsigned char getMCByte(const uint8_t idx); // Returns one Manchester byte in correct order. This is a helper function to retrieve information out of the buffer
};
#endif