diff --git a/firmware/application/CMakeLists.txt b/firmware/application/CMakeLists.txt index 85da7d03..d40f64ec 100644 --- a/firmware/application/CMakeLists.txt +++ b/firmware/application/CMakeLists.txt @@ -173,6 +173,7 @@ set(CPPSRC ui_coasterp.cpp ui_debug.cpp ui_encoders.cpp + ui_fileman.cpp ui_font_fixed_8x16.cpp ui_freqman.cpp ui_geomap.cpp diff --git a/firmware/application/bitmap.hpp b/firmware/application/bitmap.hpp index da1d45b5..91318392 100644 --- a/firmware/application/bitmap.hpp +++ b/firmware/application/bitmap.hpp @@ -727,28 +727,6 @@ static constexpr Bitmap bitmap_icon_setup { { 16, 16 }, bitmap_icon_setup_data }; -static constexpr uint8_t bitmap_target_data[] = { - 0x80, 0x00, - 0x80, 0x00, - 0xE0, 0x03, - 0x90, 0x04, - 0x88, 0x08, - 0x04, 0x10, - 0x04, 0x10, - 0x1F, 0x7C, - 0x04, 0x10, - 0x04, 0x10, - 0x88, 0x08, - 0x90, 0x04, - 0xE0, 0x03, - 0x80, 0x00, - 0x80, 0x00, - 0x00, 0x00, -}; -static constexpr Bitmap bitmap_target { - { 16, 16 }, bitmap_target_data -}; - static constexpr uint8_t bitmap_sig_saw_down_data[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -1173,6 +1151,28 @@ static constexpr Bitmap bitmap_icon_hackrf { { 16, 16 }, bitmap_icon_hackrf_data }; +static constexpr uint8_t bitmap_icon_file_data[] = { + 0xFC, 0x03, + 0x04, 0x06, + 0x04, 0x0E, + 0x04, 0x1E, + 0x04, 0x3E, + 0x04, 0x20, + 0x04, 0x20, + 0x04, 0x20, + 0x04, 0x20, + 0x04, 0x20, + 0x04, 0x20, + 0x04, 0x20, + 0x04, 0x20, + 0x04, 0x20, + 0x04, 0x20, + 0xFC, 0x3F, +}; +static constexpr Bitmap bitmap_icon_file { + { 16, 16 }, bitmap_icon_file_data +}; + static constexpr uint8_t bitmap_icon_remote_data[] = { 0x20, 0x00, 0x20, 0x00, @@ -1387,6 +1387,28 @@ static constexpr Bitmap bitmap_sig_sine { { 32, 32 }, bitmap_sig_sine_data }; +static constexpr uint8_t bitmap_icon_dir_data[] = { + 0x00, 0x00, + 0xFC, 0x00, + 0x02, 0x01, + 0x01, 0x3E, + 0x01, 0xE0, + 0x01, 0xA0, + 0x01, 0xA0, + 0x01, 0xA0, + 0x01, 0xA0, + 0x02, 0x40, + 0x02, 0x40, + 0x02, 0x40, + 0x02, 0x40, + 0x02, 0x40, + 0xFC, 0x3F, + 0x00, 0x00, +}; +static constexpr Bitmap bitmap_icon_dir { + { 16, 16 }, bitmap_icon_dir_data +}; + static constexpr uint8_t bitmap_sig_noise_data[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -1545,6 +1567,28 @@ static constexpr Bitmap bitmap_target_verify { { 32, 32 }, bitmap_target_verify_data }; +static constexpr uint8_t bitmap_target_data[] = { + 0x80, 0x00, + 0x80, 0x00, + 0xE0, 0x03, + 0x90, 0x04, + 0x88, 0x08, + 0x04, 0x10, + 0x04, 0x10, + 0x1F, 0x7C, + 0x04, 0x10, + 0x04, 0x10, + 0x88, 0x08, + 0x90, 0x04, + 0xE0, 0x03, + 0x80, 0x00, + 0x80, 0x00, + 0x00, 0x00, +}; +static constexpr Bitmap bitmap_target { + { 16, 16 }, bitmap_target_data +}; + static constexpr uint8_t bitmap_icon_audiotx_data[] = { 0x00, 0x70, 0x00, 0x7F, diff --git a/firmware/application/file.cpp b/firmware/application/file.cpp index 2165ede5..19d1bf28 100644 --- a/firmware/application/file.cpp +++ b/firmware/application/file.cpp @@ -197,6 +197,18 @@ std::vector scan_root_directories(const std::filesystem:: return directory_list; } +void delete_file(const std::filesystem::path& file_path) { + f_unlink(reinterpret_cast(file_path.c_str())); +} + +void rename_file(const std::filesystem::path& file_path, const std::filesystem::path& new_name) { + f_rename(reinterpret_cast(file_path.c_str()), reinterpret_cast(new_name.c_str())); +} + +uint32_t make_new_directory(const std::filesystem::path& dir_path) { + return f_mkdir(reinterpret_cast(dir_path.c_str())); +} + namespace std { namespace filesystem { diff --git a/firmware/application/file.hpp b/firmware/application/file.hpp index 281012f4..c5ea1b1e 100644 --- a/firmware/application/file.hpp +++ b/firmware/application/file.hpp @@ -236,6 +236,9 @@ space_info space(const path& p); std::vector scan_root_files(const std::filesystem::path& directory, const std::filesystem::path& extension); std::vector scan_root_directories(const std::filesystem::path& directory); std::filesystem::path next_filename_stem_matching_pattern(std::filesystem::path filename_stem_pattern); +void delete_file(const std::filesystem::path& file_path); +void rename_file(const std::filesystem::path& file_path, const std::filesystem::path& new_name); +uint32_t make_new_directory(const std::filesystem::path& dir_path); /* Values added to FatFs FRESULT enum, values outside the FRESULT data type */ static_assert(sizeof(FIL::err) == 1, "FatFs FIL::err size not expected."); diff --git a/firmware/application/ui_fileman.cpp b/firmware/application/ui_fileman.cpp new file mode 100644 index 00000000..287b48c4 --- /dev/null +++ b/firmware/application/ui_fileman.cpp @@ -0,0 +1,318 @@ +/* + * Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc. + * Copyright (C) 2017 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_fileman.hpp" +#include "string_format.hpp" +#include "portapack.hpp" +#include "event_m0.hpp" + +using namespace portapack; + +namespace ui { + +void FileManBaseView::load_directory_contents(const std::filesystem::path& dir_path) { + current_path = dir_path; + + text_current.set(dir_path.string()); + + entry_list.clear(); + + for (const auto& entry : std::filesystem::directory_iterator(dir_path, u"*")) { + if (std::filesystem::is_regular_file(entry.status())) { + entry_list.push_back({ entry.path(), (uint32_t)entry.size(), false }); + } else if (std::filesystem::is_directory(entry.status())) { + entry_list.insert(entry_list.begin(), { entry.path(), 0, true }); + } + } +} + +std::filesystem::path FileManBaseView::get_absolute_path() { + std::string current_path_str = current_path.string(); + + if (current_path_str.back() != '/') + current_path_str += '/'; + current_path_str += (entry_list[menu_view.highlighted()].entry_path.string()); + + return current_path_str; +} + +FileManBaseView::FileManBaseView( + NavigationView& nav +) : nav_ (nav) +{ + load_directory_contents(current_path); + + if (!entry_list.size()) + error_ = true; + + add_children({ + &labels, + &text_current, + &button_exit + }); + + button_exit.on_select = [this, &nav](Button&) { + nav.pop(); + }; +}; + +void FileManBaseView::focus() { + + if (error_) { + button_exit.focus(); + nav_.display_modal("Error", "No files in root.", ABORT, nullptr); + } else { + menu_view.focus(); + } +} + +void FileManBaseView::refresh_list() { + std::string size_str { }; + uint32_t suffix_index; + size_t file_size; + + if (!entry_list.size()) { + if (on_refresh_widgets) + on_refresh_widgets(true); + } else { + if (on_refresh_widgets) + on_refresh_widgets(false); + + menu_view.clear(); + + for (size_t n = 0; n < entry_list.size(); n++) { + auto entry_name = entry_list[n].entry_path.filename().string(); + + if (entry_list[n].is_directory) { + + menu_view.add_item({ + entry_name, + ui::Color::yellow(), + &bitmap_icon_dir, + [this](){ + if (on_select_entry) + on_select_entry(); + } + }); + + } else { + + file_size = entry_list[n].size; + suffix_index = 0; + + while (file_size >= 1024) { + file_size /= 1024; + suffix_index++; + } + if (suffix_index > 4) + suffix_index = 4; + + size_str = to_string_dec_uint(file_size) + suffix[suffix_index]; + + menu_view.add_item({ + entry_name + std::string(21 - entry_name.length(), ' ') + size_str, + ui::Color::white(), + &bitmap_icon_file, + [this](){ + if (on_select_entry) + on_select_entry(); + } + }); + + } + } + + menu_view.set_highlighted(0); // Refresh + } +} + +void FileSaveView::on_save_name() { + /*text_prompt(nav_, &filename_buffer, 8, [this](std::string * buffer) { + //database.entries.push_back({ value_, "", *buffer }); + //save_freqman_file(entry_list[current_category_id], database); + nav_.pop(); + });*/ +} + +void FileSaveView::on_tick_second() { + rtcGetTime(&RTCD1, &datetime); + 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); +} + +FileSaveView::~FileSaveView() { + rtc_time::signal_tick_second -= signal_token_tick_second; +} + +FileSaveView::FileSaveView( + NavigationView& nav +) : FileManBaseView(nav) +{ + filename_buffer.reserve(8); + + signal_token_tick_second = rtc_time::signal_tick_second += [this]() { + this->on_tick_second(); + }; + + add_children({ + &text_save, + &button_save_name, + &text_timestamp + }); + + on_tick_second(); + + button_save_name.on_select = [this, &nav](Button&) { + on_save_name(); + }; +} + +void FileLoadView::refresh_widgets(const bool v) { + menu_view.hidden(v); + text_empty.hidden(!v); + set_dirty(); +} + +FileLoadView::FileLoadView( + NavigationView& nav +) : FileManBaseView(nav) +{ + on_refresh_widgets = [this](bool v) { + refresh_widgets(v); + }; + + add_children({ + &menu_view, + &text_empty + }); + + // Resize menu view to fill screen + menu_view.set_parent_rect({ 0, 3 * 8, 240, 29 * 8 }); + + // Just to allow exit on left + menu_view.on_left = [&nav, this]() { + nav.pop(); + }; + + refresh_list(); + + on_select_entry = [&nav, this]() { + nav_.pop(); + if (on_changed) + on_changed(entry_list[menu_view.highlighted()].entry_path); + }; +} + +void FileManagerView::on_rename(NavigationView& nav) { + text_prompt(nav, &filename_buffer, 12, [this](std::string * buffer) { + rename_file(get_absolute_path(), *buffer); + load_directory_contents(current_path); + refresh_list(); + }); +} + +void FileManagerView::on_delete() { + delete_file(get_absolute_path()); + load_directory_contents(current_path); + refresh_list(); +} + +void FileManagerView::refresh_widgets(const bool v) { + button_rename.hidden(v); + button_new_dir.hidden(v); + button_delete.hidden(v); + menu_view.hidden(v); + text_empty.hidden(!v); + set_dirty(); +} + +FileManagerView::~FileManagerView() { + // Flush ? +} + +FileManagerView::FileManagerView( + NavigationView& nav +) : FileManBaseView(nav) +{ + on_refresh_widgets = [this](bool v) { + refresh_widgets(v); + }; + + add_children({ + //&labels, + &menu_view, + &text_empty, + &button_rename, + &button_new_dir, + &button_delete + }); + + // Go back one level on left + menu_view.on_left = [&nav, this]() { + std::string current_path_str = current_path.string(); + + current_path_str = current_path_str.substr(0, current_path_str.find_last_of('/')); + load_directory_contents(current_path_str); + refresh_list(); + }; + + refresh_list(); + + on_select_entry = [this]() { + if (entry_list[menu_view.highlighted()].is_directory) { + load_directory_contents(get_absolute_path()); + refresh_list(); + } else + button_rename.focus(); + }; + + button_new_dir.on_select = [this, &nav](Button&) { + filename_buffer = ""; + + text_prompt(nav, &filename_buffer, 12, [this](std::string * buffer) { + std::string path_str = *buffer; + + make_new_directory(current_path.string() + '/' + path_str); + load_directory_contents(current_path); + refresh_list(); + }); + }; + + button_rename.on_select = [this, &nav](Button&) { + if (!entry_list[menu_view.highlighted()].is_directory) { + filename_buffer = entry_list[menu_view.highlighted()].entry_path.filename().string(); + on_rename(nav); + } + }; + + button_delete.on_select = [this, &nav](Button&) { + nav.push("Delete", "Delete " + entry_list[menu_view.highlighted()].entry_path.filename().string() + "\nAre you sure ?", YESNO, + [this](bool choice) { + if (choice) + on_delete(); + } + ); + }; +} + +} diff --git a/firmware/application/ui_fileman.hpp b/firmware/application/ui_fileman.hpp new file mode 100644 index 00000000..791da01f --- /dev/null +++ b/firmware/application/ui_fileman.hpp @@ -0,0 +1,161 @@ +/* + * Copyright (C) 2015 Jared Boone, ShareBrained Technology, Inc. + * Copyright (C) 2017 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.hpp" +#include "ui_widget.hpp" +#include "ui_painter.hpp" +#include "ui_menu.hpp" +#include "file.hpp" +#include "ui_navigation.hpp" +#include "ui_textentry.hpp" +#include "rtc_time.hpp" + +namespace ui { + +struct fileman_entry { + std::filesystem::path entry_path { }; + uint32_t size { }; + bool is_directory { }; +}; + +class FileManBaseView : public View { +public: + FileManBaseView( + NavigationView& nav + ); + + void focus() override; + + void load_directory_contents(const std::filesystem::path& dir_path); + std::filesystem::path get_absolute_path(); + + std::string title() const override { return "File manager"; }; + +protected: + NavigationView& nav_; + + const std::string suffix[5] = { "B", "kB", "MB", "GB", "??" }; + + bool error_ { false }; + std::function on_select_entry { nullptr }; + std::function on_refresh_widgets { nullptr }; + std::vector entry_list { }; + std::filesystem::path current_path { u"" }; + + void change_category(int32_t category_id); + void refresh_list(); + + Labels labels { + { { 0, 0 }, "Current:", Color::light_grey() } + }; + Text text_current { + { 8 * 8, 0 * 8, 22 * 8, 16 }, + "", + }; + + MenuView menu_view { + { 0, 2 * 8, 240, 26 * 8 }, + true + }; + Text text_empty { + { 7 * 8, 12 * 8, 16 * 8, 16 }, + "Empty directory !", + }; + + Button button_exit { + { 20 * 8, 34 * 8, 10 * 8, 4 * 8 }, + "Exit" + }; +}; + +class FileSaveView : public FileManBaseView { +public: + FileSaveView(NavigationView& nav); + ~FileSaveView(); + +private: + std::string filename_buffer { }; + rtc::RTC datetime { }; + std::string str_timestamp { }; + + void on_save_name(); + void on_tick_second(); + + SignalToken signal_token_tick_second { }; + + Text text_save { + { 4 * 8, 15 * 8, 8 * 8, 16 }, + "Save as:", + }; + Button button_save_name { + { 4 * 8, 18 * 8, 12 * 8, 32 }, + "Name (set)" + }; + Text text_timestamp { + { 17 * 8, 24 * 8, 11 * 8, 16 }, + "MM/DD HH:MM", + }; +}; + +class FileLoadView : public FileManBaseView { +public: + std::function on_changed { }; + + FileLoadView(NavigationView& nav); + +private: + void refresh_widgets(const bool v); +}; + +class FileManagerView : public FileManBaseView { +public: + FileManagerView(NavigationView& nav); + ~FileManagerView(); + +private: + std::string filename_buffer { }; + + void refresh_widgets(const bool v); + void on_rename(NavigationView& nav); + void on_delete(); + + /*Labels labels { + { { 4 * 8 + 4, 26 * 8 }, "Edit:", Color::light_grey() } + };*/ + + Button button_rename { + { 0 * 8, 28 * 8, 14 * 8, 32 }, + "Rename" + }; + + Button button_new_dir { + { 0 * 8, 33 * 8, 14 * 8, 32 }, + "New dir" + }; + + Button button_delete { + { 18 * 8, 28 * 8, 12 * 8, 32 }, + "Delete" + }; +}; + +} /* namespace ui */ diff --git a/firmware/application/ui_navigation.cpp b/firmware/application/ui_navigation.cpp index 949ce9b6..16c6439a 100644 --- a/firmware/application/ui_navigation.cpp +++ b/firmware/application/ui_navigation.cpp @@ -40,6 +40,7 @@ #include "ui_siggen.hpp" #include "ui_debug.hpp" #include "ui_encoders.hpp" +#include "ui_fileman.hpp" #include "ui_freqman.hpp" #include "ui_jammer.hpp" #include "ui_lcr.hpp" @@ -327,10 +328,11 @@ TransmittersMenuView::TransmittersMenuView(NavigationView& nav) { UtilitiesMenuView::UtilitiesMenuView(NavigationView& nav) { add_items({ - { "Frequency manager", ui::Color::green(), &bitmap_icon_freqman, [&nav](){ nav.push(); } }, - { "Whip antenna length", ui::Color::yellow(),nullptr, [&nav](){ nav.push(); } }, - { "Notepad", ui::Color::grey(), &bitmap_icon_notepad, [&nav](){ nav.push(); } }, - { "Wipe SD card", ui::Color::red(), nullptr, [&nav](){ nav.push(); } }, + { "Frequency manager", ui::Color::green(), &bitmap_icon_freqman, [&nav](){ nav.push(); } }, + { "File manager", ui::Color::yellow(), &bitmap_icon_file, [&nav](){ nav.push(); } }, + { "Whip antenna length", ui::Color::yellow(), nullptr, [&nav](){ nav.push(); } }, + { "Notepad", ui::Color::grey(), &bitmap_icon_notepad, [&nav](){ nav.push(); } }, + { "Wipe SD card", ui::Color::red(), nullptr, [&nav](){ nav.push(); } }, }); on_left = [&nav](){ nav.pop(); }; } diff --git a/firmware/graphics/icon_dir.png b/firmware/graphics/icon_dir.png new file mode 100644 index 00000000..83a66a3e Binary files /dev/null and b/firmware/graphics/icon_dir.png differ diff --git a/firmware/graphics/icon_file.png b/firmware/graphics/icon_file.png new file mode 100644 index 00000000..b975b846 Binary files /dev/null and b/firmware/graphics/icon_file.png differ