From 2648f18b12835dee9ce96057a857910bf6b8a6b8 Mon Sep 17 00:00:00 2001 From: Totoo Date: Thu, 7 Mar 2024 17:22:24 +0100 Subject: [PATCH] External sensor tester app (#1949) * added tilt * added extsensors app * added icon * Added EnvironmentData to ExtSensor app and message.hpp * Added gotenv command --- firmware/application/external/external.cmake | 6 ++ firmware/application/external/external.ld | 9 ++ .../application/external/extsensors/main.cpp | 82 ++++++++++++++++ .../external/extsensors/ui_extsensors.cpp | 86 +++++++++++++++++ .../external/extsensors/ui_extsensors.hpp | 96 +++++++++++++++++++ firmware/application/usb_serial_shell.cpp | 27 +++++- firmware/common/message.hpp | 27 +++++- firmware/common/spi_image.hpp | 1 + 8 files changed, 329 insertions(+), 5 deletions(-) create mode 100644 firmware/application/external/extsensors/main.cpp create mode 100644 firmware/application/external/extsensors/ui_extsensors.cpp create mode 100644 firmware/application/external/extsensors/ui_extsensors.hpp diff --git a/firmware/application/external/external.cmake b/firmware/application/external/external.cmake index c4fc0c4c..b37b0f08 100644 --- a/firmware/application/external/external.cmake +++ b/firmware/application/external/external.cmake @@ -62,6 +62,11 @@ set(EXTCPPSRC external/keyfob/main.cpp external/keyfob/ui_keyfob.cpp external/keyfob/ui_keyfob.hpp + + #extsensors + external/extsensors/main.cpp + external/extsensors/ui_extsensors.cpp + external/extsensors/ui_extsensors.hpp ) set(EXTAPPLIST @@ -80,4 +85,5 @@ set(EXTAPPLIST spainter keyfob tetris + extsensors ) diff --git a/firmware/application/external/external.ld b/firmware/application/external/external.ld index 26fda937..3249ea45 100644 --- a/firmware/application/external/external.ld +++ b/firmware/application/external/external.ld @@ -38,6 +38,7 @@ MEMORY ram_external_app_spainter(rwx) : org = 0xADBC0000, len = 32k ram_external_app_keyfob(rwx) : org = 0xADBD0000, len = 32k ram_external_app_tetris(rwx) : org = 0xADBE0000, len = 32k + ram_external_app_extsensors(rwx) : org = 0xADBF0000, len = 32k } SECTIONS @@ -132,4 +133,12 @@ SECTIONS *(*ui*external_app*tetris*); } > ram_external_app_tetris + .external_app_extsensors : ALIGN(4) SUBALIGN(4) + { + KEEP(*(.external_app.app_extsensors.application_information)); + *(*ui*external_app*extsensors*); + } > ram_external_app_extsensors + + + } diff --git a/firmware/application/external/extsensors/main.cpp b/firmware/application/external/extsensors/main.cpp new file mode 100644 index 00000000..27926409 --- /dev/null +++ b/firmware/application/external/extsensors/main.cpp @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2023 Bernd Herzog + * + * 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_extsensors.hpp" +#include "ui_navigation.hpp" +#include "external_app.hpp" + +namespace ui::external_app::extsensors { +void initialize_app(ui::NavigationView& nav) { + nav.push(); +} +} // namespace ui::external_app::extsensors + +extern "C" { + +__attribute__((section(".external_app.app_extsensors.application_information"), used)) application_information_t _application_information_extsensors = { + /*.memory_location = */ (uint8_t*)0x00000000, + /*.externalAppEntry = */ ui::external_app::extsensors::initialize_app, + /*.header_version = */ CURRENT_HEADER_VERSION, + /*.app_version = */ VERSION_MD5, + + /*.app_name = */ "ExtSensor", + /*.bitmap_data = */ { + 0x00, + 0x00, + 0x00, + 0x00, + 0x04, + 0x20, + 0x12, + 0x48, + 0x8A, + 0x51, + 0xCA, + 0x53, + 0xCA, + 0x53, + 0x8A, + 0x51, + 0x12, + 0x48, + 0x84, + 0x21, + 0xC0, + 0x03, + 0x40, + 0x02, + 0x60, + 0x06, + 0x20, + 0x04, + 0x30, + 0x0C, + 0xF0, + 0x0F, + }, + /*.icon_color = */ ui::Color::orange().v, + /*.menu_location = */ app_location_t::DEBUG, + + /*.m4_app_tag = portapack::spi_flash::image_tag_none */ {0, 0, 0, 0}, + /*.m4_app_offset = */ 0x00000000, // will be filled at compile time +}; +} diff --git a/firmware/application/external/extsensors/ui_extsensors.cpp b/firmware/application/external/extsensors/ui_extsensors.cpp new file mode 100644 index 00000000..572effe6 --- /dev/null +++ b/firmware/application/external/extsensors/ui_extsensors.cpp @@ -0,0 +1,86 @@ +/* + * 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_extsensors.hpp" + +#include "rtc_time.hpp" +#include "string_format.hpp" +#include "portapack_persistent_memory.hpp" + +using namespace portapack; +using namespace ui; + +namespace ui::external_app::extsensors { + +void ExtSensorsView::focus() { + text_info.focus(); +} + +ExtSensorsView::ExtSensorsView(NavigationView& nav) + : nav_{nav} { + add_children({&labels, + &text_info, + &text_gps, + &text_orientation, + &text_envl1, + &text_envl2}); +} + +ExtSensorsView::~ExtSensorsView() { +} + +void ExtSensorsView::on_any() { + if (has_data == false) { + // update text + text_info.set("Found ext module."); // todo do an ext module check for type + } + has_data = true; +} + +void ExtSensorsView::on_gps(const GPSPosDataMessage* msg) { + on_any(); + std::string tmp = to_string_decimal(msg->lat, 5); + tmp += "; "; + tmp += to_string_decimal(msg->lon, 5); + text_gps.set(tmp); +} + +void ExtSensorsView::on_orientation(const OrientationDataMessage* msg) { + on_any(); + std::string tmp = to_string_dec_uint(msg->angle); + tmp += (char)176; // ° + if (msg->tilt < 400) { + tmp += "; T: " + to_string_dec_int(msg->tilt); + } + text_orientation.set(tmp); +} + +void ExtSensorsView::on_environment(const EnvironmentDataMessage* msg) { + on_any(); + std::string tmp = "T: " + to_string_decimal(msg->temperature, 2); // temperature + tmp += (char)176; // ° + tmp += "C"; + tmp += "; H: " + to_string_decimal(msg->humidity, 1) + "%"; // humidity + text_envl1.set(tmp); + tmp = "P: " + to_string_decimal(msg->pressure, 1) + " hPa; L:"; // pressure + tmp += to_string_dec_int(msg->light) + " LUX"; // light + text_envl2.set(tmp); +} + +} // namespace ui::external_app::extsensors \ No newline at end of file diff --git a/firmware/application/external/extsensors/ui_extsensors.hpp b/firmware/application/external/extsensors/ui_extsensors.hpp new file mode 100644 index 00000000..e068ff6d --- /dev/null +++ b/firmware/application/external/extsensors/ui_extsensors.hpp @@ -0,0 +1,96 @@ +/* + * 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. + */ + +// Code from https://github.com/Flipper-XFW/Xtreme-Apps/tree/04c3a60093e2c2378e79498b4505aa8072980a42/ble_spam/protocols +// Thanks for the work of the original creators! +// Saying thanks in the main view! + +#ifndef __UI_EXTSENSORS_H__ +#define __UI_EXTSENSORS_H__ + +#include "ui.hpp" +#include "ui_language.hpp" +#include "ui_navigation.hpp" +#include "ui_record_view.hpp" +#include "app_settings.hpp" +#include "utility.hpp" + +using namespace ui; + +namespace ui::external_app::extsensors { + +class ExtSensorsView : public View { + public: + ExtSensorsView(NavigationView& nav); + ~ExtSensorsView(); + + void focus() override; + + std::string title() const override { + return "ExtSensor"; + }; + + private: + NavigationView& nav_; + + bool has_data = false; + + Labels labels{ + {{0 * 8, 3 * 16}, "GPS:", Color::light_grey()}, + {{0 * 8, 5 * 16}, "ORI:", Color::light_grey()}, + {{0 * 8, 7 * 16}, "ENV:", Color::light_grey()}}; + + Text text_info{{0 * 8, 0 * 8, 30 * 8, 16 * 1}, "Connect a compatible module..."}; + Text text_gps{{5 * 8, 3 * 16, 24 * 8, 16}, "-"}; + Text text_orientation{{5 * 8, 5 * 16, 24 * 8, 16}, "-"}; + Text text_envl1{{5 * 8, 7 * 16, 24 * 8, 16}, "-"}; + Text text_envl2{{1 * 8, 9 * 16, 24 * 8, 16}, "-"}; + + void on_any(); + + void on_gps(const GPSPosDataMessage* msg); + void on_orientation(const OrientationDataMessage* msg); + void on_environment(const EnvironmentDataMessage* msg); + + MessageHandlerRegistration message_handler_gps{ + Message::ID::GPSPosData, + [this](Message* const p) { + const auto message = static_cast(p); + this->on_gps(message); + }}; + MessageHandlerRegistration message_handler_orientation{ + Message::ID::OrientationData, + [this](Message* const p) { + const auto message = static_cast(p); + this->on_orientation(message); + }}; + + MessageHandlerRegistration message_handler_environment{ + Message::ID::EnvironmentData, + [this](Message* const p) { + const auto message = static_cast(p); + this->on_environment(message); + }}; +}; +}; // namespace ui::external_app::extsensors + +#endif /*__UI_EXTSENSORS_H__*/ diff --git a/firmware/application/usb_serial_shell.cpp b/firmware/application/usb_serial_shell.cpp index 0f952f75..e00b7f4b 100644 --- a/firmware/application/usb_serial_shell.cpp +++ b/firmware/application/usb_serial_shell.cpp @@ -922,13 +922,33 @@ static void cmd_gotgps(BaseSequentialStream* chp, int argc, char* argv[]) { } static void cmd_gotorientation(BaseSequentialStream* chp, int argc, char* argv[]) { - const char* usage = "usage: gotorientation \r\n"; - if (argc != 1) { + const char* usage = "usage: gotorientation [tilt]\r\n"; + if (argc != 1 && argc != 2) { chprintf(chp, usage); return; } uint16_t angle = strtol(argv[0], NULL, 10); - OrientationDataMessage msg{angle}; + int16_t tilt = 400; + if (argc >= 2) tilt = strtol(argv[1], NULL, 10); + OrientationDataMessage msg{angle, tilt}; + EventDispatcher::send_message(msg); + chprintf(chp, "ok\r\n"); +} + +static void cmd_gotenv(BaseSequentialStream* chp, int argc, char* argv[]) { + const char* usage = "usage: gotenv [humidity] [pressure] [light]\r\n"; + if (argc < 1 || argc > 4) { + chprintf(chp, usage); + return; + } + float temp = atof(argv[0]); + float humi = 0; + float pressure = 0; + uint16_t light = 0; + if (argc > 1) humi = atof(argv[1]); + if (argc > 2) pressure = atof(argv[2]); + if (argc > 3) light = strtol(argv[3], NULL, 10); + EnvironmentDataMessage msg{temp, humi, pressure, light}; EventDispatcher::send_message(msg); chprintf(chp, "ok\r\n"); } @@ -1055,6 +1075,7 @@ static const ShellCommand commands[] = { {"appstart", cmd_appstart}, {"gotgps", cmd_gotgps}, {"gotorientation", cmd_gotorientation}, + {"gotenv", cmd_gotenv}, {"sysinfo", cmd_sysinfo}, {"radioinfo", cmd_radioinfo}, {"pmemreset", cmd_pmemreset}, diff --git a/firmware/common/message.hpp b/firmware/common/message.hpp index aae2239a..54f5d9b5 100644 --- a/firmware/common/message.hpp +++ b/firmware/common/message.hpp @@ -120,6 +120,7 @@ class Message { SubGhzDData = 62, GPSPosData = 63, OrientationData = 64, + EnvironmentData = 65, MAX }; @@ -1328,11 +1329,33 @@ class GPSPosDataMessage : public Message { class OrientationDataMessage : public Message { public: constexpr OrientationDataMessage( - uint16_t angle = 400) + uint16_t angle = 400, + int16_t tilt = 400) : Message{ID::OrientationData}, - angle{angle} { + angle{angle}, + tilt{tilt} { } uint16_t angle = 400; //>360 -> no orientation set + int16_t tilt = 400; +}; + +class EnvironmentDataMessage : public Message { + public: + constexpr EnvironmentDataMessage( + float temperature = 0, + float humidity = 0, + float pressure = 0, + uint16_t light = 0) + : Message{ID::EnvironmentData}, + temperature{temperature}, + humidity{humidity}, + pressure{pressure}, + light{light} { + } + float temperature = 0; // celsius + float humidity = 0; // percent (rh) + float pressure = 0; // hpa + uint16_t light = 0; // lux }; #endif /*__MESSAGE_H__*/ diff --git a/firmware/common/spi_image.hpp b/firmware/common/spi_image.hpp index 27b6fda5..9ee84fb6 100644 --- a/firmware/common/spi_image.hpp +++ b/firmware/common/spi_image.hpp @@ -73,6 +73,7 @@ struct image_tag_t { char c[4]; }; +constexpr image_tag_t image_tag_none{0, 0, 0, 0}; constexpr image_tag_t image_tag_acars{'P', 'A', 'C', 'A'}; constexpr image_tag_t image_tag_adsb_rx{'P', 'A', 'D', 'R'}; constexpr image_tag_t image_tag_afsk_rx{'P', 'A', 'F', 'R'};