Build: add GCC-13, Clang-14, Clang-15, Clang-16, Clang-17
[marnav.git] / examples / simple_seatalk_nmea_converter.cpp
blobe9230cbc6cea9efe8c2e29eb99c7e2f0bda80540
1 #include <marnav/nmea/nmea.hpp>
2 #include <marnav/nmea/dpt.hpp>
3 #include <marnav/seatalk/seatalk.hpp>
4 #include <marnav/seatalk/message_00.hpp>
5 #include <marnav-io/default_seatalk_reader.hpp>
6 #include <marnav-io/default_seatalk_serial.hpp>
7 #include <boost/asio.hpp>
8 #include <map>
10 using namespace marnav;
12 namespace marnav_example
14 static std::string conv_depth_below_transducer(const seatalk::message & msg)
16 using namespace marnav::seatalk;
18 const auto & m = message_cast<message_00>(msg);
20 nmea::dpt dpt;
21 // TODO: explicit type unnecessary once seatalk is also using units
22 dpt.set_depth_meter(units::meters{m.get_depth_meters()});
23 dpt.set_transducer_offset(units::meters{0.0});
24 return nmea::to_string(dpt);
28 int main(int, char **)
30 using namespace marnav_example;
31 using namespace marnav::io;
33 // mapping of conversion functions
34 static const std::map<seatalk::message_id,
35 std::function<std::string(const seatalk::message &)>>
36 CONV = {
37 {seatalk::message_id::depth_below_transducer, conv_depth_below_transducer},
40 // open output port
41 boost::asio::io_service io;
42 boost::asio::serial_port serial{io, "/dev/ttyUSB1"};
43 serial.set_option(boost::asio::serial_port_base::baud_rate(4800));
45 // open input port and reader
46 default_seatalk_reader reader{make_default_seatalk_serial("/dev/ttyUSB0")};
47 seatalk::raw data;
49 while (reader.read_message(data)) {
50 // receive message
51 auto msg = seatalk::make_message(data);
52 if (!msg)
53 continue;
55 // convert message to NMEA sentence, if known
56 std::string nmea;
57 auto func = CONV.find(msg->type());
58 if (func != CONV.end())
59 nmea = func->second(*msg);
61 // send NMEA sentence
62 if (nmea.size() > 0) {
63 nmea += "\r\n";
64 boost::asio::write(serial, boost::asio::buffer(nmea.c_str(), nmea.size()));
68 return 0;