-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManchesterCodeProtocolBase.cpp
More file actions
103 lines (96 loc) · 3.22 KB
/
ManchesterCodeProtocolBase.cpp
File metadata and controls
103 lines (96 loc) · 3.22 KB
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
#include "ProtocolBase.h"
#include "ManchesterCodeProtocolBase.h"
#include <hardwareserial.h>
#define HIGH 0x1
#define LOW 0x0
ManchesterCodeProtocolBase::ManchesterCodeProtocolBase(
char * id,
void (*BitsstreamReceivedEvent)(ProtocolBase * protocol , byte* buffer , byte length ),
int bitstreamlength,
int sendrepeats,
float Oscilator ,
float TerminatorOscilators) : ProtocolBase(id, BitsstreamReceivedEvent , bitstreamlength , sendrepeats)
{
_oscilator = Oscilator;
_terminatoroscilators = TerminatorOscilators;
mc_insync = true;
_highduration = 0;
}
void ManchesterCodeProtocolBase::Decode(short state, unsigned int duration)
{
if (state==LOW)
{
int pulsecycleduration = duration + _highduration;
/*if (WithinExpectedDeviation( pulsecycleduration , _oscilator * 2 , _oscilator ) )
{ // half high, single low or half low, single high
if (_highduration > duration)
{
mc_insync = false;
} else
{
AddBit( decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos , false );
mc_insync = false;
}
} else*/ if (WithinExpectedDeviation( pulsecycleduration , _oscilator * 2 , _oscilator / 4) )
{ // single high, single low
if (mc_insync)
{
AddBit( decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos , true );
} else
{
AddBit( decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos , false );
}
} else if (WithinExpectedDeviation( pulsecycleduration , _oscilator * 3 , _oscilator / 4) )
{ // single and double pulse
if (_highduration>duration)
{ // double high, single low
if (mc_insync)
{ // Error, sync incorrect
FlipBits(decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos);
mc_insync = !mc_insync;
}
AddBit( decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos , true);
mc_insync = true;
} else
{ // single high, double low
if (!mc_insync)
{ // Error, sync incorrect
FlipBits(decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos);
mc_insync = !mc_insync;
}
AddBit( decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos , true);
AddBit( decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos , false);
mc_insync = false;
}
} else if ( WithinExpectedDeviation( pulsecycleduration , _oscilator * 4 , _oscilator / 4))
{ // double high, double low
if (mc_insync)
{ // Error, sync incorrect
FlipBits(decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos);
mc_insync = !mc_insync;
}
AddBit( decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos , false);
AddBit( decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos , true);
} else
{ // Terminator
if (!mc_insync)
{
AddBit( decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos , false);
}
if (decoder_bitpos>=GetBitstreamLength() )
{
DecodeBitstream(_highduration, duration);
}
ResetDecoder();
mc_insync = false;
AddBit( decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos , false);
}
} else
{ // High pulse
_highduration = duration;
}
}
void ManchesterCodeProtocolBase::DecodeBitstream(unsigned int lasthigh, unsigned int lastlow)
{
if (_BitsstreamReceivedEvent!=0) _BitsstreamReceivedEvent( this , decoder_bitbuffer , decoder_bitpos);
}