2016-07-25 10:21:27 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2016 Furrtek
|
|
|
|
*
|
|
|
|
* 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_freqman.hpp"
|
|
|
|
|
|
|
|
#include "portapack.hpp"
|
|
|
|
#include "event_m0.hpp"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
using namespace portapack;
|
|
|
|
|
|
|
|
namespace ui {
|
2016-12-06 07:31:38 -05:00
|
|
|
|
2017-02-03 11:22:12 -05:00
|
|
|
void FrequencySaveView::on_save_name(char * name) {
|
|
|
|
database.entries.push_back({ value_, "", name, (int32_t)options_category.selected_index_value() });
|
|
|
|
nav_.pop();
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
2017-01-29 20:09:00 -05:00
|
|
|
|
2017-02-03 11:22:12 -05:00
|
|
|
void FrequencySaveView::on_save_timestamp() {
|
2017-02-03 10:10:27 -05:00
|
|
|
database.entries.push_back({ value_, "", str_timestamp, (int32_t)options_category.selected_index_value() });
|
2017-02-03 11:22:12 -05:00
|
|
|
nav_.pop();
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
2016-12-06 07:31:38 -05:00
|
|
|
void FrequencySaveView::focus() {
|
2017-01-30 08:10:30 -05:00
|
|
|
if (error == ERROR_ACCESS) {
|
2017-01-29 20:09:00 -05:00
|
|
|
nav_.display_modal("Error", "File acces error", ABORT, nullptr);
|
2017-02-03 11:22:12 -05:00
|
|
|
} else if (error == ERROR_DUPLICATE) {
|
|
|
|
nav_.display_modal("Error", "Frequency already saved", INFO, nullptr);
|
|
|
|
error = NO_ERROR;
|
2017-01-30 08:10:30 -05:00
|
|
|
} else {
|
2017-01-29 20:09:00 -05:00
|
|
|
button_save_timestamp.focus();
|
2017-01-30 08:10:30 -05:00
|
|
|
}
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FrequencySaveView::on_tick_second() {
|
|
|
|
rtcGetTime(&RTCD1, &datetime);
|
2017-01-15 22:45:44 -05:00
|
|
|
str_timestamp = to_string_dec_uint(datetime.month(), 2, '0') + "/" + to_string_dec_uint(datetime.day(), 2, '0') + " " +
|
|
|
|
to_string_dec_uint(datetime.hour(), 2, '0') + ":" + to_string_dec_uint(datetime.minute(), 2, '0');
|
|
|
|
text_timestamp.set(str_timestamp);
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
FrequencySaveView::~FrequencySaveView() {
|
2017-01-15 22:45:44 -05:00
|
|
|
rtc_time::signal_tick_second -= signal_token_tick_second;
|
2017-02-03 10:10:27 -05:00
|
|
|
save_freqman_file(database);
|
2016-12-06 07:31:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
FrequencySaveView::FrequencySaveView(
|
|
|
|
NavigationView& nav,
|
|
|
|
const rf::Frequency value
|
2016-12-25 19:31:38 -05:00
|
|
|
) : nav_ (nav),
|
|
|
|
value_ (value)
|
|
|
|
{
|
2017-02-03 10:10:27 -05:00
|
|
|
using name_t = std::string;
|
|
|
|
using value_t = int32_t;
|
|
|
|
using option_t = std::pair<name_t, value_t>;
|
|
|
|
using options_t = std::vector<option_t>;
|
|
|
|
options_t categories;
|
2017-01-10 13:40:33 -05:00
|
|
|
File freqs_file;
|
2017-01-30 08:10:30 -05:00
|
|
|
size_t n;
|
2017-01-10 13:40:33 -05:00
|
|
|
|
2017-02-03 10:10:27 -05:00
|
|
|
if (!load_freqman_file(database)) {
|
2017-01-29 20:09:00 -05:00
|
|
|
if (!create_freqman_file(freqs_file)) {
|
|
|
|
error = ERROR_ACCESS;
|
|
|
|
return;
|
|
|
|
}
|
2017-01-10 13:40:33 -05:00
|
|
|
}
|
2016-12-25 19:31:38 -05:00
|
|
|
|
2017-02-03 10:10:27 -05:00
|
|
|
for (n = 0; n < database.entries.size(); n++) {
|
|
|
|
if (database.entries[n].value == value_) {
|
2017-01-30 08:10:30 -05:00
|
|
|
error = ERROR_DUPLICATE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
signal_token_tick_second = rtc_time::signal_tick_second += [this]() {
|
2016-12-25 19:31:38 -05:00
|
|
|
this->on_tick_second();
|
|
|
|
};
|
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
add_children({
|
2016-12-09 12:21:47 -05:00
|
|
|
&big_display,
|
|
|
|
&text_save,
|
|
|
|
&button_save_name,
|
|
|
|
&button_save_timestamp,
|
2016-12-25 19:31:38 -05:00
|
|
|
&text_timestamp,
|
2017-02-03 10:10:27 -05:00
|
|
|
&text_category,
|
|
|
|
&options_category,
|
2016-12-09 12:21:47 -05:00
|
|
|
&button_cancel
|
2017-01-15 22:45:44 -05:00
|
|
|
});
|
2017-02-03 10:10:27 -05:00
|
|
|
|
|
|
|
// Populate categories OptionsField
|
|
|
|
categories.emplace_back(std::make_pair("No cat.", -1));
|
|
|
|
for (n = 0; n < database.categories.size(); n++)
|
|
|
|
categories.emplace_back(std::make_pair(database.categories[n], n));
|
|
|
|
options_category.set_options(categories);
|
|
|
|
options_category.set_selected_index(0);
|
2016-12-09 12:21:47 -05:00
|
|
|
|
2016-12-25 19:31:38 -05:00
|
|
|
on_tick_second();
|
|
|
|
|
2016-12-09 12:21:47 -05:00
|
|
|
big_display.set(value);
|
|
|
|
|
2016-12-25 19:31:38 -05:00
|
|
|
button_save_name.on_select = [this, &nav](Button&) {
|
2017-02-03 11:22:12 -05:00
|
|
|
textentry(nav, desc_buffer, 28, [this](char * buffer) {
|
|
|
|
on_save_name(buffer);
|
|
|
|
});
|
2016-12-25 19:31:38 -05:00
|
|
|
};
|
|
|
|
button_save_timestamp.on_select = [this, &nav](Button&) {
|
2017-02-03 11:22:12 -05:00
|
|
|
on_save_timestamp();
|
2016-12-25 19:31:38 -05:00
|
|
|
};
|
2017-02-03 10:10:27 -05:00
|
|
|
|
2016-12-09 12:21:47 -05:00
|
|
|
button_cancel.on_select = [this, &nav](Button&) {
|
|
|
|
nav.pop();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-25 19:31:38 -05:00
|
|
|
void FrequencyLoadView::setup_list() {
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
menu_view.clear();
|
|
|
|
|
2017-02-03 10:10:27 -05:00
|
|
|
for (n = 0; n < database.entries.size(); n++) {
|
|
|
|
menu_view.add_item({
|
|
|
|
freqman_item_string(database.entries[n]),
|
|
|
|
ui::Color::white(),
|
|
|
|
nullptr,
|
|
|
|
[this](){
|
|
|
|
on_frequency_select();
|
|
|
|
}
|
|
|
|
});
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
menu_view.set_highlighted(menu_view.highlighted()); // Refresh
|
|
|
|
}
|
|
|
|
|
|
|
|
void FrequencyLoadView::on_frequency_select() {
|
|
|
|
nav_.pop();
|
2017-02-03 10:10:27 -05:00
|
|
|
if (on_changed) on_changed(database.entries[menu_view.highlighted()].value);
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
2016-12-09 12:21:47 -05:00
|
|
|
void FrequencyLoadView::focus() {
|
2017-01-29 20:09:00 -05:00
|
|
|
if (error == ERROR_ACCESS)
|
|
|
|
nav_.display_modal("Error", "File acces error", ABORT, nullptr);
|
|
|
|
else if (error == ERROR_EMPTY)
|
|
|
|
nav_.display_modal("Error", "Frequency DB empty", ABORT, nullptr);
|
|
|
|
else
|
|
|
|
menu_view.focus();
|
2016-12-09 12:21:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
FrequencyLoadView::FrequencyLoadView(
|
2016-12-25 19:31:38 -05:00
|
|
|
NavigationView& nav
|
|
|
|
) : nav_ (nav)
|
|
|
|
{
|
2017-02-03 10:10:27 -05:00
|
|
|
if (!load_freqman_file(database)) {
|
2017-01-29 20:09:00 -05:00
|
|
|
error = ERROR_ACCESS;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-03 10:10:27 -05:00
|
|
|
if (database.entries.size() == 0) {
|
2017-01-29 20:09:00 -05:00
|
|
|
error = ERROR_EMPTY;
|
|
|
|
return;
|
|
|
|
}
|
2016-12-06 07:31:38 -05:00
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
add_children({
|
2016-12-25 19:31:38 -05:00
|
|
|
&menu_view,
|
|
|
|
&button_cancel
|
2017-01-15 22:45:44 -05:00
|
|
|
});
|
2016-07-25 10:21:27 -04:00
|
|
|
|
2016-12-25 19:31:38 -05:00
|
|
|
setup_list();
|
|
|
|
|
2017-02-03 10:10:27 -05:00
|
|
|
// Just to allow exit on left
|
|
|
|
menu_view.on_left = [this]() {
|
|
|
|
on_frequency_select();
|
|
|
|
};
|
|
|
|
|
2016-12-25 19:31:38 -05:00
|
|
|
button_cancel.on_select = [this, &nav](Button&) {
|
2016-12-06 07:31:38 -05:00
|
|
|
nav.pop();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-25 19:31:38 -05:00
|
|
|
void FreqManView::on_frequency_select() {
|
2017-02-03 10:10:27 -05:00
|
|
|
options_category.set_selected_index(database.entries[menu_view.highlighted()].category_id + 1);
|
2016-12-25 19:31:38 -05:00
|
|
|
button_edit_freq.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreqManView::on_edit_freq(rf::Frequency f) {
|
2017-02-03 10:10:27 -05:00
|
|
|
database.entries[menu_view.highlighted()].value = f;
|
2017-02-03 11:31:43 -05:00
|
|
|
//setup_list();
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void FreqManView::on_edit_desc(NavigationView& nav) {
|
|
|
|
char desc_buffer[32] = { 0 };
|
|
|
|
|
2017-02-03 10:10:27 -05:00
|
|
|
strcpy(desc_buffer, database.entries[menu_view.highlighted()].description.c_str());
|
2016-12-25 19:31:38 -05:00
|
|
|
textentry(nav, desc_buffer, 28, [this, &desc_buffer](char * buffer) {
|
2017-02-03 10:10:27 -05:00
|
|
|
database.entries[menu_view.highlighted()].description = buffer;
|
2017-02-03 11:31:43 -05:00
|
|
|
//setup_list();
|
2016-12-25 19:31:38 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreqManView::on_delete() {
|
2017-02-03 10:10:27 -05:00
|
|
|
database.entries.erase(database.entries.begin() + menu_view.highlighted());
|
2017-02-03 11:31:43 -05:00
|
|
|
//setup_list();
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
2017-02-03 10:10:27 -05:00
|
|
|
void FreqManView::on_edit_category(int32_t category_id) {
|
|
|
|
database.entries[menu_view.highlighted()].category_id = category_id;
|
|
|
|
}
|
|
|
|
|
2016-12-25 19:31:38 -05:00
|
|
|
void FreqManView::setup_list() {
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
menu_view.clear();
|
|
|
|
|
2017-02-03 10:10:27 -05:00
|
|
|
for (n = 0; n < database.entries.size(); n++) {
|
|
|
|
menu_view.add_item({
|
|
|
|
freqman_item_string(database.entries[n]),
|
|
|
|
ui::Color::white(),
|
|
|
|
nullptr,
|
|
|
|
[this](){
|
|
|
|
on_frequency_select();
|
|
|
|
}
|
|
|
|
});
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
2017-02-03 11:22:12 -05:00
|
|
|
menu_view.set_highlighted(0); // Refresh
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
2016-12-06 07:31:38 -05:00
|
|
|
void FreqManView::focus() {
|
2017-01-29 20:09:00 -05:00
|
|
|
if (error == ERROR_ACCESS)
|
|
|
|
nav_.display_modal("Error", "File acces error", ABORT, nullptr);
|
|
|
|
else if (error == ERROR_EMPTY)
|
|
|
|
nav_.display_modal("Error", "Frequency DB empty", ABORT, nullptr);
|
|
|
|
else
|
|
|
|
menu_view.focus();
|
2016-12-25 19:31:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
FreqManView::~FreqManView() {
|
2017-02-03 10:10:27 -05:00
|
|
|
save_freqman_file(database);
|
2016-07-25 10:21:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
FreqManView::FreqManView(
|
|
|
|
NavigationView& nav
|
2016-12-25 19:31:38 -05:00
|
|
|
) : nav_ (nav)
|
|
|
|
{
|
2017-02-03 10:10:27 -05:00
|
|
|
using name_t = std::string;
|
|
|
|
using value_t = int32_t;
|
|
|
|
using option_t = std::pair<name_t, value_t>;
|
|
|
|
using options_t = std::vector<option_t>;
|
|
|
|
options_t categories;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if (!load_freqman_file(database)) {
|
2017-01-29 20:09:00 -05:00
|
|
|
error = ERROR_ACCESS;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-03 10:10:27 -05:00
|
|
|
if (database.entries.size() == 0) {
|
2017-01-29 20:09:00 -05:00
|
|
|
error = ERROR_EMPTY;
|
|
|
|
return;
|
|
|
|
}
|
2016-12-25 19:31:38 -05:00
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
add_children({
|
2016-12-25 19:31:38 -05:00
|
|
|
&menu_view,
|
2017-01-30 08:10:30 -05:00
|
|
|
&text_edit,
|
2016-12-25 19:31:38 -05:00
|
|
|
&button_edit_freq,
|
|
|
|
&button_edit_desc,
|
2017-02-03 10:10:27 -05:00
|
|
|
&text_category,
|
|
|
|
&options_category,
|
2016-12-25 19:31:38 -05:00
|
|
|
&button_del,
|
2016-12-06 07:31:38 -05:00
|
|
|
&button_exit
|
2017-01-15 22:45:44 -05:00
|
|
|
});
|
2016-12-25 19:31:38 -05:00
|
|
|
|
|
|
|
setup_list();
|
|
|
|
|
2017-02-03 10:10:27 -05:00
|
|
|
// Populate categories OptionsField
|
|
|
|
categories.emplace_back(std::make_pair("No cat.", -1));
|
|
|
|
for (n = 0; n < database.categories.size(); n++)
|
|
|
|
categories.emplace_back(std::make_pair(database.categories[n], n));
|
|
|
|
options_category.set_options(categories);
|
|
|
|
options_category.set_selected_index(0);
|
|
|
|
|
|
|
|
options_category.on_change = [this](size_t, int32_t category_id) {
|
|
|
|
on_edit_category(category_id);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Just to allow exit on left
|
|
|
|
menu_view.on_left = [this]() {
|
|
|
|
on_frequency_select();
|
|
|
|
};
|
|
|
|
|
2016-12-25 19:31:38 -05:00
|
|
|
button_edit_freq.on_select = [this, &nav](Button&) {
|
2017-02-03 10:10:27 -05:00
|
|
|
auto new_view = nav.push<FrequencyKeypadView>(database.entries[menu_view.highlighted()].value);
|
2016-12-25 19:31:38 -05:00
|
|
|
new_view->on_changed = [this](rf::Frequency f) {
|
|
|
|
on_edit_freq(f);
|
2016-07-25 10:21:27 -04:00
|
|
|
};
|
2016-12-25 19:31:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
button_edit_desc.on_select = [this, &nav](Button&) {
|
|
|
|
on_edit_desc(nav);
|
|
|
|
};
|
|
|
|
|
|
|
|
button_del.on_select = [this, &nav](Button&) {
|
|
|
|
nav.push<ModalMessageView>("Confirm", "Are you sure ?", YESNO,
|
|
|
|
[this](bool choice) {
|
|
|
|
if (choice) {
|
|
|
|
on_delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
2016-07-25 10:21:27 -04:00
|
|
|
|
2016-12-06 07:31:38 -05:00
|
|
|
button_exit.on_select = [this, &nav](Button&) {
|
2016-07-25 10:21:27 -04:00
|
|
|
nav.pop();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|