From 7d4390accc42e2b6f6efcd31d9c329c179beae6e Mon Sep 17 00:00:00 2001 From: todd Date: Wed, 15 May 2024 17:20:58 +0100 Subject: [PATCH] BF: Wait .1ms and stitch unfinished messages together rather than waiting for full BBTK message --- psychopy_bbtk/tpad.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/psychopy_bbtk/tpad.py b/psychopy_bbtk/tpad.py index 0c0845b..4178147 100644 --- a/psychopy_bbtk/tpad.py +++ b/psychopy_bbtk/tpad.py @@ -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), @@ -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 @@ -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 ): @@ -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 @@ -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):