From 131523d72616cd23ef911362c27657123814e47f Mon Sep 17 00:00:00 2001 From: Totoo Date: Wed, 20 Nov 2024 09:22:55 +0100 Subject: [PATCH] Csv from subghzd (#2375) --- firmware/application/apps/ui_subghzd.cpp | 27 ++++++++++++++++++++- firmware/application/apps/ui_subghzd.hpp | 31 +++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/firmware/application/apps/ui_subghzd.cpp b/firmware/application/apps/ui_subghzd.cpp index bab43a7d0..fa878252c 100644 --- a/firmware/application/apps/ui_subghzd.cpp +++ b/firmware/application/apps/ui_subghzd.cpp @@ -24,6 +24,7 @@ #include "audio.hpp" #include "baseband_api.hpp" #include "string_format.hpp" +#include "file_path.hpp" #include "portapack_persistent_memory.hpp" using namespace portapack; @@ -31,6 +32,18 @@ using namespace ui; namespace ui { +std::string SubGhzDRecentEntry::to_csv() { + std::string csv = ";"; + csv += SubGhzDView::getSensorTypeName((FPROTO_SUBGHZD_SENSOR)sensorType); + csv += ";" + to_string_dec_uint(bits) + ";"; + csv += to_string_hex(data, 64 / 4); + return csv; +} + +void SubGhzDLogger::log_data(SubGhzDRecentEntry& data) { + log_file.write_entry(data.to_csv()); +} + void SubGhzDRecentEntryDetailView::update_data() { // process protocol data parseProtocol(); @@ -76,16 +89,25 @@ SubGhzDView::SubGhzDView(NavigationView& nav) &field_vga, &field_frequency, &button_clear_list, + &check_log, &recent_entries_view}); baseband::run_image(portapack::spi_flash::image_tag_subghzd); + logger = std::make_unique(); button_clear_list.on_select = [this](Button&) { recent.clear(); recent_entries_view.set_dirty(); }; field_frequency.set_step(10000); - + check_log.on_select = [this](Checkbox&, bool v) { + logging = v; + if (logger && logging) { + logger->append(logs_dir.string() + "/SUBGHZDLOG_" + to_string_timestamp(rtc_time::now()) + ".CSV"); + logger->write_header(); + } + }; + check_log.set_value(logging); const Rect content_rect{0, header_height, screen_width, screen_height - header_height}; recent_entries_view.set_parent_rect(content_rect); recent_entries_view.on_select = [this](const SubGhzDRecentEntry& entry) { @@ -107,6 +129,9 @@ void SubGhzDView::on_tick_second() { void SubGhzDView::on_data(const SubGhzDDataMessage* data) { SubGhzDRecentEntry key{data->sensorType, data->data, data->bits}; + if (logger && logging) { + logger->log_data(key); + } auto matching_recent = find(recent, key.key()); if (matching_recent != std::end(recent)) { // Found within. Move to front of list, increment counter. diff --git a/firmware/application/apps/ui_subghzd.hpp b/firmware/application/apps/ui_subghzd.hpp index 20271a354..afdf79e20 100644 --- a/firmware/application/apps/ui_subghzd.hpp +++ b/firmware/application/apps/ui_subghzd.hpp @@ -34,6 +34,7 @@ #include "app_settings.hpp" #include "radio_state.hpp" #include "utility.hpp" +#include "log_file.hpp" #include "recent_entries.hpp" #include "../baseband/fprotos/subghztypes.hpp" @@ -68,6 +69,23 @@ struct SubGhzDRecentEntry { void reset_age() { age = 0; } + + std::string to_csv(); +}; + +class SubGhzDLogger { + public: + Optional append(const std::filesystem::path& filename) { + return log_file.append(filename); + } + + void log_data(SubGhzDRecentEntry& data); + void write_header() { + log_file.write_entry(";Type; Bits; Data;"); + } + + private: + LogFile log_file{}; }; using SubGhzDRecentEntries = RecentEntries; using SubGhzDRecentEntriesView = RecentEntriesView; @@ -93,10 +111,13 @@ class SubGhzDView : public View { 1'750'000 /* bandwidth */, 4'000'000 /* sampling rate */, ReceiverModel::Mode::AMAudio}; + bool logging = false; app_settings::SettingsManager settings_{ "rx_subghzd", app_settings::Mode::RX, - {}}; + { + {"log"sv, &logging}, + }}; SubGhzDRecentEntries recent{}; @@ -118,8 +139,16 @@ class SubGhzDView : public View { {0, 16, 7 * 8, 32}, "Clear"}; + Checkbox check_log{ + {10 * 8, 18}, + 3, + "Log", + true}; + static constexpr auto header_height = 3 * 16; + std::unique_ptr logger{}; + const RecentEntriesColumns columns{{ {"Type", 19}, {"Bits", 4},