diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.cpp | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..e699c6a --- /dev/null +++ b/src/main.cpp | |||
| @@ -0,0 +1,86 @@ | |||
| 1 | #include <asio.hpp> | ||
| 2 | #include <ctime> | ||
| 3 | #include <iostream> | ||
| 4 | #include <string> | ||
| 5 | |||
| 6 | #include "minmea.h" | ||
| 7 | |||
| 8 | using namespace std; | ||
| 9 | |||
| 10 | // constexpr auto PORT = "/dev/cuaU0"; | ||
| 11 | constexpr auto PORT = "/dev/ugen0.14"; | ||
| 12 | constexpr auto BAUDRATE = 9600; | ||
| 13 | |||
| 14 | namespace { | ||
| 15 | |||
| 16 | auto | ||
| 17 | read_line(asio::serial_port& com) | ||
| 18 | { | ||
| 19 | for ( string result;; ) { | ||
| 20 | char chr = 0; | ||
| 21 | |||
| 22 | asio::read(com, asio::buffer(&chr, 1)); | ||
| 23 | switch ( chr ) { | ||
| 24 | case '\r': | ||
| 25 | break; | ||
| 26 | case '\n': | ||
| 27 | return result; | ||
| 28 | default: | ||
| 29 | result += chr; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | string | ||
| 35 | get_gps_timestamp(const minmea_time& time, const minmea_date& date) | ||
| 36 | { | ||
| 37 | tm tm_utc{}; | ||
| 38 | ::minmea_getdatetime(&tm_utc, &date, &time); | ||
| 39 | |||
| 40 | auto timestamp = ::timegm(&tm_utc); | ||
| 41 | |||
| 42 | tm tm_local{}; | ||
| 43 | (void) ::localtime_r(×tamp, &tm_local); | ||
| 44 | |||
| 45 | static const auto buffer_size = 100; | ||
| 46 | array<char, buffer_size> buffer{}; | ||
| 47 | |||
| 48 | (void) ::strftime(buffer.data(), buffer.size(), "%Y-%m-%d %H:%M:%S", &tm_local); | ||
| 49 | |||
| 50 | return buffer.data(); | ||
| 51 | } | ||
| 52 | |||
| 53 | } // namespace | ||
| 54 | |||
| 55 | int | ||
| 56 | main() | ||
| 57 | { | ||
| 58 | try { | ||
| 59 | asio::io_context io_context{}; | ||
| 60 | asio::serial_port com(io_context, PORT); | ||
| 61 | com.set_option(asio::serial_port::baud_rate(BAUDRATE)); | ||
| 62 | |||
| 63 | for ( ;; ) { | ||
| 64 | const auto line = read_line(com); | ||
| 65 | const auto nmea_id = ::minmea_sentence_id(line.c_str(), true); | ||
| 66 | |||
| 67 | if ( nmea_id != MINMEA_SENTENCE_RMC ) { | ||
| 68 | continue; | ||
| 69 | } | ||
| 70 | |||
| 71 | minmea_sentence_rmc frame{}; | ||
| 72 | if ( !::minmea_parse_rmc(&frame, line.c_str()) || !frame.valid ) { | ||
| 73 | continue; | ||
| 74 | } | ||
| 75 | |||
| 76 | auto timestamp = get_gps_timestamp(frame.time, frame.date); | ||
| 77 | |||
| 78 | cout << timestamp << ": $RMC floating point degree coordinates and speed: (" | ||
| 79 | << ::minmea_tocoord(&frame.latitude) << "," << ::minmea_tocoord(&frame.longitude) << ") " | ||
| 80 | << ::minmea_tofloat(&frame.speed) << '\n'; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | catch ( exception& e ) { | ||
| 84 | cerr << "io-error: " << e.what() << '\n'; | ||
| 85 | } | ||
| 86 | } | ||
