NMEA: remove deprecated type talker_id
[marnav.git] / src / marnav / nmea / bwc.cpp
blobdfc0ccdf8c4474469b9e81d4054dbea7f9d3385e
1 #include <marnav/nmea/bwc.hpp>
2 #include "convert.hpp"
3 #include "checks.hpp"
4 #include <marnav/nmea/io.hpp>
6 namespace marnav
8 namespace nmea
10 constexpr sentence_id bwc::ID;
11 constexpr const char * bwc::TAG;
13 bwc::bwc()
14 : sentence(ID, TAG, talker::global_positioning_system)
18 bwc::bwc(talker talk, fields::const_iterator first, fields::const_iterator last)
19 : sentence(ID, TAG, talk)
21 const auto size = std::distance(first, last);
22 if ((size != 12) && (size != 13))
23 throw std::invalid_argument{"invalid number of fields in bwc"};
25 utils::optional<unit::distance> distance_unit;
27 read(*(first + 0), time_utc_);
28 read(*(first + 1), lat_);
29 read(*(first + 2), lat_hem_);
30 read(*(first + 3), lon_);
31 read(*(first + 4), lon_hem_);
32 read(*(first + 5), bearing_true_);
33 read(*(first + 6), bearing_true_ref_);
34 read(*(first + 7), bearing_mag_);
35 read(*(first + 8), bearing_mag_ref_);
36 read(*(first + 9), distance_);
37 read(*(first + 10), distance_unit);
38 read(*(first + 11), waypoint_id_);
40 if (size == 13)
41 read(*(first + 12), mode_ind_);
43 // instead of reading data into temporary lat/lon, let's correct values afterwards
44 lat_ = correct_hemisphere(lat_, lat_hem_);
45 lon_ = correct_hemisphere(lon_, lon_hem_);
47 check_value(distance_unit, {unit::distance::nm}, "distance nautical miles unit");
50 utils::optional<geo::longitude> bwc::get_lon() const
52 return (lon_ && lon_hem_) ? lon_ : utils::optional<geo::longitude>{};
55 utils::optional<geo::latitude> bwc::get_lat() const
57 return (lat_ && lat_hem_) ? lat_ : utils::optional<geo::latitude>{};
60 void bwc::set_lat(const geo::latitude & t)
62 lat_ = t;
63 lat_hem_ = convert_hemisphere(t);
66 void bwc::set_lon(const geo::longitude & t)
68 lon_ = t;
69 lon_hem_ = convert_hemisphere(t);
72 void bwc::set_bearing_true(double t) noexcept
74 bearing_true_ = t;
75 bearing_true_ref_ = reference::TRUE;
78 void bwc::set_bearing_mag(double t) noexcept
80 bearing_mag_ = t;
81 bearing_mag_ref_ = reference::MAGNETIC;
84 utils::optional<units::length> bwc::get_distance() const
86 if (!distance_)
87 return {};
88 return {*distance_};
91 void bwc::set_distance(units::length t)
93 if (t.value() < 0.0)
94 throw std::invalid_argument{"invalid argument, distance less than zero"};
95 distance_ = t.get<units::nautical_miles>();
98 void bwc::append_data_to(std::string & s) const
100 append(s, to_string(time_utc_));
101 append(s, to_string(lat_));
102 append(s, to_string(lat_hem_));
103 append(s, to_string(lon_));
104 append(s, to_string(lon_hem_));
105 append(s, to_string(bearing_true_));
106 append(s, to_string(bearing_true_ref_));
107 append(s, to_string(bearing_mag_));
108 append(s, to_string(bearing_mag_ref_));
109 append(s, to_string(distance_));
110 append(s, to_string_if(unit::distance::nm, distance_));
111 append(s, to_string(waypoint_id_));
112 append(s, to_string(mode_ind_));