Skip to content

Commit

Permalink
Fix end-of-line sounds in simulation
Browse files Browse the repository at this point in the history
In order to let the UI play the end-of-line sounds, we
need the communications mock to not emit reqLine messages
back-to-back.
  • Loading branch information
jonathanperret committed Dec 13, 2024
1 parent b234a81 commit 3286f99
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
53 changes: 30 additions & 23 deletions src/main/python/main/ayab/engine/communication_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import logging
from time import sleep
from collections import deque

from PySide6.QtWidgets import QMessageBox

Expand All @@ -47,8 +46,9 @@ def __del__(self) -> None:
def reset(self):
self.__is_open = False
self.__is_started = False
self.rx_msg_list = deque([], maxlen=100)
self.rx_msg_list = list()
self.__line_count = 0
self.__started_row = False

def is_open(self) -> bool:
"""Return status of the interface."""
Expand Down Expand Up @@ -109,29 +109,36 @@ def cnf_line_API6(self, line_number, color, flags, line_data) -> bool:
"""Send a row of stitch data."""
return True

def update_API6(self) -> tuple[bytes, Token, int]:
def update_API6(self) -> tuple[bytes | None, Token, int]:
"""Read and parse data packet."""
if self.__is_open and self.__is_started:
reqLine = bytes([Token.reqLine.value, self.__line_count])
self.__line_count += 1
self.__line_count %= 256
self.rx_msg_list.append(reqLine)
if self.__delay:
sleep(1) # wait for knitting progress dialog to update
# step through output line by line
if self.__step:
# pop up box waits for user input before moving on to next line
msg = QMessageBox()
msg.setIcon(QMessageBox.Icon.Information)
msg.setText("Line number = " + str(self.__line_count))
msg.setStandardButtons(
QMessageBox.StandardButton.Ok | QMessageBox.StandardButton.Cancel
)
ret = None
ret = msg.exec_()
while ret is None:
pass
# Alternate between reqLine and no message
# (so that the UI makes the end-of-line sound for each row)
if self.__started_row:
self.__started_row = False
reqLine = bytes([Token.reqLine.value, self.__line_count])
self.__line_count += 1
self.__line_count %= 256
self.rx_msg_list.append(reqLine)
if self.__delay:
sleep(1) # wait for knitting progress dialog to update
# step through output line by line
if self.__step:
# pop up box waits for user input before moving on to next line
msg = QMessageBox()
msg.setIcon(QMessageBox.Icon.Information)
msg.setText("Line number = " + str(self.__line_count))
msg.setStandardButtons(
QMessageBox.StandardButton.Ok
| QMessageBox.StandardButton.Cancel
)
ret = None
ret = msg.exec_()
while ret is None:
pass
else:
self.__started_row = True
if len(self.rx_msg_list) > 0:
return self.parse_API6(self.rx_msg_list.popleft()) # FIFO
return self.parse_API6(self.rx_msg_list.pop(0)) # FIFO
# else
return self.parse_API6(None)
3 changes: 3 additions & 0 deletions src/main/python/main/ayab/tests/test_communication_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ def test_req_line_API6(self):
)
self.comm_dummy.update_API6() # cnfStart

# Alternates between requesting a line and no output
for i in range(0, 256):
bytes_read = self.comm_dummy.update_API6()
assert bytes_read == (bytearray([Token.reqLine.value, i]), Token.reqLine, i)
bytes_read = self.comm_dummy.update_API6()
assert bytes_read == (None, Token.none, 0)

0 comments on commit 3286f99

Please sign in to comment.