2021-03-07 17:05:23 -05:00
/*
* Copyright ( C ) 2014 Jared Boone , ShareBrained Technology , Inc .
* Copyright ( C ) 2017 Furrtek
*
* 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 .
*/
# include "ui_aprs_rx.hpp"
# include "audio.hpp"
# include "rtc_time.hpp"
# include "baseband_api.hpp"
# include "string_format.hpp"
# include "portapack_persistent_memory.hpp"
using namespace portapack ;
void APRSLogger : : log_raw_data ( const std : : string & data ) {
2023-05-19 16:39:35 -04:00
log_file . write_entry ( data ) ;
2021-03-07 17:05:23 -05:00
}
namespace ui {
2023-05-18 16:16:05 -04:00
template < >
2021-03-07 17:05:23 -05:00
void RecentEntriesTable < APRSRecentEntries > : : draw (
2023-05-18 16:16:05 -04:00
const Entry & entry ,
const Rect & target_rect ,
Painter & painter ,
const Style & style ) {
Color target_color ;
// auto entry_age = entry.age;
target_color = Color : : green ( ) ;
2023-08-25 21:01:37 -04:00
std : : string entry_string = " " ;
2023-05-18 16:16:05 -04:00
entry_string + = entry . source_formatted ;
entry_string . append ( 10 - entry . source_formatted . size ( ) , ' ' ) ;
entry_string + = " " ;
entry_string + = ( entry . hits < = 999 ? to_string_dec_uint ( entry . hits , 4 ) : " 999+ " ) ;
entry_string + = " " ;
entry_string + = entry . time_string ;
painter . draw_string (
target_rect . location ( ) ,
style ,
entry_string ) ;
if ( entry . has_position ) {
painter . draw_bitmap ( target_rect . location ( ) + Point ( 12 * 8 , 0 ) , bitmap_target , target_color , style . background ) ;
}
2021-03-07 17:05:23 -05:00
}
void APRSRxView : : focus ( ) {
2023-05-18 16:16:05 -04:00
options_region . focus ( ) ;
2021-03-07 17:05:23 -05:00
}
2023-05-18 16:16:05 -04:00
APRSRxView : : APRSRxView ( NavigationView & nav , Rect parent_rect )
2023-06-18 15:11:04 -04:00
: View ( parent_rect ) , nav_ { nav } {
2023-05-18 16:16:05 -04:00
baseband : : run_image ( portapack : : spi_flash : : image_tag_aprs_rx ) ;
add_children ( { & rssi ,
& channel ,
& field_rf_amp ,
& field_lna ,
& field_vga ,
2023-06-05 14:09:50 -04:00
& field_volume ,
2023-05-18 16:16:05 -04:00
& options_region ,
& field_frequency ,
& record_view ,
& console } ) ;
// DEBUG
record_view . on_error = [ & nav ] ( std : : string message ) {
nav . display_modal ( " Error " , message ) ;
} ;
record_view . set_sampling_rate ( 24000 ) ;
options_region . on_change = [ this ] ( size_t , int32_t i ) {
if ( i = = 0 ) {
field_frequency . set_value ( 144390000 ) ;
} else if ( i = = 1 ) {
field_frequency . set_value ( 144800000 ) ;
} else if ( i = = 2 ) {
field_frequency . set_value ( 145175000 ) ;
} else if ( i = = 3 ) {
field_frequency . set_value ( 144575000 ) ;
2024-01-25 15:10:43 -05:00
} else if ( i = = 4 ) {
field_frequency . set_value ( 145825000 ) ;
2023-05-18 16:16:05 -04:00
}
} ;
field_frequency . set_step ( 100 ) ;
options_region . set_selected_index ( 0 , true ) ;
logger = std : : make_unique < APRSLogger > ( ) ;
if ( logger )
logger - > append ( LOG_ROOT_DIR " /APRS.TXT " ) ;
baseband : : set_aprs ( 1200 ) ;
audio : : set_rate ( audio : : Rate : : Hz_24000 ) ;
audio : : output : : start ( ) ;
receiver_model . enable ( ) ;
2021-03-07 17:05:23 -05:00
}
2023-05-18 16:16:05 -04:00
void APRSRxView : : on_packet ( const APRSPacketMessage * message ) {
std : : string str_console = " \x1B " ;
aprs : : APRSPacket packet = message - > packet ;
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
std : : string stream_text = packet . get_stream_text ( ) ;
str_console + = ( char ) ( ( console_color + + & 3 ) + 9 ) ;
str_console + = stream_text ;
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
if ( logger ) {
logger - > log_raw_data ( stream_text ) ;
}
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
// if(reset_console){ //having more than one console causes issues when switching tabs where one is disabled, and the other enabled breaking the scoll setup.
// console.on_hide();
// console.on_show();
// reset_console = false;
// }
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
console . writeln ( str_console ) ;
2021-03-07 17:05:23 -05:00
}
void APRSRxView : : on_data ( uint32_t value , bool is_data ) {
2023-05-18 16:16:05 -04:00
std : : string str_console = " \x1B " ;
std : : string str_byte = " " ;
if ( is_data ) {
// Colorize differently after message splits
str_console + = ( char ) ( ( console_color & 3 ) + 9 ) ;
if ( value = = ' \n ' ) {
// Message split
console . writeln ( " " ) ;
console_color + + ;
if ( logger ) {
logger - > log_raw_data ( str_log ) ;
str_log = " " ;
}
} else {
if ( ( value > = 32 ) & & ( value < 127 ) ) {
str_console + = ( char ) value ; // Printable
str_byte + = ( char ) value ;
} else {
str_console + = " [ " + to_string_hex ( value , 2 ) + " ] " ; // Not printable
str_byte + = " [ " + to_string_hex ( value , 2 ) + " ] " ;
}
console . write ( str_console ) ;
if ( logger ) str_log + = str_byte ;
}
} else {
}
2021-03-07 17:05:23 -05:00
}
2023-05-18 16:16:05 -04:00
void APRSRxView : : on_show ( ) {
// some bug where the display scroll area is set to the entire screen when switching from the list tab with details showing back to the stream view.
// reset_console = true;
2021-03-07 17:05:23 -05:00
}
APRSRxView : : ~ APRSRxView ( ) {
2023-05-18 16:16:05 -04:00
audio : : output : : stop ( ) ;
receiver_model . disable ( ) ;
baseband : : shutdown ( ) ;
2021-03-07 17:05:23 -05:00
}
void APRSTableView : : on_show_list ( ) {
2023-05-18 16:16:05 -04:00
details_view . hidden ( true ) ;
recent_entries_view . hidden ( false ) ;
send_updates = false ;
focus ( ) ;
2021-03-07 17:05:23 -05:00
}
void APRSTableView : : on_show_detail ( const APRSRecentEntry & entry ) {
2023-05-18 16:16:05 -04:00
recent_entries_view . hidden ( true ) ;
details_view . hidden ( false ) ;
details_view . set_entry ( entry ) ;
details_view . update ( ) ;
details_view . focus ( ) ;
detailed_entry_key = entry . key ( ) ;
send_updates = true ;
2021-03-07 17:05:23 -05:00
}
2023-05-18 16:16:05 -04:00
APRSTableView : : APRSTableView ( NavigationView & nav , Rect parent_rec )
: View ( parent_rec ) , nav_ { nav } {
add_children ( { & recent_entries_view ,
& details_view } ) ;
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
hidden ( true ) ;
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
details_view . hidden ( true ) ;
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
recent_entries_view . set_parent_rect ( { 0 , 0 , 240 , 280 } ) ;
details_view . set_parent_rect ( { 0 , 0 , 240 , 280 } ) ;
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
recent_entries_view . on_select = [ this ] ( const APRSRecentEntry & entry ) {
this - > on_show_detail ( entry ) ;
} ;
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
details_view . on_close = [ this ] ( ) {
this - > on_show_list ( ) ;
} ;
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
/* for(size_t i = 0; i <32 ; i++){
std : : string id = " test " + i ;
auto & entry = : : on_packet ( recent , i ) ;
entry . set_source_formatted ( id ) ;
}
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
recent_entries_view . set_dirty ( ) ; */
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
/*
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
std : : string str1 = " test1 " ;
std : : string str12 = " test2 " ;
std : : string str13 = " test2 " ;
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
auto & entry = : : on_packet ( recent , 0x1 ) ;
entry . set_source_formatted ( str1 ) ;
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
auto & entry2 = : : on_packet ( recent , 0x2 ) ;
entry2 . set_source_formatted ( str12 ) ;
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
auto & entry3 = : : on_packet ( recent , 0x2 ) ;
entry2 . set_source_formatted ( str13 ) ;
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
recent_entries_view . set_dirty ( ) ;
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
*/
2021-03-07 17:05:23 -05:00
}
2023-05-18 16:16:05 -04:00
void APRSTableView : : on_pkt ( const APRSPacketMessage * message ) {
aprs : : APRSPacket packet = message - > packet ;
rtc : : RTC datetime ;
std : : string str_timestamp ;
std : : string source_formatted = packet . get_source_formatted ( ) ;
std : : string info_string = packet . get_stream_text ( ) ;
2024-01-21 13:47:28 -05:00
rtc_time : : now ( datetime ) ;
2023-05-18 16:16:05 -04:00
auto & entry = : : on_packet ( recent , packet . get_source ( ) ) ;
entry . reset_age ( ) ;
entry . inc_hit ( ) ;
str_timestamp = to_string_datetime ( datetime , HMS ) ;
entry . set_time_string ( str_timestamp ) ;
entry . set_info_string ( info_string ) ;
entry . set_source_formatted ( source_formatted ) ;
if ( entry . has_position & & ! packet . has_position ( ) ) {
// maintain position info
} else {
entry . set_has_position ( packet . has_position ( ) ) ;
entry . set_pos ( packet . get_position ( ) ) ;
}
if ( entry . key ( ) = = details_view . entry ( ) . key ( ) ) {
details_view . set_entry ( entry ) ;
details_view . update ( ) ;
}
recent_entries_view . set_dirty ( ) ;
2021-03-07 17:05:23 -05:00
}
2023-05-18 16:16:05 -04:00
void APRSTableView : : on_show ( ) {
on_show_list ( ) ;
2021-03-07 17:05:23 -05:00
}
2023-05-18 16:16:05 -04:00
void APRSTableView : : on_hide ( ) {
details_view . hidden ( true ) ;
2021-03-07 17:05:23 -05:00
}
2023-05-18 16:16:05 -04:00
void APRSTableView : : focus ( ) {
recent_entries_view . focus ( ) ;
}
2021-03-07 17:05:23 -05:00
2023-05-18 16:16:05 -04:00
APRSTableView : : ~ APRSTableView ( ) {
2021-03-07 17:05:23 -05:00
}
void APRSDetailsView : : focus ( ) {
2023-05-18 16:16:05 -04:00
button_done . focus ( ) ;
2021-03-07 17:05:23 -05:00
}
2023-05-18 16:16:05 -04:00
void APRSDetailsView : : set_entry ( const APRSRecentEntry & entry ) {
entry_copy = entry ;
2021-03-07 17:05:23 -05:00
}
2023-05-18 16:16:05 -04:00
void APRSDetailsView : : update ( ) {
if ( ! hidden ( ) ) {
// uint32_t age = entry_copy.age;
console . clear ( true ) ;
console . write ( entry_copy . info_string ) ;
button_see_map . hidden ( ! entry_copy . has_position ) ;
}
if ( send_updates )
2024-01-05 07:44:30 -05:00
geomap_view - > update_position ( entry_copy . pos . latitude , entry_copy . pos . longitude , 0 , 0 , 0 ) ;
2021-03-07 17:05:23 -05:00
}
APRSDetailsView : : ~ APRSDetailsView ( ) {
}
APRSDetailsView : : APRSDetailsView (
2023-05-18 16:16:05 -04:00
NavigationView & nav ) {
add_children ( { & console ,
& button_done ,
& button_see_map } ) ;
button_done . on_select = [ this , & nav ] ( Button & ) {
console . clear ( true ) ;
this - > on_close ( ) ;
} ;
button_see_map . on_select = [ this , & nav ] ( Button & ) {
geomap_view = nav . push < GeoMapView > (
entry_copy . source_formatted ,
0 , // entry_copy.pos.altitude,
GeoPos : : alt_unit : : FEET ,
2024-01-05 07:44:30 -05:00
GeoPos : : spd_unit : : HIDDEN ,
2023-05-18 16:16:05 -04:00
entry_copy . pos . latitude ,
entry_copy . pos . longitude ,
0 , /*entry_copy.velo.heading,*/
[ this ] ( ) {
send_updates = false ;
hidden ( false ) ;
update ( ) ;
} ) ;
send_updates = true ;
hidden ( true ) ;
} ;
2021-03-07 17:05:23 -05:00
} ;
2023-05-18 16:16:05 -04:00
APRSRXView : : APRSRXView ( NavigationView & nav )
: nav_ { nav } {
add_children ( { & tab_view ,
& view_stream ,
& view_table } ) ;
2021-03-07 17:05:23 -05:00
}
2023-05-18 16:16:05 -04:00
void APRSRXView : : focus ( ) {
tab_view . focus ( ) ;
2021-03-07 17:05:23 -05:00
}
APRSRXView : : ~ APRSRXView ( ) {
}
} /* namespace ui */