|
|
@@ -3,15 +3,18 @@
|
|
|
radio-ui sim [--host radio.lo] window with the simulated panel
|
|
|
radio-ui i2c --bus N [--host H] real panel through i2c-dev
|
|
|
radio-ui drm [--host H] real panel through the DRM driver
|
|
|
+ radio-ui keytest --bus N hardware test: panel shows the keys
|
|
|
|
|
|
-i2c and drm read keys from evdev; with --window-keys (test mode) they read the
|
|
|
-PC keyboard through a window on $DISPLAY instead.
|
|
|
+i2c reads the keys from the U2 expander on its bus (0x76). drm reads the evdev
|
|
|
+device radio-keys if there is one, otherwise U2 on the panel's own bus. With
|
|
|
+--window-keys (test mode) they also take the PC keyboard through a window on
|
|
|
+$DISPLAY.
|
|
|
"""
|
|
|
import argparse
|
|
|
import os
|
|
|
import sys
|
|
|
|
|
|
-from . import platform
|
|
|
+from . import keypad, platform
|
|
|
|
|
|
|
|
|
def _int(text):
|
|
|
@@ -44,6 +47,13 @@ def _add_keys(p, backlight_help):
|
|
|
p.add_argument("--backlight", metavar="PATH", help=backlight_help)
|
|
|
|
|
|
|
|
|
+def _add_i2c_keys(p):
|
|
|
+ p.add_argument("--keys-addr", type=_int, default=keypad.DEFAULT_ADDR,
|
|
|
+ help="key expander address (0x%02x)" % keypad.DEFAULT_ADDR)
|
|
|
+ p.add_argument("--keys-poll", type=int, default=keypad.POLL_MS, metavar="MS",
|
|
|
+ help="key polling period (%(default)s ms)")
|
|
|
+
|
|
|
+
|
|
|
def parse_args(argv):
|
|
|
ap = argparse.ArgumentParser(prog="radio-ui", description="Radio front panel UI")
|
|
|
sub = ap.add_subparsers(dest="frontend", metavar="FRONTEND")
|
|
|
@@ -73,10 +83,40 @@ def parse_args(argv):
|
|
|
p.add_argument("--pattern", choices=("geometry", "gray"),
|
|
|
help="show a test pattern and exit")
|
|
|
_add_keys(p, "backlight sysfs brightness file (default: expander pin P1.0)")
|
|
|
+ _add_i2c_keys(p)
|
|
|
+ p.add_argument("--no-i2c-keys", action="store_true",
|
|
|
+ help="do not read the key expander; use evdev instead")
|
|
|
|
|
|
p = sub.add_parser("drm", help="real panel through the s1d13700-tca DRM driver")
|
|
|
_add_common(p, "localhost")
|
|
|
_add_keys(p, "backlight sysfs brightness file (%s)" % platform.DEFAULT_BACKLIGHT)
|
|
|
+ p.add_argument("--keys-bus", type=int, metavar="N",
|
|
|
+ help="read the key expander on /dev/i2c-N (default: the "
|
|
|
+ "panel's bus, unless the evdev device %s exists)"
|
|
|
+ % platform.DEFAULT_KEYS_NAME)
|
|
|
+ p.add_argument("--no-i2c-keys", action="store_true",
|
|
|
+ help="never read the key expander; evdev only")
|
|
|
+ p.add_argument("--allow-adapter", metavar="NAME",
|
|
|
+ help="accept the key bus adapter with this exact name")
|
|
|
+ p.add_argument("--pattern", choices=("geometry", "gray"),
|
|
|
+ help="show a test pattern and exit")
|
|
|
+ _add_i2c_keys(p)
|
|
|
+
|
|
|
+ p = sub.add_parser("keytest", help="hardware test: the panel shows the key expander")
|
|
|
+ _add_geometry(p)
|
|
|
+ p.add_argument("--bus", type=int, required=True, help="I2C bus number")
|
|
|
+ p.add_argument("--addr", type=_int, default=0x74, help="panel expander address (0x74)")
|
|
|
+ p.add_argument("--allow-adapter", metavar="NAME",
|
|
|
+ help="accept the adapter with this exact name")
|
|
|
+ p.add_argument("--no-panel", action="store_true",
|
|
|
+ help="keys only, printed to stdout; leave the panel alone")
|
|
|
+ p.add_argument("--init-only", action="store_true",
|
|
|
+ help="set up the panel (display and backlight on, key "
|
|
|
+ "screen shown) and the key expander, then exit leaving "
|
|
|
+ "them on")
|
|
|
+ p.add_argument("--exit-after", type=int, default=0, metavar="MS",
|
|
|
+ help="quit after this many ms (default: on Ctrl-C)")
|
|
|
+ _add_i2c_keys(p)
|
|
|
|
|
|
args = ap.parse_args(argv)
|
|
|
if getattr(args, "tx_cap", 7) < 7 or getattr(args, "tx_cap", 0) > 1025:
|
|
|
@@ -124,16 +164,30 @@ def make_pattern(name, w, h):
|
|
|
|
|
|
# ---- frontends -------------------------------------------------------------------
|
|
|
|
|
|
-def _start_keys(args, app, loop):
|
|
|
+def _start_keys(args, app, loop, i2c_fd=None):
|
|
|
+ """Key sources: the test mode window, the I2C expander (when i2c_fd is
|
|
|
+ given), an evdev device named by --input, or else the radio-keys one."""
|
|
|
+ found = False
|
|
|
if args.window_keys:
|
|
|
from . import gtkkeys
|
|
|
gtkkeys.require_display()
|
|
|
gtkkeys.KeyWindow(app.key, loop.quit)
|
|
|
print("test mode: keys from the window on %s" % os.environ.get("DISPLAY", "?"))
|
|
|
- if args.input and not platform.start_evdev_keys(app.key, args.input):
|
|
|
+ found = True
|
|
|
+ if i2c_fd is not None:
|
|
|
+ if keypad.start(i2c_fd, args.keys_addr, app.key, args.keys_poll):
|
|
|
+ print("keys: expander at 0x%02x, polled every %d ms"
|
|
|
+ % (args.keys_addr, args.keys_poll))
|
|
|
+ found = True
|
|
|
+ if args.input:
|
|
|
+ if platform.start_evdev_keys(app.key, args.input):
|
|
|
+ found = True
|
|
|
+ else:
|
|
|
print("cannot read %s" % args.input, file=sys.stderr)
|
|
|
- elif not platform.start_evdev_keys(app.key, args.input):
|
|
|
- print("no key input device, running display-only", file=sys.stderr)
|
|
|
+ elif not found:
|
|
|
+ found = platform.start_evdev_keys(app.key)
|
|
|
+ if not found:
|
|
|
+ print("no key input, running display-only", file=sys.stderr)
|
|
|
|
|
|
|
|
|
def run_i2c(args):
|
|
|
@@ -187,7 +241,7 @@ def run_i2c(args):
|
|
|
|
|
|
loop = GLib.MainLoop()
|
|
|
app = App(args.host, args.port, args.width, args.height, present, backlight, args.kmsg)
|
|
|
- _start_keys(args, app, loop)
|
|
|
+ _start_keys(args, app, loop, None if args.no_i2c_keys else fd)
|
|
|
platform.run(loop)
|
|
|
app.close()
|
|
|
try:
|
|
|
@@ -210,6 +264,26 @@ def run_drm(args):
|
|
|
return 1
|
|
|
blpath = args.backlight or platform.DEFAULT_BACKLIGHT
|
|
|
|
|
|
+ if args.pattern:
|
|
|
+ dev.present(make_pattern(args.pattern, dev.width, dev.height), dev.width * 4,
|
|
|
+ 0, 0, dev.width, dev.height)
|
|
|
+ platform.backlight_set(blpath, True)
|
|
|
+ print("pattern shown on card%d" % dev.card)
|
|
|
+ return 0
|
|
|
+
|
|
|
+ keys_bus = args.keys_bus
|
|
|
+ if keys_bus is None and not args.no_i2c_keys and not args.input \
|
|
|
+ and not platform.have_keys_device():
|
|
|
+ keys_bus = dev.i2c_bus()
|
|
|
+ keys_fd = None
|
|
|
+ if keys_bus is not None:
|
|
|
+ try:
|
|
|
+ keys_fd = platform.open_i2c_bus(keys_bus, args.allow_adapter)
|
|
|
+ except platform.BusRefused as e:
|
|
|
+ print(e, file=sys.stderr)
|
|
|
+ if args.keys_bus is not None:
|
|
|
+ return 1
|
|
|
+
|
|
|
def present(*a):
|
|
|
try:
|
|
|
dev.present(*a)
|
|
|
@@ -219,12 +293,86 @@ def run_drm(args):
|
|
|
loop = GLib.MainLoop()
|
|
|
app = App(args.host, args.port, dev.width, dev.height, present,
|
|
|
lambda on: platform.backlight_set(blpath, on), args.kmsg)
|
|
|
- _start_keys(args, app, loop)
|
|
|
+ _start_keys(args, app, loop, keys_fd)
|
|
|
platform.run(loop)
|
|
|
app.close()
|
|
|
return 0
|
|
|
|
|
|
|
|
|
+def run_keytest(args):
|
|
|
+ from gi.repository import GLib
|
|
|
+
|
|
|
+ from .keytest import KeyTest
|
|
|
+
|
|
|
+ try:
|
|
|
+ fd = platform.open_i2c_bus(args.bus, args.allow_adapter)
|
|
|
+ except platform.BusRefused as e:
|
|
|
+ print(e, file=sys.stderr)
|
|
|
+ return 1
|
|
|
+
|
|
|
+ exp = keypad.Expander(fd, args.keys_addr)
|
|
|
+ try:
|
|
|
+ exp.setup()
|
|
|
+ raw = exp.read_inputs()
|
|
|
+ except OSError as e:
|
|
|
+ print("key expander at 0x%02x: %s" % (args.keys_addr, e.strerror), file=sys.stderr)
|
|
|
+ return 1
|
|
|
+ print("key expander at 0x%02x: P0=%02x P1=%02x (all keys up: 00 00)"
|
|
|
+ % (args.keys_addr, raw & 0xff, raw >> 8))
|
|
|
+ stuck = [keypad.KEY_PINS[b] for b in sorted(keypad.KEY_PINS) if raw & (1 << b)]
|
|
|
+ if stuck:
|
|
|
+ print("held or stuck high at start: " + ", ".join(stuck))
|
|
|
+ if raw & ~keypad.KEY_MASK:
|
|
|
+ print("unused pins P0.0-P0.2 read high: %02x" % (raw & ~keypad.KEY_MASK))
|
|
|
+
|
|
|
+ present = lambda *a: None # noqa: E731
|
|
|
+ panel = None
|
|
|
+ if not args.no_panel:
|
|
|
+ from .panel import Panel
|
|
|
+ panel = Panel(args.width, args.height, args.bpp, args.tcr, fd=fd, addr=args.addr)
|
|
|
+ try:
|
|
|
+ panel.backlight(True)
|
|
|
+ panel.init()
|
|
|
+ except OSError as e:
|
|
|
+ print(e, file=sys.stderr)
|
|
|
+ return 1
|
|
|
+
|
|
|
+ def present(data, stride, x, y, w, h):
|
|
|
+ try:
|
|
|
+ panel.update(data, stride, x, y, w, h)
|
|
|
+ except OSError as e:
|
|
|
+ print("panel update failed: %s" % e, file=sys.stderr)
|
|
|
+
|
|
|
+ test = KeyTest(args.width, args.height, present)
|
|
|
+ if args.init_only:
|
|
|
+ test.on_raw(raw) # draws the key screen
|
|
|
+ print("initialised; panel and backlight left on")
|
|
|
+ return 0
|
|
|
+ poller = keypad.KeyPoller(exp, test.on_event, test.on_raw, args.keys_poll)
|
|
|
+
|
|
|
+ def watch_errors():
|
|
|
+ if poller.errors != test.errors:
|
|
|
+ test.draw(poller.errors)
|
|
|
+ return True
|
|
|
+
|
|
|
+ GLib.timeout_add(500, watch_errors)
|
|
|
+ loop = GLib.MainLoop()
|
|
|
+ if args.exit_after:
|
|
|
+ GLib.timeout_add(args.exit_after, lambda: loop.quit() or False)
|
|
|
+ print("press the keys; Ctrl-C quits")
|
|
|
+ platform.run(loop)
|
|
|
+ poller.close()
|
|
|
+ print("%d presses, %d repeats, %d read errors"
|
|
|
+ % (test.presses, test.repeats, poller.errors))
|
|
|
+ if panel:
|
|
|
+ try:
|
|
|
+ panel.display(False)
|
|
|
+ panel.backlight(False)
|
|
|
+ except OSError:
|
|
|
+ pass
|
|
|
+ return 0
|
|
|
+
|
|
|
+
|
|
|
def run_sim(args):
|
|
|
from . import sim # first: it picks the Gtk version
|
|
|
from gi.repository import GLib, Gtk
|
|
|
@@ -236,7 +384,8 @@ def run_sim(args):
|
|
|
|
|
|
def main(argv=None):
|
|
|
args = parse_args(sys.argv[1:] if argv is None else argv)
|
|
|
- return {"sim": run_sim, "i2c": run_i2c, "drm": run_drm}[args.frontend](args)
|
|
|
+ return {"sim": run_sim, "i2c": run_i2c, "drm": run_drm,
|
|
|
+ "keytest": run_keytest}[args.frontend](args)
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|