buba/buba/bubacmd.py

108 lines
3.5 KiB
Python

"""
Commands to send to the display. Commands are always sent to the websocket, so emulators can display the data. If a serial port is available, send the commands there as well.
"""
import logging
from os import path
from typing import Callable
from pyfis.aegmis import MIS1TextDisplay
class BubaCmd:
ALIGN_LEFT = 0x00
ALIGN_RIGHT = 0x01
ALIGN_CENTER = 0x02
ALIGN_SCROLL = 0x03 # apparently not supported
def __init__(self, serial: str, send: Callable):
self.log = logging.getLogger(__name__)
self.serial = serial
self.display = None
if not path.exists(serial):
self.serial = None
self.log.warning(f"Unable to find serial port '{serial}', ignoring")
if self.serial is not None:
self.display = MIS1TextDisplay(serial)
self.send = send
def simple_text(self, page, row, col, text, align=MIS1TextDisplay.ALIGN_LEFT):
"""
Send text to the specified row.
The remainder of the row is cleared. This mirrors MIS1TextDisplay.simple_text
:param self:
:param page: which page to write the text to
:param row: which row to write the text to
:param col: in which column to start
:param text: to write
:param align: alignment options, see MIS1TextDisplay.ALIGN_*
:return:
"""
text = text.encode("CP437", "replace").decode("CP437") # force text to be CP437 compliant
if self.display is not None:
self.display.simple_text(page, row, col, text, align)
self.send({
'cmd': 'simple_text',
'page': page,
'row': row,
'col': col,
'text': text,
'align': align,
})
def text(self, page, row, col_start, col_end, text, align=MIS1TextDisplay.ALIGN_LEFT):
"""
Send text to the specified row, placing it between col_start and col_end.
This mirrors MIS1TextDisplay.text
:param self:
:param page: which page to write the text to
:param row: which row to write the text to
:param col_start: starting column
:param col_end: ending column
:param text: to write
:param align: alignment options, see MIS1TextDisplay.ALIGN_*
:return:
"""
text = text.encode("CP437", "replace").decode("CP437") # force text to be CP437 compliant
if self.display is not None:
self.display.text(page, row, col_start, col_end, text, align)
self.send({
'cmd': 'text',
'page': page,
'row': row,
'col_start': col_start,
'col_end': col_end,
'text': text,
'align': align,
})
def set_page(self, page):
"""
Display the given page.
:param self:
:param page: page number to display
:return:
"""
return self.set_pages([(page, 255)])
def set_pages(self, pages):
"""
Configure automatic paging.
You can specify up to 10 tuples of (pagenumber, delay).
The display will automatically step through the list and show each page number for
the given time. After all entries have been shown, the process starts over.
:param self:
:param pages:
:return:
"""
if self.display is not None:
self.display.set_pages(pages)
self.send({
'cmd': 'set_pages',
'pages': pages,
})