-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfix.h
99 lines (73 loc) · 1.87 KB
/
fix.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
#ifndef FIX_H
#define FIX_H
float fmap(float x, float in_min, float in_max, float out_min, float out_max);
uint32_t getMacAddress();
class Fix
{
public:
char techno[8] = "Techno!";
uint32_t id;
double lat = 0;
double lng = 0;
uint32_t ageWhenReceived = 0;
uint32_t timeWhenReceived = 0;
uint32_t satellites = 0;
int32_t hdop = 0;
float batteryVoltage = 0;
float rssi = 0;
float snr = 0;
uint32_t uptime;
bool goodFix()
{
return lat != 0 && lng != 0 && hdop < 165;
}
double batteryFraction()
{
return fmap(batteryVoltage, 340, 623, 0.0, 1.0);
}
double distanceTo( Fix theirFix )
{
return TinyGPSPlus::distanceBetween(lat, lng, theirFix.lat, theirFix.lng);
};
double headingTo( Fix theirFix )
{
return TinyGPSPlus::courseTo(lat, lng, theirFix.lat, theirFix.lng);
};
bool decode(uint8_t*buf, uint32_t len)
{
if( len != sizeof( Fix ))
{
Serial.println("decode - wrong size");
return false;
}
if( 0 != strncmp( (char*) buf, "Techno!", strlen(techno)))
{
Serial.println("not techno");
return false;
}
memcpy(this, buf, len);
if( id != 0x67CDF1D2 && id != 0x52A2DF5C ) // my two t-echo units
{
Serial.printf("Wrong id %X, not for us\n", id);
return false;
}
if( id == getMacAddress())
{
Serial.printf("id %X is ours (%X)!\n", id, getMacAddress());
return false;
}
timeWhenReceived = millis(); // so we can calculate age in our own timebase
return true;
};
uint32_t age()
{
return ageWhenReceived + (millis() - timeWhenReceived);
};
int encode(uint8_t*buf)
{
ageWhenReceived = ageWhenReceived + (millis() - timeWhenReceived); // so we send total time since fix - doing a sneaky thing here, using this to mean sometime 'since received form gps' and sometimes 'since received from lora
memcpy(buf, this, sizeof( Fix ));
return sizeof( Fix );
};
};
#endif