From b108d975c08a90e4048c724139faf40589b15468 Mon Sep 17 00:00:00 2001 From: Bernd Herzog Date: Fri, 22 Nov 2024 09:54:34 +0100 Subject: [PATCH] fixed memory usage (#2380) * fixed memory usage in ui_about_simple * fixed memory usage in freqman_db * fixed memory usage in tone_key --- firmware/application/apps/ui_about_simple.cpp | 10 +++--- firmware/application/apps/ui_freqman.cpp | 32 +++++++++++++------ firmware/application/freqman.cpp | 7 ++-- firmware/application/freqman_db.cpp | 10 +++--- firmware/application/tone_key.cpp | 2 +- firmware/application/tone_key.hpp | 3 +- firmware/application/ui/ui_tone_key.cpp | 2 +- 7 files changed, 42 insertions(+), 24 deletions(-) diff --git a/firmware/application/apps/ui_about_simple.cpp b/firmware/application/apps/ui_about_simple.cpp index dd77cf47..281f5b01 100644 --- a/firmware/application/apps/ui_about_simple.cpp +++ b/firmware/application/apps/ui_about_simple.cpp @@ -1,5 +1,7 @@ #include "ui_about_simple.hpp" +#include + #define ROLL_SPEED_FRAME_PER_LINE 60 // cuz frame rate of pp screen is probably 60, scroll per sec @@ -7,7 +9,7 @@ namespace ui { // TODO: Generate this automatically from github // Information: a line starting with a '#' will be yellow coloured -const std::string authors_list[] = { +constexpr std::string_view authors_list[] = { "# * List of contributors * ", " ", "#Mayhem:", @@ -70,18 +72,18 @@ AboutView::AboutView(NavigationView& nav) { button_ok.focus(); }; - for (const std::string& authors_line : authors_list) { + for (auto& authors_line : authors_list) { // if it's starting with #, it's a title and we have to substract the '#' and paint yellow if (authors_line.size() > 0) { if (authors_line[0] == '#') { menu_view.add_item( - {authors_line.substr(1, authors_line.size() - 1), + {(std::string)authors_line.substr(1, authors_line.size() - 1), ui::Theme::getInstance()->fg_yellow->foreground, nullptr, nullptr}); } else { menu_view.add_item( - {authors_line, + {(std::string)authors_line, Theme::getInstance()->bg_darkest->foreground, nullptr, nullptr}); diff --git a/firmware/application/apps/ui_freqman.cpp b/firmware/application/apps/ui_freqman.cpp index 7b6ea5a7..7e230dc6 100644 --- a/firmware/application/apps/ui_freqman.cpp +++ b/firmware/application/apps/ui_freqman.cpp @@ -40,27 +40,39 @@ namespace fs = std::filesystem; // TODO: Clean up after moving to better lookup tables. using options_t = OptionsField::options_t; -extern options_t freqman_modulations; -extern options_t freqman_bandwidths[4]; -extern options_t freqman_steps; -extern options_t freqman_steps_short; + +using option_db_t = std::pair; +using options_db_t = std::vector; + +extern options_db_t freqman_modulations; +extern options_db_t freqman_bandwidths[4]; +extern options_db_t freqman_steps; +extern options_db_t freqman_steps_short; + +options_t dboptions_to_options(const options_db_t& dboptions) { + options_t options; + for (const auto& dboption : dboptions) { + options.emplace_back(dboption.first, dboption.second); + } + return options; +} /* Set options. */ void freqman_set_modulation_option(OptionsField& option) { - option.set_options(freqman_modulations); + option.set_options(dboptions_to_options(freqman_modulations)); } void freqman_set_bandwidth_option(freqman_index_t modulation, OptionsField& option) { if (is_valid(modulation)) - option.set_options(freqman_bandwidths[modulation]); + option.set_options(dboptions_to_options(freqman_bandwidths[modulation])); } void freqman_set_step_option(OptionsField& option) { - option.set_options(freqman_steps); + option.set_options(dboptions_to_options(freqman_steps)); } void freqman_set_step_option_short(OptionsField& option) { - option.set_options(freqman_steps_short); + option.set_options(dboptions_to_options(freqman_steps_short)); } namespace ui { @@ -438,7 +450,7 @@ void FrequencyEditView::populate_bandwidth_options() { auto& bandwidths = freqman_bandwidths[entry_.modulation]; for (auto i = 0u; i < bandwidths.size(); ++i) { auto& item = bandwidths[i]; - options.push_back({item.first, (OptionsField::value_t)i}); + options.push_back({(std::string)item.first, (OptionsField::value_t)i}); } } @@ -451,7 +463,7 @@ void FrequencyEditView::populate_step_options() { for (auto i = 0u; i < freqman_steps.size(); ++i) { auto& item = freqman_steps[i]; - options.push_back({item.first, (OptionsField::value_t)i}); + options.push_back({(std::string)item.first, (OptionsField::value_t)i}); } field_step.set_options(std::move(options)); diff --git a/firmware/application/freqman.cpp b/firmware/application/freqman.cpp index 17f3d66b..63632ce3 100644 --- a/firmware/application/freqman.cpp +++ b/firmware/application/freqman.cpp @@ -28,8 +28,11 @@ using option_t = ui::OptionsField::option_t; using options_t = ui::OptionsField::options_t; -extern options_t freqman_steps; -extern const option_t* find_by_index(const options_t& options, freqman_index_t index); +using option_db_t = std::pair; +using options_db_t = std::vector; + +extern options_db_t freqman_steps; +extern const option_db_t* find_by_index(const options_db_t& options, freqman_index_t index); /* Option value lookup. */ int32_t freqman_entry_get_step_value(freqman_index_t step) { diff --git a/firmware/application/freqman_db.cpp b/firmware/application/freqman_db.cpp index 61a071e5..89eb52b1 100644 --- a/firmware/application/freqman_db.cpp +++ b/firmware/application/freqman_db.cpp @@ -41,7 +41,7 @@ namespace fs = std::filesystem; const std::filesystem::path freqman_extension{u".TXT"}; // NB: Don't include UI headers to keep this code unit testable. -using option_t = std::pair; +using option_t = std::pair; using options_t = std::vector; options_t freqman_modulations = { @@ -194,27 +194,27 @@ bool operator==(const freqman_entry& lhs, const freqman_entry& rhs) { std::string freqman_entry_get_modulation_string(freqman_index_t modulation) { if (auto opt = find_by_index(freqman_modulations, modulation)) - return opt->first; + return (std::string)opt->first; return {}; } std::string freqman_entry_get_bandwidth_string(freqman_index_t modulation, freqman_index_t bandwidth) { if (modulation < freqman_modulations.size()) { if (auto opt = find_by_index(freqman_bandwidths[modulation], bandwidth)) - return opt->first; + return (std::string)opt->first; } return {}; } std::string freqman_entry_get_step_string(freqman_index_t step) { if (auto opt = find_by_index(freqman_steps, step)) - return opt->first; + return (std::string)opt->first; return {}; } std::string freqman_entry_get_step_string_short(freqman_index_t step) { if (auto opt = find_by_index(freqman_steps_short, step)) - return opt->first; + return (std::string)opt->first; return {}; } diff --git a/firmware/application/tone_key.cpp b/firmware/application/tone_key.cpp index 41e3aec2..291a42c0 100644 --- a/firmware/application/tone_key.cpp +++ b/firmware/application/tone_key.cpp @@ -96,7 +96,7 @@ float tone_key_frequency(tone_index index) { std::string tone_key_string(tone_index index) { if (index < 0 || (unsigned)index >= tone_keys.size()) return std::string(""); - return tone_keys[index].first; + return (std::string)tone_keys[index].first; } // Return string showing frequency only from specific table index diff --git a/firmware/application/tone_key.hpp b/firmware/application/tone_key.hpp index 13eae1b4..0f57e90f 100644 --- a/firmware/application/tone_key.hpp +++ b/firmware/application/tone_key.hpp @@ -26,6 +26,7 @@ #include #include +#include #include namespace tonekey { @@ -35,7 +36,7 @@ namespace tonekey { #define F2Ix100(x) (int32_t)(x * 100.0 + 0.5) // add 0.5f to round vs truncate during FP->int conversion using tone_index = int32_t; -using tone_key_t = std::vector>; +using tone_key_t = std::vector>; extern const tone_key_t tone_keys; diff --git a/firmware/application/ui/ui_tone_key.cpp b/firmware/application/ui/ui_tone_key.cpp index 019be359..814eaa35 100644 --- a/firmware/application/ui/ui_tone_key.cpp +++ b/firmware/application/ui/ui_tone_key.cpp @@ -34,7 +34,7 @@ void tone_keys_populate(OptionsField& field) { for (size_t c = 0; c < tone_keys.size(); c++) { auto f = tone_keys[c].second; if ((c != 0) && (f < 1000 * 100)) - tone_name = "CTCSS " + fx100_string(f) + " #" + tone_keys[c].first; + tone_name = "CTCSS " + fx100_string(f) + " #" + (std::string)tone_keys[c].first; else tone_name = tone_keys[c].first;