NMEA: detectio of the optional tag block in front of a sentence, ignoring it for...
[marnav.git] / src / marnav / nmea / bwr.cpp
blobd102b393bcef62f618ae197f13dc5ae77e92a53c
1 #include "bwr.hpp"
2 #include <marnav/nmea/io.hpp>
3 #include <marnav/nmea/convert.hpp>
5 namespace marnav
7 namespace nmea
9 MARNAV_NMEA_DEFINE_SENTENCE_PARSE_FUNC(bwr)
11 constexpr const char * bwr::TAG;
13 bwr::bwr()
14 : sentence(ID, TAG, talker_id::global_positioning_system)
18 bwr::bwr(talker talk, fields::const_iterator first, fields::const_iterator last)
19 : sentence(ID, TAG, talk)
21 const auto n = std::distance(first, last);
22 if ((n < 12) || (n > 13))
23 throw std::invalid_argument{"invalid number of fields in bwr"};
25 read(*(first + 0), time_utc);
26 read(*(first + 1), lat);
27 read(*(first + 2), lat_hem);
28 read(*(first + 3), lon);
29 read(*(first + 4), lon_hem);
30 read(*(first + 5), bearing_true);
31 read(*(first + 6), bearing_true_ref);
32 read(*(first + 7), bearing_mag);
33 read(*(first + 8), bearing_mag_ref);
34 read(*(first + 9), distance);
35 read(*(first + 10), distance_unit);
36 read(*(first + 11), waypoint_id);
38 if (n > 12)
39 read(*(first + 12), mode_ind);
41 // instead of reading data into temporary lat/lon, let's correct values afterwards
42 lat = correct_hemisphere(lat, lat_hem);
43 lon = correct_hemisphere(lon, lon_hem);
46 utils::optional<geo::longitude> bwr::get_longitude() const
48 return (lon && lon_hem) ? lon : utils::optional<geo::longitude>{};
51 utils::optional<geo::latitude> bwr::get_latitude() const
53 return (lat && lat_hem) ? lat : utils::optional<geo::latitude>{};
56 void bwr::set_lat(const geo::latitude & t)
58 lat = t;
59 lat_hem = convert_hemisphere(t);
62 void bwr::set_lon(const geo::longitude & t)
64 lon = t;
65 lon_hem = convert_hemisphere(t);
68 void bwr::set_bearing_true(double t) noexcept
70 bearing_true = t;
71 bearing_true_ref = reference::TRUE;
74 void bwr::set_bearing_mag(double t) noexcept
76 bearing_mag = t;
77 bearing_mag_ref = reference::MAGNETIC;
80 void bwr::set_distance(double t) noexcept
82 distance = t;
83 distance_unit = unit::distance::nm;
86 std::vector<std::string> bwr::get_data() const
88 return {to_string(time_utc), to_string(lat), to_string(lat_hem), to_string(lon),
89 to_string(lon_hem), to_string(bearing_true), to_string(bearing_true_ref),
90 to_string(bearing_mag), to_string(bearing_mag_ref), to_string(distance),
91 to_string(distance_unit), to_string(waypoint_id), to_string(mode_ind)};