-
Notifications
You must be signed in to change notification settings - Fork 0
/
TX20.c
106 lines (96 loc) · 2.63 KB
/
TX20.c
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
#include "TX20.h"
const char TX20_Directions[16][4] = {{"N"}, {"NNE"}, {"NE"}, {"ENE"}, {"E"}, {"ESE"}, {"SE"}, {"SSE"}, {"S"}, {"SSW"}, {"SW"}, {"WSW"}, {"W"}, {"WNW"}, {"NW"}, {"NNW"}};
void RPi_TX20_InitPins( void )
{
// Set the DATA pin to input
TX20_DATA_SET_INPUT;
}
unsigned char RPi_TX20_GetReading(int *iDir, int *iSpeed )
{
// Local Variables
int a = 0;
int x = 0;
int datagram[41];
int start =0;
int startframe = 0;
int winddir = 0;
int winddir2 = 0;
int windspeed = 0;
int windspeed2 = 0;
int checksum = 0;
unsigned char chk;
int bitcount=0;
unsigned int isValidData = 0;
unsigned int pin;
//Pull DTR low to signal TX20 to send data
TX20_DTR_SET_OUTPUT_LOW;
// Input should be low right now
if ( TX20_DATA_GET_BIT != 0 )
{
printf("Error 1 : Invalid pin state\n");
//return FALSE;
}
//printf("Waiting for pin to go high\n");
while(isValidData < 1)
{
uint8_t value = TX20_DATA_GET_BIT; // Read some data
//printf("read from pin 15: %d\n", value);
if (value == 1 && start == 0){
start = 1;
a = 0;
startframe=winddir=checksum=winddir2=0;
windspeed=0;windspeed2=0;
//printf("start of data found\n");
}
if(start == 1){
for (bitcount=41; bitcount>0; bitcount--) {
pin = (TX20_DATA_GET_BIT);
if (bitcount > 41-5){
// start, inverted
startframe = (startframe<<1)|(pin^1);
} else
if (bitcount > 41-5-4){
// wind dir, inverted
winddir = winddir>>1 | ((pin^1)<<3);
} else
if (bitcount > 41-5-4-12){
// windspeed, inverted
windspeed = windspeed>>1 | ((pin^1)<<11);
} else
if (bitcount > 41-5-4-12-4){
// checksum, inverted
checksum = checksum>>1 | ((pin^1)<<3);
} else
if (bitcount > 41-5-4-12-4-4){
// wind dir
winddir2 = winddir2>>1 | (pin<<3);
} else {
// windspeed
windspeed2 = windspeed2>>1 | (pin<<11);
}
datagram[a] = pin;
a++;
TX20_DoDelay;
//printf("a: %d",a);
}
}
//delayMicroseconds(1220);
TX20_DoDelay;
//check if we got a valid datagram
if(a == 41){
//Calculate Checksum
chk= ( winddir + (windspeed&0xf) + ((windspeed>>4)&0xf) + ((windspeed>>8)&0xf) );chk&=0xf;
//Decide on the result to return
if (startframe==4 && winddir==winddir2 && windspeed==windspeed2 && checksum==chk){
isValidData=1;
//printf("is valid set\n");
} else {
isValidData=0;
}
}
}
//return values
*iDir = winddir;
*iSpeed = windspeed;
return isValidData;
}