Skip to content

Commit

Permalink
Albatross: write timeout change and minor code refactoring (#15269)
Browse files Browse the repository at this point in the history
Summary of the issue:
Driver used 0 for write_timeout (non-blocking). If display was updated frequently (for example pressing and holding arrow key down in long document), display updating continued some time after for example arrow key was not pressed anymore. Using write_timeout 0.2 seconds (quite typical value with other braille drivers) resolves this.

Description of user facing changes
Braille shows pretty quickly valid information after situations where braille line was updated frequently (for example when pressing and holding arrow key down in long document).

Description of development approach
In addition to write_timeout change, there is a change how display is updated after reconnection and exit from internal menu. Using braille.handler._displayWithCursor function, driver code can be reduced.
  • Loading branch information
burmancomp authored Aug 22, 2023
1 parent 9e8e419 commit 7b6e071
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
4 changes: 2 additions & 2 deletions source/brailleDisplayDrivers/albatross/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@
"""

READ_TIMEOUT = 0.2
WRITE_TIMEOUT = 0
WRITE_TIMEOUT = 0.2
SLEEP_TIMEOUT = 0.2
"""How long to sleep between port init or open retries."""
"""How long to sleep when port cannot be opened or I/O buffers reset fails."""

MAX_INIT_RETRIES = 20
"""
Expand Down
43 changes: 16 additions & 27 deletions source/brailleDisplayDrivers/albatross/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ def __init__(self, port="Auto"):
self._keyLayout: int
# Keep old display data, only changed content is sent.
self._oldCells: List[int] = []
# After reconnection and when user exits from device menu, display may not
# update automatically. Keep current cell content for this.
self._currentCells: List[int] = []
# Set to True when filtering redundant init packets when init byte
# received but not yet settings byte.
self._initByteReceived = False
Expand All @@ -114,14 +111,14 @@ def __init__(self, port="Auto"):
self._exitEvent = Event()
# Thread for read
self._handleRead = None
# Display function is called manually when display is switched back on
# or exited from internal menu. Ensuring data integrity.
# Display function is called indirectly manually when display is switched
# back on or exited from internal menu. Ensuring data integrity.
self._displayLock = Lock()
# For proper write operations because calls may be from different threads
self._writeLock = Lock()
# Timer to keep connection (see KC_INTERVAL).
# Timer to keep connection (see L{KC_INTERVAL}).
self._kc = None
# When previous write was done (see KC_INTERVAL).
# When previous write was done (see L{KC_INTERVAL}).
self._writeTime = 0.0
self._searchPorts(port)

Expand Down Expand Up @@ -565,18 +562,13 @@ def _handleInitPackets(self, data: bytes):
return
self._handleSettingsByte(data)
# Reconnected or exited device menu if length of _oldCells is numCells.
# Also checking that _currentCells has sometimes updated.
# Show last known content.
if (
len(self._oldCells) == self.numCells
and len(self._oldCells) == len(self._currentCells)
):
if len(self._oldCells) == self.numCells:
# Ensure display is updated after reconnection and exit from internal menu.
self._clearOldCells()
self.display(self._currentCells)
if not self._tryToConnect:
log.debug(
"Updated display content after reconnection or display menu exit"
)
braille.handler._displayWithCursor()
log.debug(
"Updated display content after reconnection or display menu exit"
)
if self._waitingSettingsByte:
self._waitingSettingsByte = False

Expand Down Expand Up @@ -736,16 +728,13 @@ def display(self, cells: List[int]):
"""Prepare cell content for display.
@param cells: List of cells content
"""
# Using lock because called also manually when display is switched back on
# or exited from internal menu.
# No connection
if self._tryToConnect or self._disabledConnection:
log.debug("returning, no connection")
return
# Using lock because called also indirectly manually when display is
# switched back on or exited from internal menu.
with self._displayLock:
# Keep _currentCells up to date for reconnection regardless
# of connection state of driver.
self._currentCells = cells.copy()
# No connection
if self._tryToConnect or self._disabledConnection:
log.debug("returning, no connection")
return
writeBytes: List[bytes] = [START_BYTE, ]
# Only changed content is sent (cell index and data).
for i, cell in enumerate(cells):
Expand Down

0 comments on commit 7b6e071

Please sign in to comment.