Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Add method to query whether there's an unfinished message #9

Merged
merged 1 commit into from
May 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions psychopy_bbtk/tpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ def dispatchMessages(self):
True if request sent successfully
"""
self.parent.dispatchMessages()

def hasUnfinishedMessage(self):
"""
Is the parent TPad waiting for an end-of-line character?

Returns
-------
bool
True if there is a partial message waiting for an end-of-line
"""
return self.parent.hasUnfinishedMessage()

def parseMessage(self, message):
# if given a string, split according to regex
Expand Down Expand Up @@ -225,6 +236,17 @@ def dispatchMessages(self):
True if request sent successfully
"""
self.parent.dispatchMessages()

def hasUnfinishedMessage(self):
"""
Is the parent TPad waiting for an end-of-line character?

Returns
-------
bool
True if there is a partial message waiting for an end-of-line
"""
return self.parent.hasUnfinishedMessage()

def parseMessage(self, message):
# if given a string, split according to regex
Expand Down Expand Up @@ -450,6 +472,31 @@ def dispatchMessages(self):
logging.debug(f"Received unparsable message from TPad: {line}")
# mark that a dispatch has finished
self._dispatchInProgress = False

def hasUnfinishedMessage(self):
"""
We don't wait for an end-of-line from the TPad device before continuing, as
dispatchMessages is often called in a frame loop so waiting for messages would
ruin the frame rate. If just the start of a message has been received, this
function will return True.

Usage
-----
If you want to be certain that all events have been dispatched, you can do:
```
timeout = Clock()
while myTPad.hasUnfinishedMessage() and timeout.getTime() < 0.1:
myTPad.dispatchMessages()
```
(the purpose of the `timeout` clock is to avoid getting into an infinite loop
if the TPad sends anything unexpected)

Returns
-------
bool
True if there is a partial message waiting for an end-of-line
"""
return bool(self._lastMessage)

@staticmethod
def _detectComPort():
Expand Down
Loading