From a141f3ab48d6f099ada7bbd57a8d6ec112034d22 Mon Sep 17 00:00:00 2001 From: HTotoo Date: Tue, 19 Nov 2024 13:22:47 +0100 Subject: [PATCH 1/2] first test --- .../application/apps/ui_weatherstation.cpp | 29 +++++++++++++++++++ .../application/apps/ui_weatherstation.hpp | 26 +++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/firmware/application/apps/ui_weatherstation.cpp b/firmware/application/apps/ui_weatherstation.cpp index 21fd5911d..01e641f18 100644 --- a/firmware/application/apps/ui_weatherstation.cpp +++ b/firmware/application/apps/ui_weatherstation.cpp @@ -25,6 +25,7 @@ #include "audio.hpp" #include "baseband_api.hpp" #include "string_format.hpp" +#include "file_path.hpp" #include "portapack_persistent_memory.hpp" #include "../baseband/fprotos/fprotogeneral.hpp" @@ -35,6 +36,21 @@ namespace pmem = portapack::persistent_memory; namespace ui { +std::string WeatherRecentEntry::to_csv() { + std::string csv = ","; + csv += WeatherView::getWeatherSensorTypeName((FPROTO_WEATHER_SENSOR)sensorType); + csv += "," + to_string_dec_uint(id) + ","; + csv += to_string_decimal(temp, 2) + ","; + csv += to_string_dec_uint(humidity) + ","; + csv += to_string_dec_uint(channel) + ","; + csv += to_string_dec_uint(battery_low) + ","; + return csv; +} + +void WeatherLogger::log_data(WeatherRecentEntry& data) { + log_file.write_entry(data.to_csv()); +} + void WeatherRecentEntryDetailView::update_data() { // set text elements text_type.set(WeatherView::getWeatherSensorTypeName((FPROTO_WEATHER_SENSOR)entry_.sensorType)); @@ -98,8 +114,11 @@ WeatherView::WeatherView(NavigationView& nav) &field_frequency, &options_temperature, &button_clear_list, + &check_log, &recent_entries_view}); + logger = std::make_unique(); + baseband::run_image(portapack::spi_flash::image_tag_weather); button_clear_list.on_select = [this](Button&) { @@ -114,6 +133,13 @@ WeatherView::WeatherView(NavigationView& nav) }; options_temperature.set_selected_index(weather_units_fahr, false); + check_log.on_select = [this](Checkbox&, bool v) { + logging = v; + if (logger && logging) + logger->append(logs_dir.string() + "/WEATHERLOG_" + to_string_timestamp(rtc_time::now()) + ".TXT"); + }; + 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 WeatherRecentEntry& entry) { @@ -140,6 +166,9 @@ void WeatherView::on_tick_second() { void WeatherView::on_data(const WeatherDataMessage* data) { WeatherRecentEntry key = process_data(data); + if (logger && logging) { + logger->log_data(key); + } // WeatherRecentEntry key{data->sensorType, data->id, data->temp, data->humidity, data->channel, data->battery_low}; auto matching_recent = find(recent, key.key()); if (matching_recent != std::end(recent)) { diff --git a/firmware/application/apps/ui_weatherstation.hpp b/firmware/application/apps/ui_weatherstation.hpp index d1dfd5632..ec1ebe054 100644 --- a/firmware/application/apps/ui_weatherstation.hpp +++ b/firmware/application/apps/ui_weatherstation.hpp @@ -30,6 +30,7 @@ #include "app_settings.hpp" #include "radio_state.hpp" #include "utility.hpp" +#include "log_file.hpp" #include "recent_entries.hpp" #include "../baseband/fprotos/weathertypes.hpp" @@ -78,7 +79,22 @@ struct WeatherRecentEntry { void reset_age() { age = 0; } + + std::string to_csv(); }; + +class WeatherLogger { + public: + Optional append(const std::filesystem::path& filename) { + return log_file.append(filename); + } + + void log_data(WeatherRecentEntry& data); + + private: + LogFile log_file{}; +}; + using WeatherRecentEntries = RecentEntries; using WeatherRecentEntriesView = RecentEntriesView; @@ -108,6 +124,7 @@ class WeatherView : public View { app_settings::Mode::RX, { {"units_fahr"sv, &weather_units_fahr}, + {"log"sv, &logging}, }}; WeatherRecentEntries recent{}; @@ -140,8 +157,17 @@ class WeatherView : public View { {0, 16, 7 * 8, 32}, "Clear"}; + Checkbox check_log{ + {10 * 8, 7 * 8 + 2}, + 3, + "Log", + true}; + static constexpr auto header_height = 3 * 16; + std::unique_ptr logger{}; + bool logging = false; + const RecentEntriesColumns columns{{ {"Type", 10}, {"Temp", 5}, From 4cb74711c3a0f66248b5de241aec84349a88e8bd Mon Sep 17 00:00:00 2001 From: HTotoo Date: Tue, 19 Nov 2024 14:28:06 +0100 Subject: [PATCH 2/2] better csv --- .../application/apps/ui_weatherstation.cpp | 18 ++++++++++-------- .../application/apps/ui_weatherstation.hpp | 7 +++++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/firmware/application/apps/ui_weatherstation.cpp b/firmware/application/apps/ui_weatherstation.cpp index 01e641f18..2eae1607f 100644 --- a/firmware/application/apps/ui_weatherstation.cpp +++ b/firmware/application/apps/ui_weatherstation.cpp @@ -37,13 +37,13 @@ namespace pmem = portapack::persistent_memory; namespace ui { std::string WeatherRecentEntry::to_csv() { - std::string csv = ","; + std::string csv = ";"; csv += WeatherView::getWeatherSensorTypeName((FPROTO_WEATHER_SENSOR)sensorType); - csv += "," + to_string_dec_uint(id) + ","; - csv += to_string_decimal(temp, 2) + ","; - csv += to_string_dec_uint(humidity) + ","; - csv += to_string_dec_uint(channel) + ","; - csv += to_string_dec_uint(battery_low) + ","; + csv += ";" + to_string_dec_uint(id) + ";"; + csv += to_string_decimal(temp, 2) + ";"; + csv += to_string_dec_uint(humidity) + ";"; + csv += to_string_dec_uint(channel) + ";"; + csv += to_string_dec_uint(battery_low); return csv; } @@ -135,8 +135,10 @@ WeatherView::WeatherView(NavigationView& nav) check_log.on_select = [this](Checkbox&, bool v) { logging = v; - if (logger && logging) - logger->append(logs_dir.string() + "/WEATHERLOG_" + to_string_timestamp(rtc_time::now()) + ".TXT"); + if (logger && logging) { + logger->append(logs_dir.string() + "/WEATHERLOG_" + to_string_timestamp(rtc_time::now()) + ".CSV"); + logger->write_header(); + } }; check_log.set_value(logging); diff --git a/firmware/application/apps/ui_weatherstation.hpp b/firmware/application/apps/ui_weatherstation.hpp index ec1ebe054..6c265e517 100644 --- a/firmware/application/apps/ui_weatherstation.hpp +++ b/firmware/application/apps/ui_weatherstation.hpp @@ -90,6 +90,9 @@ class WeatherLogger { } void log_data(WeatherRecentEntry& data); + void write_header() { + log_file.write_entry(";Type; id; Temp; Hum; CH; Batt"); + } private: LogFile log_file{}; @@ -119,6 +122,7 @@ class WeatherView : public View { 1'750'000 /* bandwidth */, 2'000'000 /* sampling rate */, ReceiverModel::Mode::AMAudio}; + bool logging = false; app_settings::SettingsManager settings_{ "rx_weather", app_settings::Mode::RX, @@ -158,7 +162,7 @@ class WeatherView : public View { "Clear"}; Checkbox check_log{ - {10 * 8, 7 * 8 + 2}, + {10 * 8, 18}, 3, "Log", true}; @@ -166,7 +170,6 @@ class WeatherView : public View { static constexpr auto header_height = 3 * 16; std::unique_ptr logger{}; - bool logging = false; const RecentEntriesColumns columns{{ {"Type", 10},