From fff63e056a1dd39e97ac753cc4b39d64fe5d34d9 Mon Sep 17 00:00:00 2001 From: Totoo Date: Tue, 17 Dec 2024 18:53:26 +0100 Subject: [PATCH 1/3] 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 a00626b20..908dfc45d 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 560dfb407..9177dc07a 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 000000000..040fbe8c5 --- /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 000000000..ece1d2119 --- /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 000000000..74fc65eaa --- /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__*/ From ead9449609ec1c5372c0dfe015a4604312f61523 Mon Sep 17 00:00:00 2001 From: sommermorgentraum <24917424+zxkmm@users.noreply.github.com> Date: Wed, 18 Dec 2024 17:06:25 +0800 Subject: [PATCH 2/3] add tuner app (#2429) * _ * icon --- firmware/application/external/external.cmake | 8 +- firmware/application/external/external.ld | 11 +- firmware/application/external/tuner/main.cpp | 83 ++++++ .../application/external/tuner/ui_tuner.cpp | 256 ++++++++++++++++++ .../application/external/tuner/ui_tuner.hpp | 145 ++++++++++ firmware/graphics/icon_tune_fork.png | Bin 0 -> 886 bytes 6 files changed, 499 insertions(+), 4 deletions(-) create mode 100644 firmware/application/external/tuner/main.cpp create mode 100644 firmware/application/external/tuner/ui_tuner.cpp create mode 100644 firmware/application/external/tuner/ui_tuner.hpp create mode 100644 firmware/graphics/icon_tune_fork.png diff --git a/firmware/application/external/external.cmake b/firmware/application/external/external.cmake index 908dfc45d..ea51c060c 100644 --- a/firmware/application/external/external.cmake +++ b/firmware/application/external/external.cmake @@ -134,11 +134,14 @@ 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 + + #tuner + external/tuner/main.cpp + external/tuner/ui_tuner.cpp ) set(EXTAPPLIST @@ -172,7 +175,8 @@ set(EXTAPPLIST ook_editor shoppingcart_lock flippertx - remote + remote mcu_temperature fmradio + tuner ) diff --git a/firmware/application/external/external.ld b/firmware/application/external/external.ld index 9177dc07a..28a568ace 100644 --- a/firmware/application/external/external.ld +++ b/firmware/application/external/external.ld @@ -55,7 +55,9 @@ 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 + ram_external_app_fmradio(rwx) : org = 0xADD10000, len = 32k + ram_external_app_tuner(rwx) : org = 0xADD20000, len = 32k + } SECTIONS @@ -252,11 +254,16 @@ 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 + + .external_app_tuner : ALIGN(4) SUBALIGN(4) + { + KEEP(*(.external_app.app_tuner.application_information)); + *(*ui*external_app*tuner*); + } > ram_external_app_tuner } diff --git a/firmware/application/external/tuner/main.cpp b/firmware/application/external/tuner/main.cpp new file mode 100644 index 000000000..4df2414ea --- /dev/null +++ b/firmware/application/external/tuner/main.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2024 Bernd + * + * 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_tuner.hpp" +#include "ui_navigation.hpp" +#include "external_app.hpp" + +namespace ui::external_app::tuner { +void initialize_app(ui::NavigationView& nav) { + nav.push(); +} +} // namespace ui::external_app::tuner + +extern "C" { + +__attribute__((section(".external_app.app_tuner.application_information"), used)) application_information_t _application_information_tuner = { + /*.memory_location = */ (uint8_t*)0x00000000, + /*.externalAppEntry = */ ui::external_app::tuner::initialize_app, + /*.header_version = */ CURRENT_HEADER_VERSION, + /*.app_version = */ VERSION_MD5, + + /*.app_name = */ "Tuner", + /*.bitmap_data = */ { + 0x00, + 0x00, + 0x00, + 0x00, + 0x22, + 0x44, + 0x21, + 0x84, + 0x2D, + 0xB4, + 0x25, + 0xA4, + 0x25, + 0xA4, + 0x2D, + 0xB4, + 0x61, + 0x86, + 0xC2, + 0x43, + 0x80, + 0x01, + 0x80, + 0x01, + 0x80, + 0x01, + 0x80, + 0x01, + 0x00, + 0x00, + 0x00, + 0x00, + }, + /*.icon_color = */ ui::Color::cyan().v, + /*.menu_location = */ app_location_t::UTILITIES, + /*.desired_menu_position = */ -1, + + /*.m4_app_tag = portapack::spi_flash::image_tag_none */ {'P', 'A', 'B', 'P'}, + /*.m4_app_offset = */ 0x00000000, // will be filled at compile time +}; +} diff --git a/firmware/application/external/tuner/ui_tuner.cpp b/firmware/application/external/tuner/ui_tuner.cpp new file mode 100644 index 000000000..5aa9e1c03 --- /dev/null +++ b/firmware/application/external/tuner/ui_tuner.cpp @@ -0,0 +1,256 @@ +/* + * copyleft 2024 sommermorgentraum + * + * 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_tuner.hpp" +#include "baseband_api.hpp" +#include "audio.hpp" +#include "portapack.hpp" + +using namespace portapack; + +namespace ui::external_app::tuner { + +TunerView::TunerView(NavigationView& nav) + : nav_{nav} { + baseband::run_prepared_image(portapack::memory::map::m4_code.base()); // proc_audio_beep baseband is external too + + add_children({ + &labels, + &field_volume, + &options_instrument, + &options_note, + &button_play_stop, + &text_note_frequency, + &text_note_octave_shift, + }); + + audio::set_rate(audio::Rate::Hz_24000); + + options_instrument.on_change = [this](size_t, int32_t value) { + const Instrument* selected_instrument = nullptr; + + switch (value) { + case 0: + selected_instrument = &guitar; + break; + case 1: + selected_instrument = &violin; + break; + case 2: + selected_instrument = &pitch_fork; + break; + } + + if (selected_instrument) { + update_notes_for_instrument(*selected_instrument); + } + + update_current_note(); + }; + + options_note.on_change = [this](size_t, int32_t index) { + const Instrument* current_instrument = nullptr; + switch (options_instrument.selected_index_value()) { + case 0: + current_instrument = &guitar; + break; + case 1: + current_instrument = &violin; + break; + case 2: + current_instrument = &pitch_fork; + break; + } + + if (current_instrument) { + auto it = current_instrument->notes.begin(); + std::advance(it, index); + if (it != current_instrument->notes.end()) { + current_note_frequency = it->second.frequency; + current_note_sample_rate = it->second.sample_rate; + current_note_octave_shift = it->second.octave_shift; + } + } + + update_current_note(); + }; + + button_play_stop.on_select = [this]() { + if (current_note_playing) { + stop_play(); + } else { + play_change_note(); + } + }; + + options_instrument.set_selected_index(0); + update_notes_for_instrument(guitar); + update_current_note(); + + field_volume.set_value(0); // seems that a change is required to force update, so setting to 0 first + field_volume.set_value(99); + + audio::set_rate(audio::Rate::Hz_24000); + audio::output::start(); +} + +TunerView::~TunerView() { + receiver_model.disable(); + baseband::shutdown(); + audio::output::stop(); +} + +void TunerView::focus() { + options_instrument.focus(); +} + +void TunerView::update_notes_for_instrument(const Instrument& instrument) { + std::vector note_options; + size_t index = 0; + + for (const auto& note_pair : instrument.notes) { + note_options.emplace_back(OptionsField::option_t{ + note_pair.first, + index++}); + } + + options_note.set_options(note_options); + if (!note_options.empty()) { + options_note.set_selected_index(0); + } +} + +void TunerView::update_current_note() { + const Instrument* current_instrument = nullptr; + + switch (options_instrument.selected_index_value()) { + case 0: + current_instrument = &guitar; + break; + case 1: + current_instrument = &violin; + break; + case 2: + current_instrument = &pitch_fork; + break; + } + + if (current_instrument) { + std::string note_name = options_note.selected_index_name(); + + // map + auto note_it = current_instrument->notes.find(note_name); + if (note_it != current_instrument->notes.end()) { + current_note_frequency = note_it->second.frequency; + current_note_sample_rate = note_it->second.sample_rate; + current_note_octave_shift = note_it->second.octave_shift; + + text_note_frequency.set(std::to_string(current_note_frequency)); + text_note_octave_shift.set(std::to_string(current_note_octave_shift)); + + set_dirty(); + + if (current_note_playing) { + play_change_note(); + } + } + } +} + +void TunerView::stop_play() { + baseband::request_beep_stop(); + current_note_playing = false; + button_play_stop.set_bitmap(&bitmap_icon_replay); +} + +void TunerView::play_change_note() { + if (current_note_playing) { + baseband::request_beep_stop(); + audio::set_rate(current_note_sample_rate); + baseband::request_audio_beep(current_note_frequency, protected_sample_rate(current_note_sample_rate), 0); + } else { + audio::set_rate(current_note_sample_rate); + baseband::request_audio_beep(current_note_frequency, protected_sample_rate(current_note_sample_rate), 0); + } + current_note_playing = true; + button_play_stop.set_bitmap(&bitmap_icon_sleep); + set_dirty(); +} + +uint32_t TunerView::protected_sample_rate(audio::Rate r) { + switch (r) { + case audio::Rate::Hz_12000: + return 12000; + case audio::Rate::Hz_24000: + return 24000; + case audio::Rate::Hz_48000: + return 48000; + default: + return 24000; + } +} + +void TunerView::paint(Painter& painter) { + View::paint(painter); + + painter.fill_rectangle( + {screen_width / 4, 8 * 16, screen_width / 2, 6 * 16}, + Theme::getInstance()->bg_darkest->background); + + if (!current_note_playing) return; + + painter.fill_rectangle( + {screen_width / 4, 10 * 16, 2, 2 * 16}, + Theme::getInstance()->fg_light->foreground); + + painter.fill_rectangle( + {screen_width / 4, 12 * 16, screen_width / 4 * 2 + 2, 2}, + Theme::getInstance()->fg_light->foreground); + + painter.fill_rectangle( + {(screen_width / 4) * 3, 10 * 16, 2, 2 * 16}, + Theme::getInstance()->fg_light->foreground); + + painter.draw_string( + {screen_width / 4 - 2 * 8, 8 * 16}, + (current_note_octave_shift == 0) ? *Theme::getInstance()->bg_darkest : *Theme::getInstance()->fg_red, + std::to_string(current_note_frequency)); + + int16_t real_frequency = current_note_frequency; + if (current_note_octave_shift > 0) { + for (int i = 0; i < current_note_octave_shift; i++) { + real_frequency *= 2; + } + } else if (current_note_octave_shift < 0) { + for (int i = 0; i > current_note_octave_shift; i--) { + real_frequency /= 2; + } + } + painter.draw_string({(screen_width / 4) * 3 - 2 * 8, 8 * 16}, + (current_note_octave_shift == 0) ? *Theme::getInstance()->bg_darkest : *Theme::getInstance()->fg_red, + std::to_string(real_frequency)); + + painter.draw_string({screen_width / 2 - 3 * 16, 13 * 16}, + (current_note_octave_shift == 0) ? *Theme::getInstance()->bg_darkest : *Theme::getInstance()->fg_red, + std::to_string(current_note_octave_shift) + " * 8ev"); +} + +} // namespace ui::external_app::tuner diff --git a/firmware/application/external/tuner/ui_tuner.hpp b/firmware/application/external/tuner/ui_tuner.hpp new file mode 100644 index 000000000..7b76d0404 --- /dev/null +++ b/firmware/application/external/tuner/ui_tuner.hpp @@ -0,0 +1,145 @@ +/* + * copyleft 2024 sommermorgentraum + * + * 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. + */ + +#ifndef __UI_TUNER_H__ +#define __UI_TUNER_H__ + +#include "ui_navigation.hpp" +#include "ui_receiver.hpp" +#include "audio.hpp" + +namespace ui::external_app::tuner { + +struct Instrument { + std::string name; + struct NoteInfo { + uint16_t frequency; + audio::Rate sample_rate; // tune samplerate to allow for freqs + int8_t octave_shift; + // PP hardware can't handle some extremely low/high frequencies, this indicates how much the freq that were played is shifted up/down, + // for example, if shift is -2 and note is A3, it means the real string should play A5 but PP's speaker plays A3 + }; + std::map notes; // this is for fast looking : O(log(n)) , usage `auto note = guitar.notes["A4"];` +}; + +class TunerView : public View { + public: + TunerView(NavigationView& nav); + ~TunerView(); + TunerView(const TunerView& other) = delete; + TunerView& operator=(const TunerView& other) = delete; + + void focus() override; + void update_audio_beep(); + + std::string title() const override { return "Tuner"; }; + + private: + NavigationView& nav_; + bool beep{false}; + + void update_notes_for_instrument(const Instrument& instrument); + void play_change_note(); + void stop_play(); + void update_current_note(); + uint32_t protected_sample_rate(audio::Rate r); + void paint(Painter& painter) override; + + uint16_t current_note_frequency{440}; + audio::Rate current_note_sample_rate{audio::Rate::Hz_12000}; + int8_t current_note_octave_shift{0}; + bool current_note_playing{false}; + + Labels labels{ + {{0 * 8, 1 * 16}, "Instrument:", Theme::getInstance()->fg_light->foreground}, + {{0 * 8, 2 * 16}, "Note:", Theme::getInstance()->fg_light->foreground}, + {{0 * 8, 3 * 16}, "Note Frequency:", Theme::getInstance()->fg_light->foreground}, + {{0 * 8, 4 * 16}, "Note Octave Shift:", Theme::getInstance()->fg_light->foreground}, + {{0 * 8, 5 * 16}, "Volume:", Theme::getInstance()->fg_light->foreground}}; + + Text text_note_frequency{ + {(sizeof("Note Frequency:") + 1) * 8, 3 * 16, screen_width - (sizeof("Note Frequency:") + 1) * 8, 16}, + "", + }; + + Text text_note_octave_shift{ + {(sizeof("Note Octave Shift:") + 1) * 8, 4 * 16, screen_width - (sizeof("Note Octave Shift:") + 1) * 8, 16}, + "", + }; + + AudioVolumeField field_volume{ + {(sizeof("Volume:") + 1) * 8, 5 * 16}}; + + // TODO: load list runtime + OptionsField options_instrument{ + {(sizeof("Instrument:") + 1) * 8, 1 * 16}, + screen_width - (sizeof("Instrument:") + 1) * 8, + {{"Guitar", 0}, + {"Violin", 1}, + {"Pitch Fork", 2}}}; + + OptionsField options_note{ + {(sizeof("Note:") + 1) * 8, 2 * 16}, + screen_width - (sizeof("Note:") + 1) * 8, + {}}; + + NewButton button_play_stop{ + {0 * 8, 16 * 16, screen_width, screen_height - 16 * 16}, + {}, + &bitmap_icon_replay, + Theme::getInstance()->fg_red->foreground}; + + // TODO: load from file DB + const Instrument guitar = { + .name = "Folk Guitar", + .notes = { + {"E2", {165, audio::Rate::Hz_12000, -1}}, // actual: E2, PP speaker: E3 + {"A2", {110, audio::Rate::Hz_12000, 0}}, + {"D3", {147, audio::Rate::Hz_12000, 0}}, + {"G3", {196, audio::Rate::Hz_24000, 0}}, + {"B3", {247, audio::Rate::Hz_24000, 0}}, + {"E4", {330, audio::Rate::Hz_24000, 0}}}}; + + const Instrument violin = { + .name = "Violin 440 Standard, 12ET", + .notes = { + {"G3", {196, audio::Rate::Hz_12000, 0}}, + {"D4", {294, audio::Rate::Hz_24000, 0}}, + {"A4", {440, audio::Rate::Hz_48000, 0}}, + {"E5", {659, audio::Rate::Hz_48000, 0}}}}; + + const Instrument pitch_fork = {// freq copied from flipper app + .name = "Pitch Fork", + .notes = { + {"12ET A4", {440, audio::Rate::Hz_48000, 0}}, + {"Sarti's A4", {436, audio::Rate::Hz_48000, 0}}, + {"1858 A4", {435, audio::Rate::Hz_48000, 0}}, + {"Verdi's A4", {432, audio::Rate::Hz_48000, 0}}}}; + + std::map instruments{ + {"Guitar", guitar}, + {"Violin", violin}, + {"Pitch Fork", pitch_fork}}; +}; + +} // namespace ui::external_app::tuner + +#endif /*__UI_TUNER_H__*/ diff --git a/firmware/graphics/icon_tune_fork.png b/firmware/graphics/icon_tune_fork.png new file mode 100644 index 0000000000000000000000000000000000000000..a542e12852f6bacd5c8e93d86e6af8610685161d GIT binary patch literal 886 zcmV-+1Bv{JP)EX>4Tx04R}tkv&MmKpe$iQ>7x+B6bjQ2vVKwq9Ts93Pq?8YK2xEOfLO`CJjl7 zi=*ILaPVWX>fqw6tAnc`2!4P#J2)x2NQwVT3N2ziIPS;0dyl(!fKV?p&FYE)nr@q^ zL|n{dSH-|9f*3$QMiG*kWz0!Z5*^3aJ$!t(i|qi@OrgSSBMn%7%%AEysMnz~Bf00)P_ zc!9FlJG{H6y|;hQH2eDjgV}PewL1zy00006VoOIv00000008+zyMF)x010qNS#tmY z4c7nw4c7reD4Tcy000McNliru=nN4KCkiVVn8g490dq-2K~y-)rIW#G+dvS8zm<)5 z74Q;hQ;RAI)j>TLax2)kKp!BGxAIHmSq#k{dwAPDy7^SSnx%Vi8)B}sCUrfIybs;Vjg(lm{eBsl@DvMl@V)eozp zD55Y7ueUW2;)!G!hSx<=d;?Yho^$SLa#ChM?+p@qQp&tx)%Jb=76_v#GS*t9wXW(! z4cs&YoO2quvDV&qIvoQX`o90)i{tnbI8sX8X{|FLS4#ae#teY3KzBNwo|a{~2mH}m z-zufue*xrg zbz*}DDW$EaoxjJ*AKrx*<|)f^X9MxdHtqvm;P1AC_A3nYUHSnY0ji0G@aHDmWdHyG M07*qoM6N<$f=L#OI{*Lx literal 0 HcmV?d00001 From 9cea76a9f331a831ebb79d0cca99b3d6d9bcc1ea Mon Sep 17 00:00:00 2001 From: sommermorgentraum <24917424+zxkmm@users.noreply.github.com> Date: Wed, 18 Dec 2024 17:25:02 +0800 Subject: [PATCH 3/3] add metronome app (merge tuner firstly to satisfy conflict) (#2431) * _ * _ --- firmware/application/external/external.cmake | 11 +- firmware/application/external/external.ld | 12 +- .../application/external/metronome/main.cpp | 84 +++++++++ .../external/metronome/ui_metronome.cpp | 177 ++++++++++++++++++ .../external/metronome/ui_metronome.hpp | 126 +++++++++++++ 5 files changed, 404 insertions(+), 6 deletions(-) create mode 100644 firmware/application/external/metronome/main.cpp create mode 100644 firmware/application/external/metronome/ui_metronome.cpp create mode 100644 firmware/application/external/metronome/ui_metronome.hpp diff --git a/firmware/application/external/external.cmake b/firmware/application/external/external.cmake index ea51c060c..03fbffad0 100644 --- a/firmware/application/external/external.cmake +++ b/firmware/application/external/external.cmake @@ -139,9 +139,13 @@ set(EXTCPPSRC external/fmradio/main.cpp external/fmradio/ui_fmradio.cpp - #tuner + #tuner external/tuner/main.cpp external/tuner/ui_tuner.cpp + + #metronome + external/metronome/main.cpp + external/metronome/ui_metronome.cpp ) set(EXTAPPLIST @@ -175,8 +179,9 @@ set(EXTAPPLIST ook_editor shoppingcart_lock flippertx - remote + remote mcu_temperature fmradio - tuner + tuner + metronome ) diff --git a/firmware/application/external/external.ld b/firmware/application/external/external.ld index 28a568ace..51a24ea56 100644 --- a/firmware/application/external/external.ld +++ b/firmware/application/external/external.ld @@ -56,8 +56,8 @@ MEMORY 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 = 0xADD10000, len = 32k - ram_external_app_tuner(rwx) : org = 0xADD20000, len = 32k - + ram_external_app_tuner(rwx) : org = 0xADD20000, len = 32k + ram_external_app_metronome(rwx) : org = 0xADD30000, len = 32k } SECTIONS @@ -261,9 +261,15 @@ SECTIONS *(*ui*external_app*fmradio*); } > ram_external_app_fmradio - .external_app_tuner : ALIGN(4) SUBALIGN(4) + .external_app_tuner : ALIGN(4) SUBALIGN(4) { KEEP(*(.external_app.app_tuner.application_information)); *(*ui*external_app*tuner*); } > ram_external_app_tuner + + .external_app_metronome : ALIGN(4) SUBALIGN(4) + { + KEEP(*(.external_app.app_metronome.application_information)); + *(*ui*external_app*metronome*); + } > ram_external_app_metronome } diff --git a/firmware/application/external/metronome/main.cpp b/firmware/application/external/metronome/main.cpp new file mode 100644 index 000000000..620e8962a --- /dev/null +++ b/firmware/application/external/metronome/main.cpp @@ -0,0 +1,84 @@ +/* + * Copyright (C) 2024 Bernd + * + * 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_metronome.hpp" +#include "ui_navigation.hpp" +#include "external_app.hpp" + +namespace ui::external_app::metronome { +void initialize_app(ui::NavigationView& nav) { + nav.push(); +} +} // namespace ui::external_app::metronome + +extern "C" { + +__attribute__((section(".external_app.app_metronome.application_information"), used)) application_information_t _application_information_metronome = { + /*.memory_location = */ (uint8_t*)0x00000000, + /*.externalAppEntry = */ ui::external_app::metronome::initialize_app, + /*.header_version = */ CURRENT_HEADER_VERSION, + /*.app_version = */ VERSION_MD5, + + /*.app_name = */ "Metronome", + /*.bitmap_data = */ { + 0x00, + 0x00, + 0xC0, + 0x43, + 0x20, + 0x66, + 0xA0, + 0x34, + 0x20, + 0x18, + 0xB0, + 0x0C, + 0x10, + 0x04, + 0x10, + 0x06, + 0x10, + 0x0B, + 0x98, + 0x19, + 0x08, + 0x10, + 0xF8, + 0x1F, + 0xF8, + 0x1F, + 0xF8, + 0x1F, + 0xF0, + 0x0F, + 0x00, + 0x00, + + }, + /*.icon_color = */ ui::Color::cyan().v, + /*.menu_location = */ app_location_t::UTILITIES, + /*.desired_menu_position = */ -1, + + /*.m4_app_tag = portapack::spi_flash::image_tag_none */ {'P', 'A', 'B', 'P'}, + /*.m4_app_offset = */ 0x00000000, // will be filled at compile time +}; +} \ No newline at end of file diff --git a/firmware/application/external/metronome/ui_metronome.cpp b/firmware/application/external/metronome/ui_metronome.cpp new file mode 100644 index 000000000..75dca3b38 --- /dev/null +++ b/firmware/application/external/metronome/ui_metronome.cpp @@ -0,0 +1,177 @@ +/* + * copyleft 2024 sommermorgentraum + * + * 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_metronome.hpp" +#include "baseband_api.hpp" +#include "audio.hpp" +#include "portapack.hpp" + +using namespace portapack; + +namespace ui::external_app::metronome { + +MetronomeView::MetronomeView(NavigationView& nav) + : nav_{nav} { + baseband::run_prepared_image(portapack::memory::map::m4_code.base()); // proc_audio_beep baseband is external too + + add_children({ + &labels, + &field_volume, + &button_play_stop, + &field_rythm_unaccent_time, + &field_rythm_accent_time, + &field_accent_beep_tune, + &field_unaccent_beep_tune, + &field_beep_flash_duration, + &field_bpm, + &progressbar, + }); + + field_bpm.set_value(120); + field_rythm_accent_time.set_value(4); + field_rythm_unaccent_time.set_value(4); + field_accent_beep_tune.set_value(880); + field_unaccent_beep_tune.set_value(440); + field_beep_flash_duration.set_value(100); + button_play_stop.on_select = [this]() { + if (playing_) { + stop_play(); + } else { + play(); + } + }; + + field_volume.set_value(0); // seems that a change is required to force update, so setting to 0 first + field_volume.set_value(99); + + audio::set_rate(audio::Rate::Hz_48000); + + audio::output::start(); +} + +MetronomeView::~MetronomeView() { + should_exit = true; + if (thread) { + chThdWait(thread); + thread = nullptr; + } + receiver_model.disable(); + baseband::shutdown(); + audio::output::stop(); +} + +void MetronomeView::focus() { + field_bpm.focus(); +} + +void MetronomeView::stop_play() { + if (playing_) { + playing_ = false; + button_play_stop.set_bitmap(&bitmap_icon_replay); + baseband::request_beep_stop(); + progressbar.set_value(0); + progressbar.set_style(Theme::getInstance()->fg_light); + } +} + +void MetronomeView::play() { + if (!playing_) { + playing_ = true; + current_beat_ = 0; + button_play_stop.set_bitmap(&bitmap_icon_sleep); + + if (!thread) { + thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, MetronomeView::static_fn, this); + } + } +} + +void MetronomeView::beep_accent_beat() { + baseband::request_audio_beep(field_accent_beep_tune.value(), 48000, field_beep_flash_duration.value()); +} + +void MetronomeView::beep_unaccent_beat() { + baseband::request_audio_beep(field_unaccent_beep_tune.value(), 48000, field_beep_flash_duration.value()); +} + +// TODO: draw the beat +// void MetronomeView::paint(Painter& painter) { +// View::paint(painter); + +// painter.fill_rectangle( +// {visual_x, visual_y, visual_width, visual_height}, +// Theme::getInstance()->bg_darkest->background); + +// if (playing_) { +// const bool is_accent_beat = (current_beat_ % field_rythm_accent_time.value()) == 0; + +// const Color beat_color = is_accent_beat ? +// Theme::getInstance()->fg_red->foreground : +// Theme::getInstance()->fg_green->foreground; + +// painter.fill_rectangle( +// {visual_x + visual_width/4, +// visual_y + visual_height/4, +// visual_width/2, +// visual_height/2}, +// beat_color); +// } +// } + +msg_t MetronomeView::static_fn(void* arg) { + auto obj = static_cast(arg); + obj->run(); + return 0; +} + +void MetronomeView::run() { + while (!should_exit) { + if (!playing_) { + chThdSleepMilliseconds(100); + continue; + } + + uint32_t base_interval = (60 * 1000) / field_bpm.value(); // quarter note as 1 beat + + uint32_t beats_per_measure = field_rythm_unaccent_time.value(); // how many beates per bar + progressbar.set_max(beats_per_measure); + uint32_t beat_unit = field_rythm_accent_time.value(); // which note type (quarter, eighth, etc.) as 1 beat + + uint32_t actual_interval = (base_interval * 4) / beat_unit; // e.g. when beat_unit==8 it's 1/2 of base_interval AKA eighths notes + + uint32_t beat_in_measure = current_beat_ % beats_per_measure; // current beat in this bar (need to decide accent or unaccent) + progressbar.set_value(beat_in_measure + 1); + + // accent beat is the first beat of this bar + if (beat_in_measure == 0) { + beep_accent_beat(); + progressbar.set_style(Theme::getInstance()->fg_red); + } else { + beep_unaccent_beat(); + progressbar.set_style(Theme::getInstance()->fg_green); + } + + current_beat_++; + chThdSleepMilliseconds(actual_interval); + } +} + +} // namespace ui::external_app::metronome \ No newline at end of file diff --git a/firmware/application/external/metronome/ui_metronome.hpp b/firmware/application/external/metronome/ui_metronome.hpp new file mode 100644 index 000000000..d813146da --- /dev/null +++ b/firmware/application/external/metronome/ui_metronome.hpp @@ -0,0 +1,126 @@ +/* + * copyleft 2024 sommermorgentraum + * + * 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. + */ + +#ifndef __UI_METRONOME_H__ +#define __UI_METRONOME_H__ + +#include "ui_navigation.hpp" +#include "ui_receiver.hpp" +#include "audio.hpp" +#include "ch.h" + +namespace ui::external_app::metronome { + +class MetronomeView : public View { + public: + MetronomeView(NavigationView& nav); + ~MetronomeView(); + MetronomeView(const MetronomeView& other) = delete; + MetronomeView& operator=(const MetronomeView& other) = delete; + + void focus() override; + + std::string title() const override { return "Metronome"; }; + + private: + NavigationView& nav_; + + void beep_accent_beat(); // e.g. 3 of 3/4 beat + void beep_unaccent_beat(); // e.g. 4 of 3/4 beat + void stop_play(); + void play(); + // void paint(Painter& painter) override; + + Thread* thread{nullptr}; + bool should_exit{false}; + static msg_t static_fn(void* arg); + void run(); + + bool playing_{false}; + uint32_t current_beat_{0}; + + Labels labels{ + {{0 * 8, 1 * 16}, "BPM:", Theme::getInstance()->fg_light->foreground}, + {{0 * 8, 2 * 16}, "Accent Beep Tune:", Theme::getInstance()->fg_light->foreground}, + {{0 * 8, 3 * 16}, "Unaccent Beep Tune:", Theme::getInstance()->fg_light->foreground}, + {{0 * 8, 4 * 16}, "Rhythm:", Theme::getInstance()->fg_light->foreground}, + {{(sizeof("Rhythm:") + 1) * 8 + 4 * 8, 4 * 16}, "/", Theme::getInstance()->fg_light->foreground}, + {{0 * 8, 5 * 16}, "Beep Flash Duration:", Theme::getInstance()->fg_light->foreground}, + {{0 * 8, 6 * 16}, "Volume:", Theme::getInstance()->fg_light->foreground}}; + + NumberField field_bpm{ + {(sizeof("BPM:") + 1) * 8, 1 * 16}, + 4, + {1, 1000}, + 1, + ' '}; + + NumberField field_rythm_unaccent_time{// e.g. 3 in 3/4 beat + {(sizeof("Rhythm:") + 1) * 8, 4 * 16}, + 2, + {1, 99}, + 1, + ' '}; + + NumberField field_rythm_accent_time{// e.g. 4 in 3/4 beat + {(sizeof("Rhythm:") + 1) * 8 + 5 * 8, 4 * 16}, + 2, + {1, 99}, + 1, + ' '}; + + NumberField field_beep_flash_duration{ + {(sizeof("Beep Flash Duration:") + 1) * 8, 5 * 16}, + 3, + {10, 999}, + 1, + ' '}; + + NumberField field_accent_beep_tune{ + {(sizeof("Accent Beep Tune:") + 1) * 8, 2 * 16}, + 5, + {380, 24000}, + 20, + ' '}; + + NumberField field_unaccent_beep_tune{ + {(sizeof("Unaccent Beep Tune:") + 1) * 8, 3 * 16}, + 5, + {380, 24000}, + 20, + ' '}; + + AudioVolumeField field_volume{ + {(sizeof("Volume:") + 1) * 8, 6 * 16}}; + + NewButton button_play_stop{ + {0 * 16, 16 * 16, screen_width, screen_height - 16 * 16}, + {}, + &bitmap_icon_replay, + Theme::getInstance()->fg_red->foreground}; + + ProgressBar progressbar{ + {0 * 16, 8 * 16, screen_width, screen_height - 14 * 16}}; +}; + +} // namespace ui::external_app::metronome + +#endif /*__UI_METRONOME_H__*/ \ No newline at end of file