/
ConstantLengthHighPulseProtocolBase.cpp
77 lines (68 loc) · 2.64 KB
/
ConstantLengthHighPulseProtocolBase.cpp
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
#include "ConstantLengthHighPulseProtocolBase.h"
#include <hardwareserial.h>
#define HIGH 0x1
#define LOW 0x0
ConstantLengthHighPulseProtocolBase::ConstantLengthHighPulseProtocolBase(
char * id,
void (*BitsstreamReceivedEvent)(ProtocolBase * protocol , byte* buffer , byte length ),
int bitstreamlength,
int sendrepeats ,
float timeperiodduration , // The duration of one oscillation
float shortperiods , // The number of timeperiods in a short-pulsecycle
float longperiods // The number of timeperiods in a long-pulsecycle
) : ProtocolBase(id , BitsstreamReceivedEvent , bitstreamlength , sendrepeats)
{
_timeperiodduration = timeperiodduration;
_shortperiods = shortperiods;
_longperiods = longperiods;
_highpulseduration = 0;
}
void ConstantLengthHighPulseProtocolBase::Decode(short state, unsigned int duration)
{
if (state==LOW)
{
unsigned int pulsecycleduration = duration + _highpulseduration ;
if (WithinExpectedDeviation( pulsecycleduration , _shortperiods * _timeperiodduration , _timeperiodduration / 8) )
{
// Are there more bits in the buffer than is expected for this protocol?
if (decoder_bitpos == GetBitstreamLength())
{
ShiftFirstBitOut(decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos);
}
AddBit( decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos , false);
} else if (WithinExpectedDeviation( pulsecycleduration , _longperiods * _timeperiodduration , _timeperiodduration / 8) )
{
// Are there more bits in the buffer than is expected for this protocol?
if (decoder_bitpos == GetBitstreamLength())
{
ShiftFirstBitOut(decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos);
}
AddBit( decoder_bitbuffer , decoder_bitbufferlength, decoder_bitpos , true);
} else
{
DecodeBitstream(_highpulseduration, duration);
ResetDecoder();
}
} else if (state==HIGH)
{
_highpulseduration = duration;
}
}
// This function returns a buffer with the pulse data of a bit for this protocol
void ConstantLengthHighPulseProtocolBase::EncodeBit(unsigned int *& pulsebuffer, byte & pulsebufferlength, bool bitvalue)
{
pulsebuffer = 0;
pulsebufferlength = 0;
SetPulse(pulsebuffer, pulsebufferlength, 0 , _shortperiods * _timeperiodduration );
if (bitvalue)
{
SetPulse(pulsebuffer, pulsebufferlength, 1 , _longperiods * _timeperiodduration );
} else
{
SetPulse(pulsebuffer, pulsebufferlength, 1 , _shortperiods * _timeperiodduration );
}
}
void ConstantLengthHighPulseProtocolBase::DecodeBitstream(unsigned int lasthigh, unsigned int lastlow)
{
if (_BitsstreamReceivedEvent!=0) _BitsstreamReceivedEvent( this , decoder_bitbuffer , decoder_bitpos);
}