diff --git a/display.py b/display.py index 7aa4352..4dd7a60 100644 --- a/display.py +++ b/display.py @@ -29,6 +29,8 @@ TOUCH_MODE = 0x51 SAVE_ATTRS = 0xa0 SEL_ATTRS = 0xc0 +SET_STARTUP = 0xf2 + IDENT = 0xf3 FONT_GEO = [(6, 8), (8, 16), (20, 40)] @@ -47,45 +49,62 @@ class Display: fg = 15 bg = 0 touch = 0 - thread = None bar = None - blinkchar = ' ' + blink = False menu = None + storage = None - def __init__(self, dev='/dev/ttyS1', timeout=0.5): + def __init__(self, dev, timeout=0.5, daemon=False): self.event = threading.Event() self.term = serial.Serial(dev, baudrate=115200, timeout=timeout) + if not daemon: + self.storage = bytearray() self.gethost(False) self.reset() + if daemon: + threading.Thread(target=self.gethostthread, daemon=True).start() + self.event.wait(1) # wait for thread to be started + self.refresh() def reset(self): self.send(MODE_GRAPH) self.font(2) self.send(CLEAR, self.bg) - def start(self): - if self.thread is None or not self.thread.is_alive(): - self.thread = threading.Thread(target=self.gethostthread) - self.thread.daemon = True - self.thread.start() - self.event.wait(1) # wait for thread to be started - print('refresh') - self.refresh() - def color(self, fg=15, bg=0): self.fg = fg self.bg = bg + self.send(SET_COLOR, bg, bg, fg, fg) def send(self, code, *args): - out = [code] + out = bytearray([code]) for arg in args: - if isinstance(arg, bytes): - out.extend(list(arg)) - elif isinstance(arg, str): - out.append(arg.encode('latin-1')) + if isinstance(arg, str): + out.extend(arg.encode('latin-1')) + elif hasattr(arg, '__len__'): + out.extend(arg) else: out.append(arg) - self.term.write(bytes([ESC,ESC,len(out)] + out)) + cmd = bytearray([ESC,ESC,len(out)]) + out + if self.storage is not None: + self.storage.extend(cmd) + self.term.write(cmd) + + def set_startup(self): + if self.storage is None: + print('storage off') + return + data = self.storage + self.storage = None + self.send(SET_STARTUP, data) + + def standard_startup(self): + self.storage = bytearray() + self.reset() + with open('/sys/class/net/enp1s0/address') as f: + netaddr = f.read().strip().lower() + self.show('startup ...', netaddr) + self.set_startup() def version(self): self.term.write(bytes([ESC,ESC,1,0xf3])) @@ -100,7 +119,7 @@ class Display: self.nrows = HEIGHT // self.rowhei self.ncols = WIDTH // self.colwid self.textbuffer = [" " * self.ncols] * self.nrows - self.send(SET_COLOR, self.bg, self.bg, self.fg, self.fg) + self.color() def text(self, text, row=0, left=0, right=None, font=2): if font != self.fontsize: @@ -132,10 +151,13 @@ class Display: self.hostip = s.getsockname()[0] except Exception as e: self.hostip = '' + hostname = None if self.hostip and truehostname: - self.hostname = socket.gethostbyaddr(self.hostip)[0] - else: - self.hostname = socket.gethostname() + try: + hostname = socket.gethostbyaddr(self.hostip)[0] + except Exception: + pass + self.hostname = hostname or socket.gethostname() def gethostthread(self): self.gethost(True) @@ -208,11 +230,17 @@ class Display: def net_display(self, x): if x is None: - hostip = self.hostip or 'no network.' - self.text(hostip.replace('.', self.blinkchar, 1), 0, 0, 15) + hostip = self.hostip or 'no network' + dotpos = hostip.find('.') + if dotpos < 0: + dotpos = len(hostip) + self.text(hostip.replace('.', ' ', 1), 0, 0, 15) + self.send(SET_COLOR, 0, 0, 0, 11 + self.blink) # yellow / blue + self.text('.', 0, dotpos, dotpos+1) + self.color() self.text(self.hostname, 1) self.event.set() - self.blinkchar = ' ' if self.blinkchar == '.' else '.' + self.blink = not self.blink else: self.menu = self.menu_main self.show('(cancel)(reboot)(shdown)') @@ -225,10 +253,17 @@ class Display: self.send(0xf0, 0xcb, 0xef, 0x20, 0x18) -d = Display() +tty = '/dev/ttyS1' +daemon = False -if len(sys.argv) == 1: - d.start() +for arg in sys.argv[1:]: + if arg == '-d': + daemon = True + else: + tty = arg + +d = Display(tty, daemon=daemon) +if daemon: while True: d.refresh() diff --git a/install.py b/install.py index 99c056a..7e06ab6 100755 --- a/install.py +++ b/install.py @@ -85,7 +85,7 @@ After = network.target [Service] User = root -ExecStart = /usr/bin/python3 /root/aputools/display.py +ExecStart = /usr/bin/python3 /root/aputools/display.py -d [Install] WantedBy = multi-user.target