Skip to content

Commit

Permalink
protect serial asyncmsg with indicate to prevent halt (#2272)
Browse files Browse the repository at this point in the history
  • Loading branch information
zxkmm authored Sep 29, 2024
1 parent b19ed5f commit b43eaa8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
4 changes: 3 additions & 1 deletion firmware/application/usb_serial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class USBSerial {
void on_channel_closed();
void setEventDispatcher(EventDispatcher* ed) { _eventDispatcher = ed; }

bool serial_connected() { return connected; }

private:
void enable_xtal();
void disable_pll0();
Expand All @@ -51,4 +53,4 @@ class USBSerial {
EventDispatcher* _eventDispatcher = NULL;
};

} // namespace portapack
} // namespace portapack
31 changes: 16 additions & 15 deletions firmware/application/usb_serial_asyncmsg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,78 +23,79 @@
*/

#include "usb_serial_asyncmsg.hpp"
#include "usb_serial.hpp"

/// value
// to_string_bin/ to_string_decimal/ to_string_hex/ to_string_hex_array/ to_string_dec_uint/ to_string_dec_int etc seems usellss so i didn't add them here
// usage: UsbSerialAsyncmsg::asyncmsg(num);

template <>
void UsbSerialAsyncmsg::asyncmsg<int64_t>(const int64_t& data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
chprintf((BaseSequentialStream*)&SUSBD1, "%s\r\n", to_string_dec_int(data).c_str());
}

template <>
void UsbSerialAsyncmsg::asyncmsg<int32_t>(const int32_t& data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
chprintf((BaseSequentialStream*)&SUSBD1, "%s\r\n", to_string_dec_int(data).c_str());
}

template <>
void UsbSerialAsyncmsg::asyncmsg<int16_t>(const int16_t& data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
chprintf((BaseSequentialStream*)&SUSBD1, "%s\r\n", to_string_dec_int(data).c_str());
}

template <>
void UsbSerialAsyncmsg::asyncmsg<int8_t>(const int8_t& data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
chprintf((BaseSequentialStream*)&SUSBD1, "%s\r\n", to_string_dec_int(data).c_str());
}

template <>
void UsbSerialAsyncmsg::asyncmsg<uint8_t>(const uint8_t& data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
chprintf((BaseSequentialStream*)&SUSBD1, "%s\r\n", to_string_dec_int(data).c_str());
}

template <>
void UsbSerialAsyncmsg::asyncmsg<uint16_t>(const uint16_t& data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
chprintf((BaseSequentialStream*)&SUSBD1, "%s\r\n", to_string_dec_int(data).c_str());
}

template <>
void UsbSerialAsyncmsg::asyncmsg<uint32_t>(const uint32_t& data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
chprintf((BaseSequentialStream*)&SUSBD1, "%s\r\n", to_string_dec_int(data).c_str());
}

template <>
void UsbSerialAsyncmsg::asyncmsg<uint64_t>(const uint64_t& data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
chprintf((BaseSequentialStream*)&SUSBD1, "%s\r\n", to_string_dec_int(data).c_str());
}

template <>
void UsbSerialAsyncmsg::asyncmsg<float>(const float& data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
chprintf((BaseSequentialStream*)&SUSBD1, "%s\r\n", to_string_decimal(data, 7).c_str());
Expand All @@ -105,7 +106,7 @@ void UsbSerialAsyncmsg::asyncmsg<float>(const float& data) {
template <>
// usage: UsbSerialAsyncmsg::asyncmsg(path);
void UsbSerialAsyncmsg::asyncmsg<std::filesystem::path>(const std::filesystem::path& data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
std::string path_str = data.string();
Expand All @@ -114,7 +115,7 @@ void UsbSerialAsyncmsg::asyncmsg<std::filesystem::path>(const std::filesystem::p

template <>
void UsbSerialAsyncmsg::asyncmsg<std::filesystem::path::string_type>(const std::filesystem::path::string_type& data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
std::string str_data(data.begin(), data.end());
Expand All @@ -127,7 +128,7 @@ void UsbSerialAsyncmsg::asyncmsg<std::filesystem::path::string_type>(const std::
template <>
// usage: UsbSerialAsyncmsg::asyncmsg(str);
void UsbSerialAsyncmsg::asyncmsg<std::string>(const std::string& data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
chprintf((BaseSequentialStream*)&SUSBD1, "%s\r\n", data.c_str());
Expand All @@ -136,7 +137,7 @@ void UsbSerialAsyncmsg::asyncmsg<std::string>(const std::string& data) {
// string literal AKA char[]
// usage: UsbSerialAsyncmsg::asyncmsg("abc");
void UsbSerialAsyncmsg::asyncmsg(const char* data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
chprintf((BaseSequentialStream*)&SUSBD1, "%s\r\n", data);
Expand All @@ -146,8 +147,8 @@ void UsbSerialAsyncmsg::asyncmsg(const char* data) {
template <>
// usage: UsbSerialAsyncmsg::asyncmsg(true);
void UsbSerialAsyncmsg::asyncmsg<bool>(const bool& data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
chprintf((BaseSequentialStream*)&SUSBD1, "%s\r\n", data ? "true" : "false");
}
}
2 changes: 1 addition & 1 deletion firmware/application/usb_serial_asyncmsg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class UsbSerialAsyncmsg {
// ussgae: UsbSerialAsyncmsg::asyncmsg(vec);
template <typename VECTORCOVER>
void UsbSerialAsyncmsg::asyncmsg(const std::vector<VECTORCOVER>& data) {
if (!portapack::async_tx_enabled) {
if (!portapack::async_tx_enabled || !portapack::usb_serial.serial_connected()) {
return;
}
for (const auto& item : data) {
Expand Down

0 comments on commit b43eaa8

Please sign in to comment.