Skip to content

Instantly share code, notes, and snippets.

@rbarrois
Created September 7, 2014 22:22
Show Gist options
  • Save rbarrois/c8cdf7e688b2a6764a31 to your computer and use it in GitHub Desktop.
Save rbarrois/c8cdf7e688b2a6764a31 to your computer and use it in GitHub Desktop.
Small test tool for mpdlcd / LCDd charset
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
import socket
import sys
DEFAULT_LCDD_CHARSET = 'utf-8'
class Runner(object):
def __init__(self, host, port, charset=DEFAULT_LCDD_CHARSET):
self.sock = socket.socket()
self.host = host
self.port = int(port)
self.charset = charset
def _send(self, txt):
raw = txt.encode(self.charset)
self.sock.sendall(raw)
def writeout(self, txt):
sys.stdout.write(txt + "\n")
def run(self):
self.setup()
try:
self.loop()
finally:
self.clean()
def setup(self):
self.sock.connect((self.host, self.port))
self._send("hello\n")
self._send("screen_add tst\n")
self._send("widget_add tst tst_txt string\n")
def clean(self):
self._send("screen_del tst\n")
self.sock.close()
def loop(self):
while True:
txt = raw_input("Type some text (empty to quit):\n")
if not txt:
break
txt = txt.decode('utf-8')
self.writeout("Displaying %s (%r)" % (txt, txt))
try:
self._send("widget_set tst tst_txt 1 1 %s\n" % txt)
except UnicodeEncodeError as e:
self.writeout("Encoding error: unable to send %s: %s" % (txt, e))
if __name__ == '__main__':
host, port = sys.argv[1:3]
if len(sys.argv) >= 4:
charset = sys.argv[3]
else:
charset = DEFAULT_LCDD_CHARSET
print("Using LCDd charset %s" % charset)
runner = Runner(host, port, charset=charset)
runner.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment