From 2a9b19c626db70d212b720a903430f410fe2b122 Mon Sep 17 00:00:00 2001 From: Dan Date: Fri, 6 Sep 2024 10:48:41 -0700 Subject: [PATCH] serial fixes for realtime connection --- VortexEngine/src/Serial/Serial.cpp | 31 +++++++++++++++++++++++++++--- VortexEngine/src/Serial/Serial.h | 4 ++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/VortexEngine/src/Serial/Serial.cpp b/VortexEngine/src/Serial/Serial.cpp index aedcb2143d..a54b8f93c7 100644 --- a/VortexEngine/src/Serial/Serial.cpp +++ b/VortexEngine/src/Serial/Serial.cpp @@ -19,6 +19,7 @@ bool SerialComs::m_serialConnected = false; uint32_t SerialComs::m_lastCheck = 0; +uint32_t SerialComs::m_lastConnected = 0; // init serial bool SerialComs::init() @@ -39,10 +40,28 @@ bool SerialComs::isConnected() m_serialConnected = false; return false; } + if (!isConnectedReal()) { + return false; + } #endif return m_serialConnected; } +bool SerialComs::isConnectedReal() +{ +#ifdef VORTEX_EMBEDDED + uint32_t now = Time::getCurtime(); + if (!Serial.usb.connected()) { + m_lastConnected = now; + } else { + if (m_lastConnected && (now - m_lastConnected) > 1800) { + return false; + } + } +#endif + return true; +} + // check for any serial connection or messages bool SerialComs::checkSerial() { @@ -51,6 +70,9 @@ bool SerialComs::checkSerial() // already connected return true; } + if (m_serialConnected) { + return isConnectedReal(); + } uint32_t now = Time::getCurtime(); // don't check for serial too fast if (m_lastCheck && (now - m_lastCheck) < MAX_SERIAL_CHECK_INTERVAL) { @@ -71,13 +93,16 @@ bool SerialComs::checkSerial() } // Begin serial communications (turns out this is actually a NO-OP in trinket source) Serial.begin(SERIAL_BAUD_RATE); - // directly open the editor connection menu because we are connected to USB serial - Menus::openMenu(MENU_EDITOR_CONNECTION); + if (Menus::curMenuID() != MENU_EDITOR_CONNECTION) { + // directly open the editor connection menu because we are connected to USB serial + Menus::openMenu(MENU_EDITOR_CONNECTION); + } #endif #endif // serial is now connected m_serialConnected = true; - return true; + // rely on the low level 'real' connection now + return isConnectedReal(); } void SerialComs::write(const char *msg, ...) diff --git a/VortexEngine/src/Serial/Serial.h b/VortexEngine/src/Serial/Serial.h index 0121b5bbb0..7ce393361f 100644 --- a/VortexEngine/src/Serial/Serial.h +++ b/VortexEngine/src/Serial/Serial.h @@ -17,6 +17,9 @@ class SerialComs // whether serial is initialized static bool isConnected(); + // why do I need this + static bool isConnectedReal(); + // check for any serial connection or messages static bool checkSerial(); @@ -36,6 +39,7 @@ class SerialComs // whether serial communications are initialized static bool m_serialConnected; static uint32_t m_lastCheck; + static uint32_t m_lastConnected; }; #endif