From a5705e1d6ca6fdf9c69edfc7ab1463f0471d4b02 Mon Sep 17 00:00:00 2001 From: HTotoo Date: Fri, 8 Dec 2023 10:40:19 +0100 Subject: [PATCH] Initial commit - wip --- firmware/application/CMakeLists.txt | 1 + firmware/application/apps/ui_subghzd.cpp | 173 ++++++++++++++++++++ firmware/application/apps/ui_subghzd.hpp | 182 ++++++++++++++++++++++ firmware/application/ui_navigation.cpp | 2 + firmware/baseband/fprotos/subghztypes.hpp | 16 ++ 5 files changed, 374 insertions(+) create mode 100644 firmware/application/apps/ui_subghzd.cpp create mode 100644 firmware/application/apps/ui_subghzd.hpp create mode 100644 firmware/baseband/fprotos/subghztypes.hpp diff --git a/firmware/application/CMakeLists.txt b/firmware/application/CMakeLists.txt index 762b09359..8519cf83c 100644 --- a/firmware/application/CMakeLists.txt +++ b/firmware/application/CMakeLists.txt @@ -301,6 +301,7 @@ set(CPPSRC apps/ui_spectrum_painter.cpp apps/ui_ss_viewer.cpp apps/ui_sstvtx.cpp + apps/ui_subghzd.cpp # apps/ui_test.cpp apps/ui_text_editor.cpp apps/ui_tone_search.cpp diff --git a/firmware/application/apps/ui_subghzd.cpp b/firmware/application/apps/ui_subghzd.cpp new file mode 100644 index 000000000..ad12ccca8 --- /dev/null +++ b/firmware/application/apps/ui_subghzd.cpp @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2014 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_subghzd.hpp" +#include "audio.hpp" +#include "baseband_api.hpp" +#include "string_format.hpp" +#include "portapack_persistent_memory.hpp" + +using namespace portapack; +using namespace ui; + +namespace ui { + +void SubGhzDRecentEntryDetailView::update_data() { + // set text elements + text_type.set(SubGhzDView::getSensorTypeName((FPROTO_SUBGHZD_SENSOR)entry_.sensorType)); + text_id.set("0x" + to_string_hex(entry_.id)); + text_temp.set(to_string_decimal(entry_.temp, 2)); + text_hum.set(to_string_dec_uint(entry_.humidity) + "%"); + text_ch.set(to_string_dec_uint(entry_.channel)); + text_batt.set(to_string_dec_uint(entry_.battery_low) + " " + ((entry_.battery_low == 0) ? "OK" : "LOW")); + text_age.set(to_string_dec_uint(entry_.age) + " sec"); +} + +SubGhzDRecentEntryDetailView::SubGhzDRecentEntryDetailView(NavigationView& nav, const SubGhzDRecentEntry& entry) + : nav_{nav}, + entry_{entry} { + add_children({&button_done, + &text_type, + &text_id, + &text_temp, + &text_hum, + &text_ch, + &text_batt, + &text_age, + &labels}); + + button_done.on_select = [&nav](const ui::Button&) { + nav.pop(); + }; + update_data(); +} + +void SubGhzDRecentEntryDetailView::focus() { + button_done.focus(); +} + +void SubGhzDView::focus() { + field_frequency.focus(); +} + +SubGhzDView::SubGhzDView(NavigationView& nav) + : nav_{nav} { + add_children({&rssi, + &field_rf_amp, + &field_lna, + &field_vga, + &field_frequency, + &button_clear_list, + &recent_entries_view}); + + baseband::run_image(portapack::spi_flash::image_tag_weather); + + button_clear_list.on_select = [this](Button&) { + recent.clear(); + recent_entries_view.set_dirty(); + }; + field_frequency.set_step(100000); + + 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) { + nav_.push(entry); + }; + baseband::set_weather(); + receiver_model.enable(); + signal_token_tick_second = rtc_time::signal_tick_second += [this]() { + on_tick_second(); + }; +} + +void SubGhzDView::on_tick_second() { + for (auto& entry : recent) { + entry.inc_age(1); + } + recent_entries_view.set_dirty(); +} + +void SubGhzDView::on_data(const WeatherDataMessage* data) { + SubGhzDRecentEntry 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)) { + // Found within. Move to front of list, increment counter. + (*matching_recent).reset_age(); + recent.push_front(*matching_recent); + recent.erase(matching_recent); + } else { + recent.emplace_front(key); + truncate_entries(recent, 64); + } + recent_entries_view.set_dirty(); +} + +SubGhzDView::~SubGhzDView() { + rtc_time::signal_tick_second -= signal_token_tick_second; + receiver_model.disable(); + baseband::shutdown(); +} + +const char* SubGhzDView::getSensorTypeName(FPROTO_SUBGHZD_SENSOR type) { + switch (type) { + case FPS_Invalid: + default: + return "Unknown"; + } +} + +std::string SubGhzDView::pad_string_with_spaces(int snakes) { + std::string paddedStr(snakes, ' '); + return paddedStr; +} + +template <> +void RecentEntriesTable::draw( + const Entry& entry, + const Rect& target_rect, + Painter& painter, + const Style& style) { + std::string line{}; + line.reserve(30); + + line = SubGhzDView::getSensorTypeName((FPROTO_SUBGHZD_SENSOR)entry.sensorType); + if (line.length() < 10) { + line += SubGhzDView::pad_string_with_spaces(10 - line.length()); + } else { + line = truncate(line, 10); + } + + std::string temp = to_string_decimal(entry.temp, 1); + std::string humStr = to_string_dec_uint(entry.humidity) + "%"; + std::string chStr = to_string_dec_uint(entry.channel); + std::string ageStr = to_string_dec_uint(entry.age); + + line += SubGhzDView::pad_string_with_spaces(6 - temp.length()) + temp; + line += SubGhzDView::pad_string_with_spaces(5 - humStr.length()) + humStr; + line += SubGhzDView::pad_string_with_spaces(4 - chStr.length()) + chStr; + line += SubGhzDView::pad_string_with_spaces(4 - ageStr.length()) + ageStr; + + line.resize(target_rect.width() / 8, ' '); + painter.draw_string(target_rect.location(), style, line); +} + +} // namespace ui \ No newline at end of file diff --git a/firmware/application/apps/ui_subghzd.hpp b/firmware/application/apps/ui_subghzd.hpp new file mode 100644 index 000000000..39a357951 --- /dev/null +++ b/firmware/application/apps/ui_subghzd.hpp @@ -0,0 +1,182 @@ +/* + * Copyright (C) 2014 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. + */ + +#ifndef __UI_SUBGHZD_H__ +#define __UI_SUBGHZD_H__ + +#include "ui.hpp" +#include "ui_navigation.hpp" +#include "ui_receiver.hpp" +#include "ui_freq_field.hpp" +#include "app_settings.hpp" +#include "radio_state.hpp" +#include "utility.hpp" +#include "recent_entries.hpp" + +#include "../baseband/fprotos/subghztypes.hpp" + +using namespace ui; + +namespace ui { + +struct SubGhzDRecentEntry { + using Key = uint64_t; + static constexpr Key invalid_key = 0x0fffffff; // todo calc the invalid all + uint8_t sensorType = FPS_Invalid; + uint32_t id = 0xFFFFFFFF; + float temp = -273.0f; + uint8_t humidity = 0xFF; + uint8_t battery_low = 0xFF; + uint8_t channel = 0xFF; + uint16_t age = 0; // updated on each seconds, show how long the signal was last seen + + SubGhzDRecentEntry() {} + SubGhzDRecentEntry( + uint8_t sensorType, + uint32_t id, + float temp, + uint8_t humidity, + uint8_t channel, + uint8_t battery_low = 0xff) + : sensorType{sensorType}, + id{id}, + temp{temp}, + humidity{humidity}, + battery_low{battery_low}, + channel{channel} { + } + Key key() const { + return (((static_cast(temp * 10) & 0xFFFF) << 48) ^ static_cast(id) << 24) | + (static_cast(sensorType) & 0xFF) << 16 | + (static_cast(humidity) & 0xFF) << 8 | + (static_cast(battery_low) & 0xF) << 4 | + (static_cast(channel) & 0xF); + } + void inc_age(int delta) { + if (UINT16_MAX - delta > age) age += delta; + } + void reset_age() { + age = 0; + } +}; +using SubGhzDRecentEntries = RecentEntries; +using SubGhzDRecentEntriesView = RecentEntriesView; + +class SubGhzDView : public View { + public: + SubGhzDView(NavigationView& nav); + ~SubGhzDView(); + + void focus() override; + + std::string title() const override { return "SubGhzD"; }; + static const char* getSensorTypeName(FPROTO_SUBGHZD_SENSOR type); + static std::string pad_string_with_spaces(int snakes); + + private: + void on_tick_second(); + void on_data(const WeatherDataMessage* data); // TODO + + NavigationView& nav_; + RxRadioState radio_state_{ + 433'920'000 /* frequency */, + 1'750'000 /* bandwidth */, + 4'000'000 /* sampling rate */, + ReceiverModel::Mode::AMAudio}; + app_settings::SettingsManager settings_{ + "rx_subghzd", + app_settings::Mode::RX, + {}}; + + SubGhzDRecentEntries recent{}; + + RFAmpField field_rf_amp{ + {13 * 8, 0 * 16}}; + LNAGainField field_lna{ + {15 * 8, 0 * 16}}; + VGAGainField field_vga{ + {18 * 8, 0 * 16}}; + RSSI rssi{ + {21 * 8, 0, 6 * 8, 4}}; + RxFrequencyField field_frequency{ + {0 * 8, 0 * 16}, + nav_}; + + SignalToken signal_token_tick_second{}; + + Button button_clear_list{ + {0, 16, 7 * 8, 32}, + "Clear"}; + + static constexpr auto header_height = 3 * 16; + + const RecentEntriesColumns columns{{ + {"Type", 19}, + {"Bits", 3}, + {"Age", 3}, + }}; + SubGhzDRecentEntriesView recent_entries_view{columns, recent}; + + MessageHandlerRegistration message_handler_packet{ + Message::ID::WeatherData, + [this](Message* const p) { + const auto message = static_cast(p); + this->on_data(message); + }}; +}; + +class SubGhzDRecentEntryDetailView : public View { + public: + SubGhzDRecentEntryDetailView(NavigationView& nav, const SubGhzDRecentEntry& entry); + + void update_data(); + void focus() override; + + private: + NavigationView& nav_; + SubGhzDRecentEntry entry_{}; + Text text_type{{0 * 8, 1 * 16, 15 * 8, 16}, "?"}; + Text text_id{{6 * 8, 2 * 16, 10 * 8, 16}, "?"}; + Text text_temp{{6 * 8, 3 * 16, 8 * 8, 16}, "?"}; + Text text_hum{{11 * 8, 4 * 16, 6 * 8, 16}, "?"}; + Text text_ch{{11 * 8, 5 * 16, 6 * 8, 16}, "?"}; + Text text_batt{{11 * 8, 6 * 16, 6 * 8, 16}, "?"}; + Text text_age{{11 * 8, 7 * 16, 6 * 8, 16}, "?"}; + + Labels labels{ + {{0 * 8, 0 * 16}, "Station type:", Color::light_grey()}, + {{0 * 8, 2 * 16}, "Id: ", Color::light_grey()}, + {{0 * 8, 3 * 16}, "Temp:", Color::light_grey()}, + {{0 * 8, 4 * 16}, "Humidity:", Color::light_grey()}, + {{0 * 8, 5 * 16}, "Channel:", Color::light_grey()}, + {{0 * 8, 6 * 16}, "Battery:", Color::light_grey()}, + {{0 * 8, 7 * 16}, "Age:", Color::light_grey()}, + }; + + Button button_done{ + {screen_width - 96 - 4, screen_height - 32 - 12, 96, 32}, + "Done"}; +}; + +} // namespace ui + +#endif /*__UI_SUBGHZD_H__*/ diff --git a/firmware/application/ui_navigation.cpp b/firmware/application/ui_navigation.cpp index e05845a9c..7137fcb70 100644 --- a/firmware/application/ui_navigation.cpp +++ b/firmware/application/ui_navigation.cpp @@ -80,6 +80,7 @@ #include "ui_touchtunes.hpp" #include "ui_view_wav.hpp" #include "ui_weatherstation.hpp" +#include "ui_subghzd.hpp" #include "ui_whipcalc.hpp" #include "ui_external_items_menu_loader.hpp" @@ -567,6 +568,7 @@ ReceiversMenuView::ReceiversMenuView(NavigationView& nav) { {"Search", Color::yellow(), &bitmap_icon_search, [&nav]() { nav.push(); }}, {"TPMS Cars", Color::green(), &bitmap_icon_tpms, [&nav]() { nav.push(); }}, {"Weather", Color::yellow(), &bitmap_icon_lge, [&nav]() { nav.push(); }}, + {"SubGhzD", Color::yellow(), &bitmap_icon_lge, [&nav]() { nav.push(); }}, // {"FSK RX", Color::yellow(), &bitmap_icon_remote, [&nav]() { nav.push(); }}, // {"DMR", Color::dark_grey(), &bitmap_icon_dmr, [&nav](){ nav.push(); }}, // {"SIGFOX", Color::dark_grey(), &bitmap_icon_fox, [&nav](){ nav.push(); }}, diff --git a/firmware/baseband/fprotos/subghztypes.hpp b/firmware/baseband/fprotos/subghztypes.hpp new file mode 100644 index 000000000..f5fe5f970 --- /dev/null +++ b/firmware/baseband/fprotos/subghztypes.hpp @@ -0,0 +1,16 @@ + +#ifndef __FPROTO_SUBGHZDTYPES_H__ +#define __FPROTO_SUBGHZDTYPES_H__ + +/* +Define known protocols. +These values must be present on the protocol's constructor, like FProtoWeatherAcurite592TXR() { sensorType = FPW_Acurite592TXR; } +Also it must have a switch-case element in the getSubGhzDSensorTypeName() function, to display it's name. +*/ + +enum FPROTO_SUBGHZD_SENSOR { + FPS_Invalid = 0, + +}; + +#endif \ No newline at end of file