Build: add GCC-13, Clang-14, Clang-15, Clang-16, Clang-17
[marnav.git] / src / marnav / nmea / lcd.cpp
blobee69452e899de17619c7ce93e2ec6c0558c638b5
1 #include <marnav/nmea/lcd.hpp>
2 #include <marnav/nmea/io.hpp>
3 #include <stdexcept>
5 namespace marnav::nmea
7 constexpr sentence_id lcd::ID;
8 constexpr const char * lcd::TAG;
10 constexpr int lcd::max_differences;
12 lcd::lcd()
13 : sentence(ID, TAG, talker::global_positioning_system)
14 , master_({0, 0})
18 lcd::lcd(talker talk, fields::const_iterator first, fields::const_iterator last)
19 : sentence(ID, TAG, talk)
21 if (std::distance(first, last) != 13)
22 throw std::invalid_argument{"invalid number of fields in lcd"};
24 read(*(first + 0), gri_);
25 read(*(first + 1), master_.snr);
26 read(*(first + 2), master_.ecd);
27 for (int i = 0; i < max_differences; ++i) {
28 std::optional<decltype(time_difference::snr)> snr;
29 std::optional<decltype(time_difference::ecd)> ecd;
30 read(*(first + (i * 2) + 3 + 0), snr);
31 read(*(first + (i * 2) + 3 + 1), ecd);
32 if (snr && ecd) {
33 time_diffs_[i] = time_difference{*snr, *ecd};
38 void lcd::check_index(int index) const
40 if ((index < 0) || (index >= max_differences)) {
41 throw std::out_of_range{"time difference index out of range"};
45 std::optional<lcd::time_difference> lcd::get_time_diff(int index) const
47 check_index(index);
48 return time_diffs_[index];
51 void lcd::set_time_diff(int index, time_difference t)
53 check_index(index);
54 time_diffs_[index] = t;
57 void lcd::append_data_to(std::string & s, const version &) const
59 append(s, to_string(gri_));
60 append(s, format(master_.snr, 3));
61 append(s, format(master_.ecd, 3));
62 for (int i = 0; i < max_differences; ++i) {
63 auto const & t = time_diffs_[i];
64 if (t) {
65 append(s, format(t->snr, 3));
66 append(s, format(t->ecd, 3));
67 } else {
68 append(s, "");
69 append(s, "");