Skip to content

Commit

Permalink
fixed memory usage (#2380)
Browse files Browse the repository at this point in the history
* fixed memory usage in ui_about_simple
* fixed memory usage in freqman_db
* fixed memory usage in tone_key
  • Loading branch information
bernd-herzog authored Nov 22, 2024
1 parent fc08d4e commit b108d97
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 24 deletions.
10 changes: 6 additions & 4 deletions firmware/application/apps/ui_about_simple.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
#include "ui_about_simple.hpp"

#include <string_view>

#define ROLL_SPEED_FRAME_PER_LINE 60
// cuz frame rate of pp screen is probably 60, scroll per sec

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:",
Expand Down Expand Up @@ -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});
Expand Down
32 changes: 22 additions & 10 deletions firmware/application/apps/ui_freqman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string_view, int32_t>;
using options_db_t = std::vector<option_db_t>;

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 {
Expand Down Expand Up @@ -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});
}
}

Expand All @@ -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));
Expand Down
7 changes: 5 additions & 2 deletions firmware/application/freqman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string_view, int32_t>;
using options_db_t = std::vector<option_db_t>;

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) {
Expand Down
10 changes: 5 additions & 5 deletions firmware/application/freqman_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, int32_t>;
using option_t = std::pair<std::string_view, int32_t>;
using options_t = std::vector<option_t>;

options_t freqman_modulations = {
Expand Down Expand Up @@ -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 {};
}

Expand Down
2 changes: 1 addition & 1 deletion firmware/application/tone_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion firmware/application/tone_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#include <cstdint>
#include <string>
#include <string_view>
#include <vector>

namespace tonekey {
Expand All @@ -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<std::pair<std::string, uint32_t>>;
using tone_key_t = std::vector<std::pair<std::string_view, uint32_t>>;

extern const tone_key_t tone_keys;

Expand Down
2 changes: 1 addition & 1 deletion firmware/application/ui/ui_tone_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit b108d97

Please sign in to comment.