From fff63e056a1dd39e97ac753cc4b39d64fe5d34d9 Mon Sep 17 00:00:00 2001 From: Totoo Date: Tue, 17 Dec 2024 18:53:26 +0100 Subject: [PATCH] FM Radio ext app (#2428) --- firmware/application/external/external.cmake | 6 + firmware/application/external/external.ld | 8 + .../application/external/fmradio/main.cpp | 83 +++++++++ .../external/fmradio/ui_fmradio.cpp | 168 ++++++++++++++++++ .../external/fmradio/ui_fmradio.hpp | 150 ++++++++++++++++ 5 files changed, 415 insertions(+) create mode 100644 firmware/application/external/fmradio/main.cpp create mode 100644 firmware/application/external/fmradio/ui_fmradio.cpp create mode 100644 firmware/application/external/fmradio/ui_fmradio.hpp diff --git a/firmware/application/external/external.cmake b/firmware/application/external/external.cmake index a00626b2..908dfc45 100644 --- a/firmware/application/external/external.cmake +++ b/firmware/application/external/external.cmake @@ -134,6 +134,11 @@ set(EXTCPPSRC #mcu_temperature external/mcu_temperature/main.cpp external/mcu_temperature/mcu_temperature.cpp + + + #fmradio + external/fmradio/main.cpp + external/fmradio/ui_fmradio.cpp ) set(EXTAPPLIST @@ -169,4 +174,5 @@ set(EXTAPPLIST flippertx remote mcu_temperature + fmradio ) diff --git a/firmware/application/external/external.ld b/firmware/application/external/external.ld index 560dfb40..9177dc07 100644 --- a/firmware/application/external/external.ld +++ b/firmware/application/external/external.ld @@ -55,6 +55,7 @@ MEMORY ram_external_app_ook_editor(rwx) : org = 0xADCE0000, len = 32k ram_external_app_remote(rwx) : org = 0xADCF0000, len = 32k ram_external_app_mcu_temperature(rwx) : org = 0xADD00000, len = 32k + ram_external_app_fmradio(rwx) : org = 0xADE00000, len = 32k } SECTIONS @@ -251,4 +252,11 @@ SECTIONS KEEP(*(.external_app.app_mcu_temperature.application_information)); *(*ui*external_app*mcu_temperature*); } > ram_external_app_mcu_temperature + + + .external_app_fmradio : ALIGN(4) SUBALIGN(4) + { + KEEP(*(.external_app.app_fmradio.application_information)); + *(*ui*external_app*fmradio*); + } > ram_external_app_fmradio } diff --git a/firmware/application/external/fmradio/main.cpp b/firmware/application/external/fmradio/main.cpp new file mode 100644 index 00000000..040fbe8c --- /dev/null +++ b/firmware/application/external/fmradio/main.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2024 HTotoo + * + * 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.hpp" +#include "ui_fmradio.hpp" +#include "ui_navigation.hpp" +#include "external_app.hpp" + +namespace ui::external_app::fmradio { +void initialize_app(ui::NavigationView& nav) { + nav.push(); +} +} // namespace ui::external_app::fmradio + +extern "C" { + +__attribute__((section(".external_app.app_fmradio.application_information"), used)) application_information_t _application_information_fmradio = { + /*.memory_location = */ (uint8_t*)0x00000000, + /*.externalAppEntry = */ ui::external_app::fmradio::initialize_app, + /*.header_version = */ CURRENT_HEADER_VERSION, + /*.app_version = */ VERSION_MD5, + + /*.app_name = */ "FM Radio", + /*.bitmap_data = */ { + 0x00, + 0x00, + 0x00, + 0x00, + 0x04, + 0x20, + 0x12, + 0x48, + 0x8A, + 0x51, + 0xCA, + 0x53, + 0xCA, + 0x53, + 0x8A, + 0x51, + 0x12, + 0x48, + 0x84, + 0x21, + 0xC0, + 0x03, + 0x40, + 0x02, + 0x60, + 0x06, + 0x20, + 0x04, + 0x30, + 0x0C, + 0xF0, + 0x0F, + }, + /*.icon_color = */ ui::Color::green().v, + /*.menu_location = */ app_location_t::RX, + /*.desired_menu_position = */ -1, + + /*.m4_app_tag = portapack::spi_flash::image_tag_wfm_audio */ {'P', 'W', 'F', 'M'}, + /*.m4_app_offset = */ 0x00000000, // will be filled at compile time +}; +} diff --git a/firmware/application/external/fmradio/ui_fmradio.cpp b/firmware/application/external/fmradio/ui_fmradio.cpp new file mode 100644 index 00000000..ece1d211 --- /dev/null +++ b/firmware/application/external/fmradio/ui_fmradio.cpp @@ -0,0 +1,168 @@ +/* + * Copyright (C) 2024 HTotoo + * + * 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_fmradio.hpp" + +#include "audio.hpp" +#include "rtc_time.hpp" +#include "baseband_api.hpp" +#include "string_format.hpp" +#include "portapack_persistent_memory.hpp" + +using namespace portapack; +using namespace modems; +using namespace ui; + +namespace ui::external_app::fmradio { + +void FmRadioView::focus() { + field_frequency.focus(); +} + +FmRadioView::FmRadioView(NavigationView& nav) + : nav_{nav} { + baseband::run_image(portapack::spi_flash::image_tag_wfm_audio); + + add_children({&rssi, + &field_rf_amp, + &field_lna, + &field_vga, + &field_volume, + &field_frequency, + &btn_fav_save, + &txt_save_help, + &btn_fav_0, + &btn_fav_1, + &btn_fav_2, + &btn_fav_3, + &btn_fav_4, + &btn_fav_5, + &btn_fav_6, + &btn_fav_7, + &btn_fav_8, + &btn_fav_9, + &audio, + &waveform}); + + txt_save_help.visible(false); + for (uint8_t i = 0; i < 12; ++i) { + if (freq_fav_list[i] == 0) { + freq_fav_list[i] = 87000000; + } + } + + if (field_frequency.value() == 0) { + field_frequency.set_value(87000000); + } + + receiver_model.set_modulation(ReceiverModel::Mode::WidebandFMAudio); + + field_frequency.set_step(25000); + receiver_model.enable(); + audio::output::start(); + + btn_fav_0.on_select = [this](Button&) { + on_btn_clicked(0); + }; + btn_fav_1.on_select = [this](Button&) { + on_btn_clicked(1); + }; + btn_fav_2.on_select = [this](Button&) { + on_btn_clicked(2); + }; + btn_fav_3.on_select = [this](Button&) { + on_btn_clicked(3); + }; + btn_fav_4.on_select = [this](Button&) { + on_btn_clicked(4); + }; + btn_fav_5.on_select = [this](Button&) { + on_btn_clicked(5); + }; + btn_fav_6.on_select = [this](Button&) { + on_btn_clicked(6); + }; + btn_fav_7.on_select = [this](Button&) { + on_btn_clicked(7); + }; + btn_fav_8.on_select = [this](Button&) { + on_btn_clicked(8); + }; + btn_fav_9.on_select = [this](Button&) { + on_btn_clicked(9); + }; + + btn_fav_save.on_select = [this](Button&) { + save_fav = !save_fav; + txt_save_help.set_text(save_fav ? "Select slot" : ""); + txt_save_help.visible(save_fav); + txt_save_help.set_dirty(); + }; + + update_fav_btn_texts(); +} + +void FmRadioView::on_btn_clicked(uint8_t i) { + if (save_fav) { + save_fav = false; + freq_fav_list[i] = field_frequency.value(); + update_fav_btn_texts(); + txt_save_help.visible(save_fav); + txt_save_help.set_text(""); + txt_save_help.set_dirty(); + return; + } + field_frequency.set_value(freq_fav_list[i]); +} + +std::string FmRadioView::to_nice_freq(rf::Frequency freq) { + std::string nice = to_string_dec_uint(freq / 1000000); + nice += "."; + nice += to_string_dec_uint((freq / 10000) % 100); + return nice; +} + +void FmRadioView::update_fav_btn_texts() { + btn_fav_0.set_text(to_nice_freq(freq_fav_list[0])); + btn_fav_1.set_text(to_nice_freq(freq_fav_list[1])); + btn_fav_2.set_text(to_nice_freq(freq_fav_list[2])); + btn_fav_3.set_text(to_nice_freq(freq_fav_list[3])); + btn_fav_4.set_text(to_nice_freq(freq_fav_list[4])); + btn_fav_5.set_text(to_nice_freq(freq_fav_list[5])); + btn_fav_6.set_text(to_nice_freq(freq_fav_list[6])); + btn_fav_7.set_text(to_nice_freq(freq_fav_list[7])); + btn_fav_8.set_text(to_nice_freq(freq_fav_list[8])); + btn_fav_9.set_text(to_nice_freq(freq_fav_list[9])); +} + +FmRadioView::~FmRadioView() { + receiver_model.disable(); + baseband::shutdown(); + audio::output::stop(); +} + +void FmRadioView::on_audio_spectrum() { + for (size_t i = 0; i < audio_spectrum_data->db.size(); i++) + audio_spectrum[i] = ((int16_t)audio_spectrum_data->db[i] - 127) * 256; + waveform.set_dirty(); +} + +} // namespace ui::external_app::fmradio diff --git a/firmware/application/external/fmradio/ui_fmradio.hpp b/firmware/application/external/fmradio/ui_fmradio.hpp new file mode 100644 index 00000000..74fc65ea --- /dev/null +++ b/firmware/application/external/fmradio/ui_fmradio.hpp @@ -0,0 +1,150 @@ +/* + * Copyright (C) 2024 HTotoo + * + * 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. + */ + +/* + FUTURE TODO: implement search function. +*/ + +#ifndef __UI_fmradio_H__ +#define __UI_fmradio_H__ + +#include "ui.hpp" +#include "ui_language.hpp" +#include "ui_navigation.hpp" +#include "ui_receiver.hpp" +#include "ui_geomap.hpp" +#include "ui_freq_field.hpp" +#include "ui_spectrum.hpp" +#include "ui_record_view.hpp" +#include "app_settings.hpp" +#include "radio_state.hpp" +#include "log_file.hpp" +#include "utility.hpp" + +using namespace ui; + +namespace ui::external_app::fmradio { + +#define FMR_BTNGRID_TOP 60 + +class FmRadioView : public View { + public: + FmRadioView(NavigationView& nav); + FmRadioView& operator=(const FmRadioView&) = delete; + FmRadioView(const FmRadioView&) = delete; + ~FmRadioView(); + + void focus() override; + + std::string title() const override { return "FM radio"; }; + + private: + NavigationView& nav_; + RxRadioState radio_state_{}; + int16_t audio_spectrum[128]{0}; + bool audio_spectrum_update = false; + AudioSpectrum* audio_spectrum_data{nullptr}; + rf::Frequency freq_fav_list[12] = {0}; + + app_settings::SettingsManager settings_{ + "rx_fmradio", + app_settings::Mode::RX, + {{"favlist0"sv, &freq_fav_list[0]}, + {"favlist1"sv, &freq_fav_list[1]}, + {"favlist2"sv, &freq_fav_list[2]}, + {"favlist3"sv, &freq_fav_list[3]}, + {"favlist4"sv, &freq_fav_list[4]}, + {"favlist5"sv, &freq_fav_list[5]}, + {"favlist6"sv, &freq_fav_list[6]}, + {"favlist7"sv, &freq_fav_list[7]}, + {"favlist8"sv, &freq_fav_list[8]}, + {"favlist9"sv, &freq_fav_list[9]}, + {"favlist10"sv, &freq_fav_list[10]}, + {"favlist11"sv, &freq_fav_list[11]}}}; + + RFAmpField field_rf_amp{ + {13 * 8, 0 * 16}}; + LNAGainField field_lna{ + {15 * 8, 0 * 16}}; + VGAGainField field_vga{ + {18 * 8, 0 * 16}}; + RSSI rssi{ + {21 * 8, 0, 6 * 8, 4}}; + AudioVolumeField field_volume{ + {28 * 8, 0 * 16}}; + + RxFrequencyField field_frequency{ + {0 * 8, 0 * 16}, + nav_}; + + TextField txt_save_help{ + {2, FMR_BTNGRID_TOP + 6 * 34 - 20, 12 * 8, 16}, + " "}; + + Audio audio{ + {21 * 8, 10, 6 * 8, 4}}; + + Waveform waveform{ + {0, 20, 30 * 8, 2 * 16}, + audio_spectrum, + 128, + 0, + false, + Theme::getInstance()->bg_darkest->foreground}; + + Button btn_fav_0{{2, FMR_BTNGRID_TOP + 0 * 34, 10 * 8, 28}, "---"}; + Button btn_fav_1{{2 + 15 * 8, FMR_BTNGRID_TOP + 0 * 34, 10 * 8, 28}, "---"}; + Button btn_fav_2{{2, FMR_BTNGRID_TOP + 1 * 34, 10 * 8, 28}, "---"}; + Button btn_fav_3{{2 + 15 * 8, FMR_BTNGRID_TOP + 1 * 34, 10 * 8, 28}, "---"}; + Button btn_fav_4{{2, FMR_BTNGRID_TOP + 2 * 34, 10 * 8, 28}, "---"}; + Button btn_fav_5{{2 + 15 * 8, FMR_BTNGRID_TOP + 2 * 34, 10 * 8, 28}, "---"}; + Button btn_fav_6{{2, FMR_BTNGRID_TOP + 3 * 34, 10 * 8, 28}, "---"}; + Button btn_fav_7{{2 + 15 * 8, FMR_BTNGRID_TOP + 3 * 34, 10 * 8, 28}, "---"}; + Button btn_fav_8{{2, FMR_BTNGRID_TOP + 4 * 34, 10 * 8, 28}, "---"}; + Button btn_fav_9{{2 + 15 * 8, FMR_BTNGRID_TOP + 4 * 34, 10 * 8, 28}, "---"}; + + Button btn_fav_save{{2, FMR_BTNGRID_TOP + 6 * 34, 7 * 8, 1 * 28}, "Save"}; + bool save_fav = false; + void on_btn_clicked(uint8_t i); + void update_fav_btn_texts(); + std::string to_nice_freq(rf::Frequency freq); + void on_audio_spectrum(); + + MessageHandlerRegistration message_handler_audio_spectrum{ + Message::ID::AudioSpectrum, + [this](const Message* const p) { + const auto message = *reinterpret_cast(p); + audio_spectrum_data = message.data; + audio_spectrum_update = true; + }}; + MessageHandlerRegistration message_handler_frame_sync{ + Message::ID::DisplayFrameSync, + [this](const Message* const) { + if (audio_spectrum_update) { + audio_spectrum_update = false; + on_audio_spectrum(); + } + }}; +}; + +} // namespace ui::external_app::fmradio + +#endif /*__UI_fmradio_H__*/