From f3989050e8265c1330524bb652d261ee478ec831 Mon Sep 17 00:00:00 2001 From: Jared Boone Date: Fri, 15 Jan 2016 15:25:32 -0800 Subject: [PATCH] Clean up AIS lat/lon types and formatting. --- firmware/application/ais_app.cpp | 37 ++++++++++++++++++----- firmware/application/ais_app.hpp | 4 +-- firmware/common/ais_packet.cpp | 4 +-- firmware/common/ais_packet.hpp | 50 ++++++++++++++++++++++++++++++-- 4 files changed, 82 insertions(+), 13 deletions(-) diff --git a/firmware/application/ais_app.cpp b/firmware/application/ais_app.cpp index 19aa6ceac..f88ba79e9 100644 --- a/firmware/application/ais_app.cpp +++ b/firmware/application/ais_app.cpp @@ -33,11 +33,34 @@ using namespace portapack; namespace ais { namespace format { -static std::string latlon_normalized(const int32_t normalized) { - const int32_t t = (normalized * 5) / 3; - const int32_t degrees = t / (100 * 10000); - const int32_t fraction = std::abs(t) % (100 * 10000); - return to_string_dec_int(degrees) + "." + to_string_dec_int(fraction, 6, '0'); +static std::string latlon_abs_normalized(const int32_t normalized) { + const uint32_t normalized_abs = std::abs(normalized); + const uint32_t t = (normalized_abs * 5) / 3; + const uint32_t degrees = t / (100 * 10000); + const uint32_t fraction = t % (100 * 10000); + return to_string_dec_uint(degrees) + "." + to_string_dec_uint(fraction, 6, '0'); +} + +static std::string latitude(const Latitude value) { + if( value.is_not_available() ) { + return "not available"; + } else if( value.is_valid() ) { + const auto normalized = value.normalized(); + return latlon_abs_normalized(normalized) + ((normalized < 0) ? "S" : "N"); + } else { + return "invalid"; + } +} + +static std::string longitude(const Longitude value) { + if( value.is_not_available() ) { + return "not available"; + } else if( value.is_valid() ) { + const auto normalized = value.normalized(); + return latlon_abs_normalized(normalized) + ((normalized < 0) ? "W" : "E"); + } else { + return "invalid"; + } } static std::string mmsi( @@ -379,8 +402,8 @@ void AISRecentEntryDetailView::paint(Painter& painter) { field_rect = draw_field(painter, field_rect, s, "Name", entry_.name); field_rect = draw_field(painter, field_rect, s, "Call", entry_.call_sign); field_rect = draw_field(painter, field_rect, s, "Dest", entry_.destination); - field_rect = draw_field(painter, field_rect, s, "Lat ", ais::format::latlon_normalized(entry_.last_position.latitude) + "N"); - field_rect = draw_field(painter, field_rect, s, "Lon ", ais::format::latlon_normalized(entry_.last_position.longitude) + "E"); + field_rect = draw_field(painter, field_rect, s, "Lat ", ais::format::latitude(entry_.last_position.latitude)); + field_rect = draw_field(painter, field_rect, s, "Lon ", ais::format::longitude(entry_.last_position.longitude)); field_rect = draw_field(painter, field_rect, s, "Stat", ais::format::navigational_status(entry_.navigational_status)); field_rect = draw_field(painter, field_rect, s, "Rx #", to_string_dec_uint(entry_.received_count)); field_rect = draw_field(painter, field_rect, s, "RoT ", ais::format::rate_of_turn(entry_.last_position.rate_of_turn)); diff --git a/firmware/application/ais_app.hpp b/firmware/application/ais_app.hpp index 31576f2d5..ee7d991f8 100644 --- a/firmware/application/ais_app.hpp +++ b/firmware/application/ais_app.hpp @@ -40,8 +40,8 @@ using namespace lpc43xx; struct AISPosition { rtc::RTC timestamp { }; - ais::Latitude latitude { 0 }; - ais::Longitude longitude { 0 }; + ais::Latitude latitude; + ais::Longitude longitude; ais::RateOfTurn rate_of_turn { -128 }; ais::SpeedOverGround speed_over_ground { 1023 }; ais::CourseOverGround course_over_ground { 3600 }; diff --git a/firmware/common/ais_packet.cpp b/firmware/common/ais_packet.cpp index 9dcf76588..dcd7cddd9 100644 --- a/firmware/common/ais_packet.cpp +++ b/firmware/common/ais_packet.cpp @@ -178,13 +178,13 @@ DateTime Packet::datetime(const size_t start_bit) const { Latitude Packet::latitude(const size_t start_bit) const { // Shifting and dividing is to sign-extend the source field. // TODO: There's probably a more elegant way to do it. - return static_cast(field_.read(start_bit, 27) << 5) / 32; + return field_.read(start_bit, 27); } Longitude Packet::longitude(const size_t start_bit) const { // Shifting and dividing is to sign-extend the source field. // TODO: There's probably a more elegant way to do it. - return static_cast(field_.read(start_bit, 28) << 4) / 16; + return field_.read(start_bit, 28); } bool Packet::crc_ok() const { diff --git a/firmware/common/ais_packet.hpp b/firmware/common/ais_packet.hpp index bb0fda4c8..47bf902ef 100644 --- a/firmware/common/ais_packet.hpp +++ b/firmware/common/ais_packet.hpp @@ -40,8 +40,54 @@ struct DateTime { uint8_t second; }; -using Latitude = int32_t; -using Longitude = int32_t; +template +struct LatLonBase { + constexpr LatLonBase( + ) : LatLonBase { raw_not_available } + { + } + + constexpr LatLonBase( + const int32_t raw + ) : raw_ { raw } + { + } + + constexpr LatLonBase( + const LatLonBase& other + ) : raw_ { other.raw_ } + { + } + + int32_t normalized() const { + return static_cast(raw() << sign_extend_shift) / (1 << sign_extend_shift); + } + + int32_t raw() const { + return raw_; + } + + bool is_not_available() const { + return raw() == raw_not_available; + } + + bool is_valid() const { + return (normalized() >= raw_valid_min) && (normalized() <= raw_valid_max); + } + +private: + int32_t raw_; + + static constexpr size_t sign_extend_shift = 32 - FieldSize; + + static constexpr int32_t raw_not_available = NAValue; + static constexpr int32_t raw_valid_min = -DegMax * 60 * 10000; + static constexpr int32_t raw_valid_max = DegMax * 60 * 10000; +}; + +using Latitude = LatLonBase<27, 90, 0x3412140>; +using Longitude = LatLonBase<28, 180, 0x6791AC0>; + using RateOfTurn = int8_t; using SpeedOverGround = uint16_t; using CourseOverGround = uint16_t;