Skip to content

Commit 90d9f97

Browse files
authored
Add a delay to account for the disconnection/reconnection with matrix device (#178)
1 parent 16f50ef commit 90d9f97

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

buildhat/serinterface.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,17 @@ def __init__(self, firmware, signature, version, device="/dev/serial0", debug=Fa
151151
self.cb.start()
152152

153153
# Drop timeout value to 1s
154+
listevt = threading.Event()
154155
self.ser.timeout = 1
155-
self.th = threading.Thread(target=self.loop, args=(self.cond, self.state == HatState.FIRMWARE, self.cbqueue))
156+
self.th = threading.Thread(target=self.loop, args=(self.cond, self.state == HatState.FIRMWARE, self.cbqueue, listevt))
156157
self.th.daemon = True
157158
self.th.start()
158159

159160
if self.state == HatState.FIRMWARE:
160161
self.write(b"port 0 ; select ; port 1 ; select ; port 2 ; select ; port 3 ; select ; echo 0\r")
162+
time.sleep(3.5)
161163
self.write(b"list\r")
164+
listevt.set()
162165
elif self.state == HatState.NEEDNEWFIRMWARE or self.state == HatState.BOOTLOADER:
163166
self.write(b"reboot\r")
164167

@@ -292,7 +295,7 @@ def callbackloop(self, q):
292295
cb[0]()(cb[1])
293296
q.task_done()
294297

295-
def loop(self, cond, uselist, q):
298+
def loop(self, cond, uselist, q, listevt):
296299
"""Event handling for Build HAT
297300
298301
:param cond: Condition used to block user's script till we're ready
@@ -312,20 +315,20 @@ def loop(self, cond, uselist, q):
312315
self.connections[portid].update(typeid, True)
313316
if typeid == 64:
314317
self.write(f"port {portid} ; on\r".encode())
315-
if uselist:
318+
if uselist and listevt.is_set():
316319
count += 1
317320
elif cmp(msg, BuildHAT.CONNECTEDPASSIVE):
318321
typeid = int(line[2 + len(BuildHAT.CONNECTEDPASSIVE):], 16)
319322
self.connections[portid].update(typeid, True)
320-
if uselist:
323+
if uselist and listevt.is_set():
321324
count += 1
322325
elif cmp(msg, BuildHAT.DISCONNECTED):
323326
self.connections[portid].update(-1, False)
324327
elif cmp(msg, BuildHAT.DEVTIMEOUT):
325328
self.connections[portid].update(-1, False)
326329
elif cmp(msg, BuildHAT.NOTCONNECTED):
327330
self.connections[portid].update(-1, False)
328-
if uselist:
331+
if uselist and listevt.is_set():
329332
count += 1
330333
elif cmp(msg, BuildHAT.RAMPDONE):
331334
with self.rampcond[portid]:

0 commit comments

Comments
 (0)