diff --git a/firmware/application/external/external.cmake b/firmware/application/external/external.cmake index ea51c060..03fbffad 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 28a568ac..51a24ea5 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 00000000..620e8962 --- /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 00000000..75dca3b3 --- /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 00000000..d813146d --- /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