2016-12-26 01:31:38 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2016 Furrtek
|
2023-05-22 22:17:28 +02:00
|
|
|
* Copyright (C) 2023 gullradriel, Nilorea Studio Inc.
|
2023-07-11 13:48:36 -07:00
|
|
|
* Copyright (C) 2023 Kyle Reed
|
2016-12-26 01:31:38 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2023-07-08 13:04:12 -07:00
|
|
|
#include "file.hpp"
|
2016-12-26 01:31:38 +01:00
|
|
|
#include "freqman.hpp"
|
2023-07-08 13:04:12 -07:00
|
|
|
#include "tone_key.hpp"
|
|
|
|
#include "ui_widget.hpp"
|
|
|
|
|
2017-02-03 15:10:27 +00:00
|
|
|
#include <algorithm>
|
2016-12-26 01:31:38 +01:00
|
|
|
|
2023-07-08 13:04:12 -07:00
|
|
|
namespace fs = std::filesystem;
|
|
|
|
using namespace tonekey;
|
|
|
|
using namespace ui;
|
|
|
|
|
2022-09-11 16:07:47 +02:00
|
|
|
using option_t = std::pair<std::string, int32_t>;
|
|
|
|
using options_t = std::vector<option_t>;
|
|
|
|
|
2023-07-08 13:04:12 -07:00
|
|
|
extern options_t freqman_modulations;
|
|
|
|
extern options_t freqman_bandwidths[4];
|
|
|
|
extern options_t freqman_steps;
|
|
|
|
extern options_t freqman_steps_short;
|
2022-09-11 16:07:47 +02:00
|
|
|
|
2023-07-11 13:48:36 -07:00
|
|
|
extern const option_t* find_by_index(const options_t& options, freqman_index_t index);
|
2023-05-19 08:16:05 +12:00
|
|
|
|
2023-07-11 13:48:36 -07:00
|
|
|
// TODO: remove in favor of FreqmanDB
|
2023-07-08 13:04:12 -07:00
|
|
|
/* Freqman file handling. */
|
|
|
|
bool load_freqman_file(const std::string& file_stem, freqman_db& db, freqman_load_options options) {
|
2023-07-11 13:48:36 -07:00
|
|
|
return parse_freqman_file(get_freqman_path(file_stem), db, options);
|
2022-09-11 16:07:47 +02:00
|
|
|
}
|
|
|
|
|
2023-07-08 13:04:12 -07:00
|
|
|
bool delete_freqman_file(const std::string& file_stem) {
|
2023-07-11 13:48:36 -07:00
|
|
|
delete_file(get_freqman_path(file_stem));
|
|
|
|
return true;
|
2023-06-07 22:46:07 -05:00
|
|
|
}
|
|
|
|
|
2023-07-08 13:04:12 -07:00
|
|
|
bool save_freqman_file(const std::string& file_stem, freqman_db& db) {
|
2023-07-11 13:48:36 -07:00
|
|
|
auto path = get_freqman_path(file_stem);
|
|
|
|
delete_file(path);
|
2016-12-26 01:31:38 +01:00
|
|
|
|
2023-07-11 13:48:36 -07:00
|
|
|
File freqman_file;
|
|
|
|
auto error = freqman_file.create(path);
|
|
|
|
if (error)
|
2022-09-11 16:07:47 +02:00
|
|
|
return false;
|
|
|
|
|
2023-07-11 13:48:36 -07:00
|
|
|
for (size_t n = 0; n < db.size(); n++)
|
|
|
|
freqman_file.write_line(to_freqman_string(*db[n]));
|
|
|
|
|
2022-09-11 16:07:47 +02:00
|
|
|
return true;
|
2017-01-10 18:40:33 +00:00
|
|
|
}
|
|
|
|
|
2023-07-11 13:48:36 -07:00
|
|
|
bool create_freqman_file(const std::string& file_stem) {
|
|
|
|
auto fs_error = make_new_file(get_freqman_path(file_stem));
|
|
|
|
return fs_error.ok();
|
2022-09-11 16:07:47 +02:00
|
|
|
}
|
|
|
|
|
2023-07-08 13:04:12 -07:00
|
|
|
/* Set options. */
|
2023-05-19 08:16:05 +12:00
|
|
|
void freqman_set_modulation_option(OptionsField& option) {
|
2023-07-08 13:04:12 -07:00
|
|
|
option.set_options(freqman_modulations);
|
2022-09-11 16:07:47 +02:00
|
|
|
}
|
|
|
|
|
2023-05-19 08:16:05 +12:00
|
|
|
void freqman_set_bandwidth_option(freqman_index_t modulation, OptionsField& option) {
|
2023-07-08 13:04:12 -07:00
|
|
|
if (is_valid(modulation))
|
|
|
|
option.set_options(freqman_bandwidths[modulation]);
|
2022-09-11 16:07:47 +02:00
|
|
|
}
|
|
|
|
|
2023-05-19 08:16:05 +12:00
|
|
|
void freqman_set_step_option(OptionsField& option) {
|
2023-07-08 13:04:12 -07:00
|
|
|
option.set_options(freqman_steps);
|
2022-09-11 16:07:47 +02:00
|
|
|
}
|
|
|
|
|
2023-05-19 08:16:05 +12:00
|
|
|
void freqman_set_step_option_short(OptionsField& option) {
|
2023-07-08 13:04:12 -07:00
|
|
|
option.set_options(freqman_steps_short);
|
2022-09-11 16:07:47 +02:00
|
|
|
}
|
|
|
|
|
2023-07-08 13:04:12 -07:00
|
|
|
/* Option value lookup. */
|
|
|
|
// TODO: use Optional instead of magic values.
|
2023-05-19 08:16:05 +12:00
|
|
|
int32_t freqman_entry_get_modulation_value(freqman_index_t modulation) {
|
2023-07-08 13:04:12 -07:00
|
|
|
if (auto opt = find_by_index(freqman_modulations, modulation))
|
|
|
|
return opt->second;
|
2023-05-19 08:16:05 +12:00
|
|
|
return -1;
|
2022-09-11 16:07:47 +02:00
|
|
|
}
|
|
|
|
|
2023-07-08 13:04:12 -07:00
|
|
|
int32_t freqman_entry_get_bandwidth_value(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->second;
|
2022-09-11 16:07:47 +02:00
|
|
|
}
|
2023-05-19 08:16:05 +12:00
|
|
|
return -1;
|
2022-09-11 16:07:47 +02:00
|
|
|
}
|
|
|
|
|
2023-07-08 13:04:12 -07:00
|
|
|
int32_t freqman_entry_get_step_value(freqman_index_t step) {
|
|
|
|
if (auto opt = find_by_index(freqman_steps, step))
|
|
|
|
return opt->second;
|
2023-05-19 08:16:05 +12:00
|
|
|
return -1;
|
2016-12-26 01:31:38 +01:00
|
|
|
}
|