2017-07-14 05:02:21 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2017 Furrtek
|
2023-11-10 11:22:29 -05:00
|
|
|
* Copyright (C) 2023 Kyle Reed
|
2023-05-18 16:16:05 -04:00
|
|
|
*
|
2017-07-14 05:02:21 -04:00
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
#include <algorithm>
|
2021-06-23 14:54:34 -04:00
|
|
|
|
2017-07-14 05:02:21 -04:00
|
|
|
#include "ui_adsb_rx.hpp"
|
|
|
|
#include "ui_alphanum.hpp"
|
|
|
|
|
|
|
|
#include "baseband_api.hpp"
|
|
|
|
#include "portapack_persistent_memory.hpp"
|
2023-11-10 11:22:29 -05:00
|
|
|
#include "rtc_time.hpp"
|
|
|
|
#include "string_format.hpp"
|
2017-07-14 05:02:21 -04:00
|
|
|
|
|
|
|
using namespace portapack;
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
static std::string get_map_tag(const AircraftRecentEntry& entry) {
|
|
|
|
return trimr(entry.callsign.empty() ? entry.icao_str : entry.callsign);
|
|
|
|
}
|
2023-10-11 01:44:56 -04:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
template <>
|
2017-08-17 07:56:47 -04:00
|
|
|
void RecentEntriesTable<AircraftRecentEntries>::draw(
|
2023-05-18 16:16:05 -04:00
|
|
|
const Entry& entry,
|
|
|
|
const Rect& target_rect,
|
|
|
|
Painter& painter,
|
|
|
|
const Style& style) {
|
|
|
|
Color target_color;
|
2023-08-25 21:01:37 -04:00
|
|
|
std::string entry_string;
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
switch (entry.state) {
|
|
|
|
case ADSBAgeState::Invalid:
|
|
|
|
case ADSBAgeState::Current:
|
|
|
|
entry_string = "";
|
|
|
|
target_color = Color::green();
|
|
|
|
break;
|
|
|
|
case ADSBAgeState::Recent:
|
|
|
|
entry_string = STR_COLOR_LIGHT_GREY;
|
|
|
|
target_color = Color::light_grey();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
entry_string = STR_COLOR_DARK_GREY;
|
|
|
|
target_color = Color::grey();
|
|
|
|
};
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
entry_string +=
|
2023-11-10 11:22:29 -05:00
|
|
|
(entry.callsign.empty() ? entry.icao_str + " " : entry.callsign + " ") +
|
2023-05-18 16:16:05 -04:00
|
|
|
to_string_dec_uint((unsigned int)(entry.pos.altitude / 100), 4) +
|
|
|
|
to_string_dec_uint((unsigned int)entry.velo.speed, 4) +
|
|
|
|
to_string_dec_uint((unsigned int)(entry.amp >> 9), 4) + " " +
|
|
|
|
(entry.hits <= 999 ? to_string_dec_uint(entry.hits, 3) + " " : "1k+ ") +
|
|
|
|
to_string_dec_uint(entry.age, 4);
|
|
|
|
|
|
|
|
painter.draw_string(
|
|
|
|
target_rect.location(),
|
|
|
|
style,
|
|
|
|
entry_string);
|
|
|
|
|
|
|
|
if (entry.pos.valid)
|
2023-11-10 11:22:29 -05:00
|
|
|
painter.draw_bitmap(target_rect.location() + Point(8 * 8, 0),
|
|
|
|
bitmap_target, target_color, style.background);
|
2017-07-23 07:20:32 -04:00
|
|
|
}
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
/* ADSBLogger ********************************************/
|
2017-11-08 15:23:06 -05:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
void ADSBLogger::log(const ADSBLogEntry& log_entry) {
|
|
|
|
std::string log_line;
|
|
|
|
log_line.reserve(100);
|
|
|
|
|
|
|
|
log_line = log_entry.raw_data;
|
|
|
|
log_line += "ICAO:" + log_entry.icao;
|
|
|
|
|
|
|
|
if (!log_entry.callsign.empty())
|
|
|
|
log_line += " " + log_entry.callsign;
|
2021-12-01 17:23:40 -05:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
if (log_entry.pos.valid)
|
|
|
|
log_line += " Alt:" + to_string_dec_int(log_entry.pos.altitude) +
|
|
|
|
" Lat:" + to_string_decimal(log_entry.pos.latitude, 7) +
|
|
|
|
" Lon:" + to_string_decimal(log_entry.pos.longitude, 7);
|
|
|
|
|
|
|
|
if (log_entry.vel.valid)
|
|
|
|
log_line += " Type:" + to_string_dec_uint(log_entry.vel_type) +
|
|
|
|
" Hdg:" + to_string_dec_uint(log_entry.vel.heading) +
|
|
|
|
" Spd: " + to_string_dec_int(log_entry.vel.speed);
|
|
|
|
|
|
|
|
log_file.write_entry(log_line);
|
2021-12-01 17:23:40 -05:00
|
|
|
}
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
/* ADSBRxAircraftDetailsView *****************************/
|
|
|
|
|
2021-12-01 17:23:40 -05:00
|
|
|
ADSBRxAircraftDetailsView::ADSBRxAircraftDetailsView(
|
2023-05-18 16:16:05 -04:00
|
|
|
NavigationView& nav,
|
2023-11-10 11:22:29 -05:00
|
|
|
const AircraftRecentEntry& entry) {
|
|
|
|
add_children(
|
|
|
|
{&labels,
|
|
|
|
&text_icao_address,
|
|
|
|
&text_registration,
|
|
|
|
&text_manufacturer,
|
|
|
|
&text_model,
|
|
|
|
&text_type,
|
|
|
|
&text_number_of_engines,
|
|
|
|
&text_engine_type,
|
|
|
|
&text_owner,
|
|
|
|
&text_operator,
|
|
|
|
&button_close});
|
|
|
|
|
|
|
|
text_icao_address.set(entry.icao_str);
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
// Try getting the aircraft information from icao24.db
|
2024-02-05 13:06:29 -05:00
|
|
|
database db{};
|
|
|
|
database::AircraftDBRecord aircraft_record;
|
2023-11-10 11:22:29 -05:00
|
|
|
auto return_code = db.retrieve_aircraft_record(&aircraft_record, entry.icao_str);
|
2023-05-18 16:16:05 -04:00
|
|
|
switch (return_code) {
|
|
|
|
case DATABASE_RECORD_FOUND:
|
|
|
|
text_registration.set(aircraft_record.aircraft_registration);
|
|
|
|
text_manufacturer.set(aircraft_record.aircraft_manufacturer);
|
|
|
|
text_model.set(aircraft_record.aircraft_model);
|
|
|
|
text_owner.set(aircraft_record.aircraft_owner);
|
|
|
|
text_operator.set(aircraft_record.aircraft_operator);
|
2023-11-10 11:22:29 -05:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
// Check for ICAO type, e.g. L2J
|
|
|
|
if (strlen(aircraft_record.icao_type) == 3) {
|
|
|
|
switch (aircraft_record.icao_type[0]) {
|
|
|
|
case 'L':
|
|
|
|
text_type.set("Landplane");
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
text_type.set("Seaplane");
|
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
text_type.set("Amphibian");
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
text_type.set("Helicopter");
|
|
|
|
break;
|
|
|
|
case 'G':
|
|
|
|
text_type.set("Gyrocopter");
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
text_type.set("Tilt-wing aircraft");
|
|
|
|
break;
|
|
|
|
}
|
2023-11-10 11:22:29 -05:00
|
|
|
|
|
|
|
text_number_of_engines.set(std::string{1, aircraft_record.icao_type[1]});
|
2023-05-18 16:16:05 -04:00
|
|
|
switch (aircraft_record.icao_type[2]) {
|
|
|
|
case 'P':
|
|
|
|
text_engine_type.set("Piston engine");
|
|
|
|
break;
|
|
|
|
case 'T':
|
|
|
|
text_engine_type.set("Turboprop/Turboshaft engine");
|
|
|
|
break;
|
|
|
|
case 'J':
|
|
|
|
text_engine_type.set("Jet engine");
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
text_engine_type.set("Electric engine");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-11-10 11:22:29 -05:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
// Check for ICAO type designator
|
|
|
|
else if (strlen(aircraft_record.icao_type) == 4) {
|
|
|
|
if (strcmp(aircraft_record.icao_type, "SHIP") == 0)
|
|
|
|
text_type.set("Airship");
|
|
|
|
else if (strcmp(aircraft_record.icao_type, "BALL") == 0)
|
|
|
|
text_type.set("Balloon");
|
|
|
|
else if (strcmp(aircraft_record.icao_type, "GLID") == 0)
|
|
|
|
text_type.set("Glider / sailplane");
|
|
|
|
else if (strcmp(aircraft_record.icao_type, "ULAC") == 0)
|
|
|
|
text_type.set("Micro/ultralight aircraft");
|
|
|
|
else if (strcmp(aircraft_record.icao_type, "GYRO") == 0)
|
|
|
|
text_type.set("Micro/ultralight autogyro");
|
|
|
|
else if (strcmp(aircraft_record.icao_type, "UHEL") == 0)
|
|
|
|
text_type.set("Micro/ultralight helicopter");
|
|
|
|
else if (strcmp(aircraft_record.icao_type, "SHIP") == 0)
|
|
|
|
text_type.set("Airship");
|
|
|
|
else if (strcmp(aircraft_record.icao_type, "PARA") == 0)
|
|
|
|
text_type.set("Powered parachute/paraplane");
|
|
|
|
}
|
|
|
|
break;
|
2023-11-10 11:22:29 -05:00
|
|
|
|
2024-02-05 14:13:06 -05:00
|
|
|
case DATABASE_RECORD_NOT_FOUND:
|
|
|
|
// Defaults should be filled by the constructor
|
|
|
|
break;
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
case DATABASE_NOT_FOUND:
|
|
|
|
text_manufacturer.set("No icao24.db file");
|
|
|
|
break;
|
|
|
|
}
|
2023-11-10 11:22:29 -05:00
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
button_close.on_select = [&nav](Button&) {
|
|
|
|
nav.pop();
|
|
|
|
};
|
2017-08-28 02:05:31 -04:00
|
|
|
}
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
void ADSBRxAircraftDetailsView::focus() {
|
|
|
|
button_close.focus();
|
2017-08-28 02:05:31 -04:00
|
|
|
}
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
/* ADSBRxDetailsView *************************************/
|
2017-08-28 02:05:31 -04:00
|
|
|
|
|
|
|
ADSBRxDetailsView::ADSBRxDetailsView(
|
2023-05-18 16:16:05 -04:00
|
|
|
NavigationView& nav,
|
2023-11-10 11:22:29 -05:00
|
|
|
const AircraftRecentEntry& entry)
|
|
|
|
: entry_(entry) {
|
|
|
|
add_children(
|
|
|
|
{&labels,
|
|
|
|
&text_icao_address,
|
|
|
|
&text_callsign,
|
|
|
|
&text_last_seen,
|
|
|
|
&text_airline,
|
|
|
|
&text_country,
|
|
|
|
&text_infos,
|
|
|
|
&text_info2,
|
|
|
|
&text_frame_pos_even,
|
|
|
|
&text_frame_pos_odd,
|
|
|
|
&button_aircraft_details,
|
|
|
|
&button_see_map});
|
|
|
|
|
|
|
|
text_icao_address.set(entry_.icao_str);
|
2023-05-18 16:16:05 -04:00
|
|
|
|
|
|
|
button_aircraft_details.on_select = [this, &nav](Button&) {
|
2023-11-10 11:22:29 -05:00
|
|
|
aircraft_details_view_ = nav.push<ADSBRxAircraftDetailsView>(entry_);
|
|
|
|
nav.set_on_pop([this]() {
|
|
|
|
aircraft_details_view_ = nullptr;
|
|
|
|
refresh_ui();
|
|
|
|
});
|
2023-05-18 16:16:05 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
button_see_map.on_select = [this, &nav](Button&) {
|
2023-11-10 11:22:29 -05:00
|
|
|
geomap_view_ = nav.push<GeoMapView>(
|
|
|
|
get_map_tag(entry_),
|
|
|
|
entry_.pos.altitude,
|
|
|
|
GeoPos::alt_unit::FEET,
|
2024-01-05 07:44:30 -05:00
|
|
|
GeoPos::spd_unit::MPH,
|
2023-11-10 11:22:29 -05:00
|
|
|
entry_.pos.latitude,
|
|
|
|
entry_.pos.longitude,
|
|
|
|
entry_.velo.heading);
|
|
|
|
nav.set_on_pop([this]() {
|
|
|
|
geomap_view_ = nullptr;
|
|
|
|
refresh_ui();
|
|
|
|
});
|
2023-05-18 16:16:05 -04:00
|
|
|
};
|
2023-11-10 11:22:29 -05:00
|
|
|
|
|
|
|
refresh_ui();
|
2017-08-28 02:05:31 -04:00
|
|
|
};
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
void ADSBRxDetailsView::focus() {
|
|
|
|
button_see_map.focus();
|
2017-07-14 05:02:21 -04:00
|
|
|
}
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
void ADSBRxDetailsView::update(const AircraftRecentEntry& entry) {
|
|
|
|
entry_ = entry;
|
|
|
|
|
|
|
|
if (aircraft_details_view_) {
|
|
|
|
// AC Details view is showing, nothing to update.
|
|
|
|
} else if (geomap_view_) {
|
|
|
|
// Map is showing, update the current item.
|
|
|
|
geomap_view_->update_tag(get_map_tag(entry_));
|
2024-01-05 07:44:30 -05:00
|
|
|
geomap_view_->update_position(entry.pos.latitude, entry.pos.longitude, entry.velo.heading, entry.pos.altitude, entry.velo.speed);
|
2023-11-10 11:22:29 -05:00
|
|
|
} else {
|
|
|
|
// Details is showing, update details.
|
|
|
|
refresh_ui();
|
|
|
|
}
|
2017-07-14 05:02:21 -04:00
|
|
|
}
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
void ADSBRxDetailsView::clear_map_markers() {
|
|
|
|
if (geomap_view_)
|
|
|
|
geomap_view_->clear_markers();
|
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
bool ADSBRxDetailsView::add_map_marker(const AircraftRecentEntry& entry) {
|
|
|
|
// Map not shown, can't add markers.
|
|
|
|
if (!geomap_view_)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
GeoMarker marker{};
|
|
|
|
marker.lon = entry.pos.longitude;
|
|
|
|
marker.lat = entry.pos.latitude;
|
|
|
|
marker.angle = entry.velo.heading;
|
|
|
|
marker.tag = get_map_tag(entry);
|
|
|
|
|
|
|
|
auto markerStored = geomap_view_->store_marker(marker);
|
|
|
|
return markerStored == MARKER_STORED;
|
2021-10-11 05:14:16 -04:00
|
|
|
}
|
|
|
|
|
2024-01-11 11:46:35 -05:00
|
|
|
void ADSBRxDetailsView::on_gps(const GPSPosDataMessage* msg) {
|
|
|
|
if (!geomap_view_)
|
|
|
|
return;
|
|
|
|
geomap_view_->update_my_position(msg->lat, msg->lon, msg->altitude);
|
|
|
|
}
|
|
|
|
void ADSBRxDetailsView::on_orientation(const OrientationDataMessage* msg) {
|
|
|
|
if (!geomap_view_)
|
|
|
|
return;
|
|
|
|
geomap_view_->update_my_orientation(msg->angle);
|
|
|
|
}
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
void ADSBRxDetailsView::refresh_ui() {
|
2024-02-05 13:06:29 -05:00
|
|
|
// The following won't change for a given airborne aircraft.
|
|
|
|
// Try getting the airline's name from airlines.db.
|
|
|
|
if (!airline_checked && !entry_.callsign.empty()) {
|
|
|
|
airline_checked = true;
|
|
|
|
|
|
|
|
database db;
|
|
|
|
database::AirlinesDBRecord airline_record;
|
|
|
|
std::string airline_code = entry_.callsign.substr(0, 3);
|
|
|
|
auto return_code = db.retrieve_airline_record(&airline_record, airline_code);
|
|
|
|
|
|
|
|
switch (return_code) {
|
|
|
|
case DATABASE_RECORD_FOUND:
|
|
|
|
text_airline.set(airline_record.airline);
|
|
|
|
text_country.set(airline_record.country);
|
|
|
|
break;
|
|
|
|
case DATABASE_RECORD_NOT_FOUND:
|
|
|
|
// text_airline.set("-"); // It's what it is constructed with
|
|
|
|
// text_country.set("-"); // It's what it is constructed with
|
|
|
|
break;
|
|
|
|
case DATABASE_NOT_FOUND:
|
|
|
|
text_airline.set("No airlines.db file");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
auto age = entry_.age;
|
|
|
|
if (age < 60)
|
|
|
|
text_last_seen.set(to_string_dec_uint(age) + " seconds ago");
|
|
|
|
else
|
|
|
|
text_last_seen.set(to_string_dec_uint(age / 60) + " minutes ago");
|
|
|
|
|
|
|
|
text_callsign.set(entry_.callsign);
|
|
|
|
text_infos.set(entry_.info_string);
|
|
|
|
if (entry_.velo.heading < 360 && entry_.velo.speed >= 0)
|
|
|
|
text_info2.set("Hdg:" + to_string_dec_uint(entry_.velo.heading) +
|
|
|
|
" Spd:" + to_string_dec_int(entry_.velo.speed));
|
|
|
|
else
|
|
|
|
text_info2.set("");
|
2021-10-11 05:14:16 -04:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
text_frame_pos_even.set(to_string_hex_array(entry_.frame_pos_even.get_raw_data(), 14));
|
|
|
|
text_frame_pos_odd.set(to_string_hex_array(entry_.frame_pos_odd.get_raw_data(), 14));
|
2021-10-11 05:14:16 -04:00
|
|
|
}
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
/* ADSBRxView ********************************************/
|
|
|
|
|
|
|
|
ADSBRxView::ADSBRxView(NavigationView& nav) {
|
|
|
|
baseband::run_image(portapack::spi_flash::image_tag_adsb_rx);
|
|
|
|
add_children(
|
|
|
|
{&labels,
|
|
|
|
&field_lna,
|
|
|
|
&field_vga,
|
|
|
|
&field_rf_amp,
|
|
|
|
&rssi,
|
|
|
|
&recent_entries_view,
|
|
|
|
&status_frame,
|
|
|
|
&status_good_frame});
|
|
|
|
|
|
|
|
recent_entries_view.set_parent_rect({0, 16, 240, 272});
|
|
|
|
recent_entries_view.on_select = [this, &nav](const AircraftRecentEntry& entry) {
|
|
|
|
detail_key = entry.key();
|
|
|
|
details_view = nav.push<ADSBRxDetailsView>(entry);
|
|
|
|
|
|
|
|
nav.set_on_pop([this]() {
|
|
|
|
detail_key = AircraftRecentEntry::invalid_key;
|
|
|
|
details_view = nullptr;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
signal_token_tick_second = rtc_time::signal_tick_second += [this]() {
|
|
|
|
on_tick_second();
|
|
|
|
};
|
|
|
|
|
|
|
|
logger = std::make_unique<ADSBLogger>();
|
|
|
|
logger->append(LOG_ROOT_DIR "/ADSB.TXT");
|
|
|
|
|
|
|
|
receiver_model.enable();
|
|
|
|
baseband::set_adsb();
|
2023-02-28 14:17:57 -05:00
|
|
|
}
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
ADSBRxView::~ADSBRxView() {
|
|
|
|
rtc_time::signal_tick_second -= signal_token_tick_second;
|
|
|
|
receiver_model.disable();
|
|
|
|
baseband::shutdown();
|
2021-10-11 05:14:16 -04:00
|
|
|
}
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
void ADSBRxView::focus() {
|
|
|
|
field_vga.focus();
|
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
void ADSBRxView::on_frame(const ADSBFrameMessage* message) {
|
2023-05-18 16:16:05 -04:00
|
|
|
auto frame = message->frame;
|
|
|
|
uint32_t ICAO_address = frame.get_ICAO_address();
|
2023-11-10 11:22:29 -05:00
|
|
|
status_frame.toggle();
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
// Bad frame, skip it.
|
|
|
|
if (!frame.check_CRC() || ICAO_address == 0)
|
|
|
|
return;
|
2021-11-01 16:37:27 -04:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
ADSBLogEntry log_entry;
|
|
|
|
status_good_frame.toggle();
|
|
|
|
|
|
|
|
rtc::RTC datetime;
|
2024-01-21 13:47:28 -05:00
|
|
|
rtcGetTime(&RTCD1, &datetime); // Reading RTC directly to avoid DST transitions when calculating delta
|
2023-11-10 11:22:29 -05:00
|
|
|
frame.set_rx_timestamp(datetime.minute() * 60 + datetime.second());
|
|
|
|
|
|
|
|
// NB: Reference to update entry in-place.
|
|
|
|
auto& entry = find_or_create_entry(ICAO_address);
|
|
|
|
entry.inc_hit();
|
|
|
|
entry.reset_age();
|
|
|
|
|
|
|
|
// Store smoothed amplitude on updates.
|
|
|
|
entry.amp = entry.hits == 0
|
|
|
|
? message->amp
|
|
|
|
: ((entry.amp * 15) + message->amp) >> 4;
|
|
|
|
|
|
|
|
log_entry.raw_data = to_string_hex_array(frame.get_raw_data(), 14);
|
|
|
|
log_entry.icao = entry.icao_str;
|
|
|
|
|
|
|
|
if (frame.get_DF() == DF_ADSB) {
|
|
|
|
uint8_t msg_type = frame.get_msg_type();
|
|
|
|
uint8_t msg_sub = frame.get_msg_sub();
|
|
|
|
uint8_t* raw_data = frame.get_raw_data();
|
|
|
|
|
|
|
|
// 4: // surveillance, altitude reply
|
|
|
|
if ((msg_type >= AIRCRAFT_ID_L) && (msg_type <= AIRCRAFT_ID_H)) {
|
|
|
|
entry.set_callsign(decode_frame_id(frame));
|
|
|
|
log_entry.callsign = entry.callsign;
|
2017-11-08 15:23:06 -05:00
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
// 9:
|
|
|
|
// 18: // Extended squitter/non-transponder
|
|
|
|
// 21: // Comm-B, identity reply
|
|
|
|
// 20: // Comm-B, altitude reply
|
|
|
|
else if (((msg_type >= AIRBORNE_POS_BARO_L) && (msg_type <= AIRBORNE_POS_BARO_H)) ||
|
|
|
|
((msg_type >= AIRBORNE_POS_GPS_L) && (msg_type <= AIRBORNE_POS_GPS_H))) {
|
|
|
|
entry.set_frame_pos(frame, raw_data[6] & 4);
|
|
|
|
log_entry.pos = entry.pos;
|
|
|
|
|
|
|
|
if (entry.pos.valid) {
|
|
|
|
std::string str_info =
|
|
|
|
"Alt:" + to_string_dec_int(entry.pos.altitude) +
|
|
|
|
" Lat:" + to_string_decimal(entry.pos.latitude, 2) +
|
|
|
|
" Lon:" + to_string_decimal(entry.pos.longitude, 2);
|
|
|
|
|
|
|
|
entry.set_info_string(std::move(str_info));
|
2023-05-18 16:16:05 -04:00
|
|
|
}
|
2023-11-10 11:22:29 -05:00
|
|
|
|
|
|
|
} else if (msg_type == AIRBORNE_VEL && msg_sub >= VEL_GND_SUBSONIC && msg_sub <= VEL_AIR_SUPERSONIC) {
|
|
|
|
entry.set_frame_velo(frame);
|
|
|
|
log_entry.vel = entry.velo;
|
|
|
|
log_entry.vel_type = msg_sub;
|
2023-05-18 16:16:05 -04:00
|
|
|
}
|
|
|
|
}
|
2023-11-10 11:22:29 -05:00
|
|
|
|
|
|
|
logger->log(log_entry);
|
2017-07-14 05:02:21 -04:00
|
|
|
}
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
void ADSBRxView::on_tick_second() {
|
2023-11-10 11:22:29 -05:00
|
|
|
status_frame.reset();
|
|
|
|
status_good_frame.reset();
|
2023-02-28 14:17:57 -05:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
++tick_count;
|
|
|
|
++ticks_since_marker_refresh;
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
// Small list, update all at once.
|
|
|
|
if (recent.size() <= max_update_entries) {
|
|
|
|
update_recent_entries(/*age_delta*/ 1);
|
|
|
|
refresh_ui();
|
2023-10-11 01:44:56 -04:00
|
|
|
return;
|
2023-11-10 11:22:29 -05:00
|
|
|
}
|
2023-10-11 01:44:56 -04:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
// Too many items, split update work into two phases:
|
|
|
|
// Entry maintenance and UI update.
|
|
|
|
if ((tick_count & 1) == 0)
|
|
|
|
update_recent_entries(/*age_delta*/ 2);
|
|
|
|
else
|
|
|
|
refresh_ui();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ADSBRxView::refresh_ui() {
|
|
|
|
// There's only one ticks handler, but 3 UIs that need to be updated.
|
|
|
|
// This code will dispatch updates to the currently active view.
|
|
|
|
|
|
|
|
if (details_view) {
|
|
|
|
// The details view is showing, forward updates to that UI.
|
|
|
|
bool current_updated = false;
|
|
|
|
bool map_needs_update = false;
|
|
|
|
|
|
|
|
if (details_view->map_active()) {
|
|
|
|
// Is it time to clear and refresh the map's markers?
|
|
|
|
if (ticks_since_marker_refresh >= MARKER_UPDATE_SECONDS) {
|
|
|
|
map_needs_update = true;
|
|
|
|
ticks_since_marker_refresh = 0;
|
|
|
|
details_view->clear_map_markers();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Refresh map immediately once active.
|
|
|
|
ticks_since_marker_refresh = MARKER_UPDATE_SECONDS;
|
2023-05-18 16:16:05 -04:00
|
|
|
}
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
// Process the entries list.
|
|
|
|
for (const auto& entry : recent) {
|
|
|
|
// Found the entry being shown in details view. Update it.
|
|
|
|
if (entry.key() == detail_key) {
|
2023-05-18 16:16:05 -04:00
|
|
|
details_view->update(entry);
|
2023-11-10 11:22:29 -05:00
|
|
|
current_updated = true;
|
2023-05-18 16:16:05 -04:00
|
|
|
}
|
2023-11-10 11:22:29 -05:00
|
|
|
|
|
|
|
// NB: current entry also gets a marker so it shows up if map is panned.
|
|
|
|
if (map_needs_update && entry.pos.valid && entry.state <= ADSBAgeState::Recent) {
|
|
|
|
map_needs_update = details_view->add_map_marker(entry);
|
2023-05-18 16:16:05 -04:00
|
|
|
}
|
2021-10-11 05:14:16 -04:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
// Any work left to do?
|
|
|
|
if (current_updated && !map_needs_update)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Main page is the top view. Redraw the entries view.
|
2023-05-18 16:16:05 -04:00
|
|
|
recent_entries_view.set_dirty();
|
|
|
|
}
|
2017-08-27 16:03:17 -04:00
|
|
|
}
|
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
void ADSBRxView::update_recent_entries(int age_delta) {
|
|
|
|
for (auto& entry : recent)
|
|
|
|
entry.inc_age(age_delta);
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
// Sort and truncate the entries, grouped by state, newest first.
|
|
|
|
sort_entries_by_state();
|
|
|
|
truncate_entries(recent);
|
|
|
|
remove_expired_entries();
|
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
AircraftRecentEntry& ADSBRxView::find_or_create_entry(uint32_t ICAO_address) {
|
|
|
|
// Find ...
|
|
|
|
auto it = find(recent, ICAO_address);
|
|
|
|
if (it != recent.end())
|
|
|
|
return *it;
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
// ... or Create.
|
|
|
|
return recent.emplace_front(ICAO_address);
|
|
|
|
}
|
2023-05-18 16:16:05 -04:00
|
|
|
|
2023-11-10 11:22:29 -05:00
|
|
|
void ADSBRxView::sort_entries_by_state() {
|
|
|
|
recent.sort([](const auto& left, const auto& right) {
|
|
|
|
return (left.state < right.state);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void ADSBRxView::remove_expired_entries() {
|
|
|
|
// NB: Assumes entried are sorted with oldest last.
|
|
|
|
auto it = recent.rbegin();
|
|
|
|
auto end = recent.rend();
|
|
|
|
|
|
|
|
// Find the first !expired entry from the back.
|
|
|
|
while (it != end) {
|
|
|
|
if (it->state != ADSBAgeState::Expired)
|
|
|
|
break;
|
|
|
|
|
|
|
|
std::advance(it, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the range of expired items.
|
|
|
|
recent.erase(it.base(), recent.end());
|
2017-07-14 05:02:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace ui */
|