diff --git a/firmware/application/CMakeLists.txt b/firmware/application/CMakeLists.txt index 90922617..79fb816d 100644 --- a/firmware/application/CMakeLists.txt +++ b/firmware/application/CMakeLists.txt @@ -204,6 +204,7 @@ set(CPPSRC portapack.cpp usb_serial_shell.cpp usb_serial_event.cpp + usb_serial_thread.cpp usb_serial.cpp qrcodegen.cpp radio.cpp diff --git a/firmware/application/apps/ble_rx_app.cpp b/firmware/application/apps/ble_rx_app.cpp index 6be25a49..b5a27faf 100644 --- a/firmware/application/apps/ble_rx_app.cpp +++ b/firmware/application/apps/ble_rx_app.cpp @@ -436,6 +436,8 @@ BLERxView::BLERxView(NavigationView& nav) nav_.push(entry); }; + usb_serial_thread = std::make_unique(); + ensure_directory(find_packet_path); ensure_directory(log_packets_path); ensure_directory(packet_save_path); @@ -684,24 +686,16 @@ bool BLERxView::saveFile(const std::filesystem::path& path) { } void BLERxView::on_data(BlePacketData* packet) { - std::string str_console = ""; - if (!logging) { str_log = ""; } str_console += pdu_type_to_string((ADV_PDU_TYPE)packet->type); - str_console += " Len:"; str_console += to_string_dec_uint(packet->size); - - str_console += "\n"; - - str_console += "Mac:"; + str_console += " Mac:"; str_console += to_string_mac_address(packet->macAddress, 6, false); - - str_console += "\n"; - str_console += "Data:"; + str_console += " Data:"; int i; @@ -709,8 +703,6 @@ void BLERxView::on_data(BlePacketData* packet) { str_console += to_string_hex(packet->data[i], 2); } - str_console += "\n"; - uint64_t macAddressEncoded = copy_mac_address_to_uint64(packet->macAddress); // Start of Packet stuffing. @@ -728,9 +720,13 @@ void BLERxView::on_data(BlePacketData* packet) { // Log at End of Packet. if (logger && logging) { - logger->log_raw_data(str_console); + logger->log_raw_data(str_console + "\r\n"); } + usb_serial_thread->serial_str = str_console + "\r\n"; + usb_serial_thread->str_ready = true; + str_console = ""; + if (!searchList.empty()) { auto it = searchList.begin(); diff --git a/firmware/application/apps/ble_rx_app.hpp b/firmware/application/apps/ble_rx_app.hpp index 3e84b2d8..8735c545 100644 --- a/firmware/application/apps/ble_rx_app.hpp +++ b/firmware/application/apps/ble_rx_app.hpp @@ -35,6 +35,7 @@ #include "radio_state.hpp" #include "log_file.hpp" #include "utility.hpp" +#include "usb_serial_thread.hpp" #include "recent_entries.hpp" @@ -194,6 +195,7 @@ class BLERxView : public View { std::string build_line_str(BleRecentEntry entry); void on_save_file(const std::string value); bool saveFile(const std::filesystem::path& path); + std::unique_ptr usb_serial_thread{}; void on_data(BlePacketData* packetData); void on_filter_change(std::string value); void on_file_changed(const std::filesystem::path& new_file_path); @@ -213,6 +215,7 @@ class BLERxView : public View { uint8_t sort_index{0}; std::string filter{}; bool logging{false}; + bool name_enable{true}; app_settings::SettingsManager settings_{ "rx_ble", @@ -225,6 +228,7 @@ class BLERxView : public View { {"name"sv, &name_enable}, }}; + std::string str_console = ""; uint8_t console_color{0}; uint32_t prev_value{0}; uint8_t channel_number = 37; diff --git a/firmware/application/usb_serial_thread.cpp b/firmware/application/usb_serial_thread.cpp new file mode 100644 index 00000000..e2ab2ebd --- /dev/null +++ b/firmware/application/usb_serial_thread.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2016 Jared Boone, ShareBrained Technology, Inc. + * + * 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 "usb_serial_thread.hpp" +#include "buffer_exchange.hpp" + +// UsbSerialThread ////////////////////////////////////////////////////////// + +UsbSerialThread::UsbSerialThread() { + create_thread(); +} + +UsbSerialThread::~UsbSerialThread() { + stop(); +} + +void UsbSerialThread::create_thread() { + thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, UsbSerialThread::static_fn, this); +} + +void UsbSerialThread::stop() { + if (thread) { + chThdTerminate(thread); + chThdWait(thread); + thread = nullptr; + } +} + +msg_t UsbSerialThread::static_fn(void* arg) { + auto obj = static_cast(arg); + obj->run(); + return 0; +} + +void UsbSerialThread::run() { + while (!chThdShouldTerminate()) { + if (str_ready) { + str_ready = false; + chprintf((BaseSequentialStream*)&SUSBD1, serial_str.c_str()); + } + + chThdSleepMilliseconds(50); + } +} diff --git a/firmware/application/usb_serial_thread.hpp b/firmware/application/usb_serial_thread.hpp new file mode 100644 index 00000000..252bbafe --- /dev/null +++ b/firmware/application/usb_serial_thread.hpp @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2016 Jared Boone, ShareBrained Technology, Inc. + * + * 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 __USB_SERIAL_THREAD_H__ +#define __USB_SERIAL_THREAD_H__ + +#include "ch.h" + +#include "event_m0.hpp" + +#include "io.hpp" +#include "optional.hpp" + +#include "chprintf.h" +#include "usb_serial_io.h" + +#include +#include +#include + +class UsbSerialThread { + public: + UsbSerialThread(); + ~UsbSerialThread(); + + void stop(); + + UsbSerialThread(const UsbSerialThread&) = delete; + UsbSerialThread(UsbSerialThread&&) = delete; + UsbSerialThread& operator=(const UsbSerialThread&) = delete; + UsbSerialThread& operator=(UsbSerialThread&&) = delete; + bool str_ready = false; + std::string serial_str{}; + + private: + Thread* thread{nullptr}; + static msg_t static_fn(void* arg); + void run(); + void create_thread(); +}; + +#endif /*__CAPTURE_THREAD_H__*/ diff --git a/firmware/common/ui_widget.cpp b/firmware/common/ui_widget.cpp index 04b97e5e..675fdb9d 100644 --- a/firmware/common/ui_widget.cpp +++ b/firmware/common/ui_widget.cpp @@ -28,8 +28,10 @@ #include #include +#include "chprintf.h" #include "irq_controls.hpp" #include "string_format.hpp" +#include "usb_serial_io.h" using namespace portapack;