Skip to content

Commit

Permalink
BF: Wait .1ms and stitch unfinished messages together rather than wai…
Browse files Browse the repository at this point in the history
…ting for full BBTK message
  • Loading branch information
todd committed May 15, 2024
1 parent c930fff commit 7d4390a
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions psychopy_bbtk/tpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

# define format for messages
messageFormat = (
r"([{channels}]) ([{states}]) ([{buttons}]) (\d\d*)"
r"([{channels}]) ([{states}]) ([{buttons}]) (\d\d*)\r\n"
).format(
channels="".join(re.escape(key) for key in channelCodes),
states="".join(re.escape(key) for key in stateCodes),
Expand Down Expand Up @@ -285,7 +285,7 @@ def resetTimer(self, clock=logging.defaultClock):
self.parent.resetTimer(clock=clock)


class TPadVoicekey:
class TPadVoiceKey:
def __init__(self, *args, **kwargs):
pass

Expand All @@ -295,7 +295,7 @@ def __init__(
self, port=None, baudrate=115200,
byteSize=8, stopBits=1,
parity="N", # 'N'one, 'E'ven, 'O'dd, 'M'ask,
eol=b"\n",
eol=b"\r\n",
maxAttempts=1, pauseDuration=1/1000,
checkAwake=True
):
Expand All @@ -309,6 +309,8 @@ def __init__(
# indicator that a message dispatch is currently in progress (prevents threaded
# dispatch loops from tripping over one another)
self._dispatchInProgress = False
# attribute to store last line in case of splicing
self._lastLine = ""
# nodes
self.nodes = []
# attribute to keep track of mode state
Expand Down Expand Up @@ -387,9 +389,18 @@ def dispatchMessages(self):
# mark that a dispatch has begun
self._dispatchInProgress = True
# get data from box
self.pause()
data = self.getResponse(length=2)
self.pause()
data = self.getResponse(length=-1, timeout=1/10000)
# handle line splicing
if data:
# split into lines
data = data.splitlines(keepends=True)
# prepend last unfinished line to first line of this dispatch
data[0] = self._lastLine + data[0]
# if last line wasn't finished, store it for next dispatch
if not data[-1].endswith("\r\n"):
self._lastLine = data.pop(-1)
else:
self._lastLine = ""
# parse lines
for line in data:
if re.match(messageFormat, line):
Expand Down

0 comments on commit 7d4390a

Please sign in to comment.