-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathposition.cpp
More file actions
91 lines (81 loc) · 2.26 KB
/
position.cpp
File metadata and controls
91 lines (81 loc) · 2.26 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
/*
mrefd - An M17-only refector
Copyright (C) 2026 Thomas A. Early
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include "position.h"
CPosition::CPosition(const uint8_t *data)
{
Get(data);
}
void CPosition::Get(const uint8_t *data)
{
// source type
source = data[0] >> 4;
// station type
station = data[0] & 0xfu;
// validity
positionValid = data[1] & 0x80u;
// position
if (positionValid)
{
constexpr double a = 1.0 / 8388607.0;
auto nlat = (int32_t(data[3] << 24) + (data[4] << 16) + (data[5] << 8)) >> 8;
latitude = 90.0 * nlat * a;
auto nlon = (int32_t(data[6] << 24) + (data[7] << 16) + (data[8] << 8)) >> 8;
longitude = 180.0 * nlon * a;
}
}
const char *CPosition::GetSource() const
{
switch (source)
{
case 0: return "M17 client";
case 1: return "OpenRTx";
case 15: return "Other";
default: return "Reserved";
}
}
const char *CPosition::GetStation() const
{
switch (station)
{
case 0: return "Fixed";
case 1: return "Mobile";
case 2: return "Handheld";
case 15: return "Other";
default: return "Reserved";
}
}
const char *CPosition::GetPosition(std::string &la, std::string &lo)
{
if (positionValid)
{
double lat = latitude + 90.0;
double lon = longitude + 180.0;
maidenhead[0] = 'A' + (int(lon) / 20);
maidenhead[1] = 'A' + (int(lat) / 10);
maidenhead[2] = '0' + ((int(lon) % 20) / 2);
maidenhead[3] = '0' + (int(lat) % 10);
maidenhead[4] = 'a' + (int(lon * 12.0) % 24);
maidenhead[5] = 'a' + (int(lat * 24.0) % 24);
maidenhead[6] = '\0';
char buf[16];
snprintf(buf, 15, "%+.6f", latitude);
la.assign(buf);
snprintf(buf, 15, "%+.6f", longitude);
lo.assign(buf);
return maidenhead;
}
return nullptr;
}