Clean up AIS lat/lon types and formatting.

This commit is contained in:
Jared Boone 2016-01-15 15:25:32 -08:00
parent 1e92d416c7
commit f3989050e8
4 changed files with 82 additions and 13 deletions

View File

@ -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));

View File

@ -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 };

View File

@ -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<int32_t>(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<int32_t>(field_.read(start_bit, 28) << 4) / 16;
return field_.read(start_bit, 28);
}
bool Packet::crc_ok() const {

View File

@ -40,8 +40,54 @@ struct DateTime {
uint8_t second;
};
using Latitude = int32_t;
using Longitude = int32_t;
template<size_t FieldSize, int32_t DegMax, uint32_t NAValue>
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<int32_t>(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;