-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadSignal.cpp
83 lines (75 loc) · 1.3 KB
/
readSignal.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
78
79
80
81
82
83
#define RCPIN (6)
#define PER (120)
char inputBuf[5];
bool readSignal( int botnum )
{
long now = micros();
if( 0 == digitalRead(RCPIN) )
{
return( false );
}
long zstart;
long p1;
do
{
zstart = micros();
p1 = zstart-now;
} while( ( p1 < (PER*5) ) && ( 1 == digitalRead(RCPIN) ) );
if( (p1<(PER*3)) || (p1>=(PER*5)) )
{
return( false );
}
long bitstart;
do
{
bitstart = micros();
p1 = bitstart-zstart;
} while( (p1<(PER*3)) && ( 0 == digitalRead(RCPIN) ) );
if( p1 >= (PER*3) )
{
return( false );
}
char tbuf[5];
char* ptr = tbuf;
byte sum = 0;
for( int b=0; b<5; b++ )
{
unsigned char c = 0;
for( int i=0; i<8; i++ )
{
int expBit = (i&1)^1;
long bitend;
long dt;
do
{
bitend = micros();
dt = bitend-bitstart;
} while( (dt<(PER*3)) && ( expBit == digitalRead(RCPIN) ) );
if( dt >= (PER*3) )
{
return( false );
}
c>>=1;
if( dt>(PER+(PER/2)) )
{
c |= 128;
}
bitstart = bitend;
}
*ptr++ = c;
sum += c;
}
if( sum != 0xff )
{
return( false );
}
if( tbuf[0] != botnum )
{
return( false );
}
for( unsigned int i=0; i<5; i++ )
{
inputBuf[i] = tbuf[i];
}
return( true );
}