Skip to content

Commit

Permalink
serial fixes for realtime connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Unreal-Dan committed Sep 6, 2024
1 parent 31d9153 commit 8946905
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
31 changes: 28 additions & 3 deletions VortexEngine/src/Serial/Serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
{
Expand All @@ -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) {
Expand All @@ -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, ...)
Expand Down
4 changes: 4 additions & 0 deletions VortexEngine/src/Serial/Serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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

0 comments on commit 8946905

Please sign in to comment.