"""radio-ui: the radio panel UI. 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 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 keypad, platform def _int(text): return int(text, 0) def _add_common(p, default_host): p.add_argument("--host", default=default_host, help="MPD host, or a socket path (%(default)s)") p.add_argument("--port", type=int, default=6600, help="MPD port (%(default)s)") p.add_argument("--kmsg", action="store_true", help="show this machine's kernel log") def _add_geometry(p): p.add_argument("--width", type=int, default=240, help="panel width (%(default)s)") p.add_argument("--height", type=int, default=160, help="panel height (%(default)s)") p.add_argument("--bpp", type=int, default=2, choices=(1, 2, 4), help="bits per pixel (%(default)s)") p.add_argument("--tcr", type=int, default=162, help="TC/R, sets the frame rate (%(default)s)") def _add_keys(p, backlight_help): p.add_argument("--input", metavar="PATH", help="key input device (default: the one named %s)" % platform.DEFAULT_KEYS_NAME) p.add_argument("--window-keys", action="store_true", help="test mode: take keys from a window on the X display " "instead of evdev (also reads --input if given)") 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") sub.required = True p = sub.add_parser("sim", help="window with the simulated panel and on-screen keys") _add_common(p, "radio.lo") _add_geometry(p) p.add_argument("--scale", type=int, default=3, help="window zoom (%(default)s)") p.add_argument("--window-keys", action="store_true", help="accepted for symmetry: this window always takes the keys") p.add_argument("--press", metavar="LIST", help="click these keys, comma separated") p.add_argument("--snapshot", metavar="FILE", help="write the window to a PNG before exiting") p.add_argument("--exit-after", type=int, default=0, metavar="MS", help="quit after this many ms") p = sub.add_parser("i2c", help="real panel through /dev/i2c-N") _add_common(p, "localhost") _add_geometry(p) p.add_argument("--bus", type=int, required=True, help="I2C bus number") p.add_argument("--addr", type=_int, default=0x74, help="expander address (0x74)") p.add_argument("--allow-adapter", metavar="NAME", help="accept the adapter with this exact name") p.add_argument("--tx-cap", type=int, default=1025, metavar="BYTES", help="largest I2C message, 7..1025 (%(default)s)") p.add_argument("--no-backlight", action="store_true", help="leave the expander's backlight pin off") 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: ap.error("--tx-cap: 7..1025") return args # ---- bring-up patterns --------------------------------------------------------- def make_pattern(name, w, h): """XRGB8888, white background""" fb = bytearray(b"\xff") * (w * h * 4) black = b"\0\0\0\0" def put(x, y): o = (y * w + x) * 4 fb[o:o + 4] = black if name == "geometry": # border, plus ticks every 10 px and long ticks every 50: shows the real size for x in range(w): put(x, 0) put(x, h - 1) if not x % 10: for i in range(4 if x % 50 else 10): put(x, 1 + i) for y in range(h): put(0, y) put(w - 1, y) if not y % 10: for i in range(4 if y % 50 else 10): put(1 + i, y) # a diagonal shows skew from a wrong line length for i in range(min(w, h)): put(i, i) else: levels = (0xff, 0xaa, 0x55, 0x00) row = bytearray() for x in range(w): v = levels[x * 4 // w] row += bytes((v, v, v, 0)) fb = row * h return fb # ---- frontends ------------------------------------------------------------------- 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", "?")) 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 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): from gi.repository import GLib from .app import App from .panel import Panel try: fd = platform.open_i2c_bus(args.bus, args.allow_adapter) except platform.BusRefused as e: print(e, file=sys.stderr) return 1 panel = Panel(args.width, args.height, args.bpp, args.tcr, fd=fd, addr=args.addr, tx_cap=args.tx_cap) if not args.backlight and not args.no_backlight: panel.backlight(True) try: panel.init() except OSError as e: print(e, file=sys.stderr) return 1 if args.pattern: fb = make_pattern(args.pattern, args.width, args.height) try: panel.update(fb, args.width * 4, 0, 0, args.width, args.height) except OSError as e: print("pattern failed: %s" % e, file=sys.stderr) return 1 st = panel.stats() print("pattern sent: %d I2C bytes in %d messages" % (st.i2c_bytes, st.i2c_msgs)) return 0 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) def backlight(on): # sysfs if a path was given, otherwise the pin on the panel's own expander if args.backlight: platform.backlight_set(args.backlight, on) return try: panel.backlight(on) except OSError as e: print("backlight: %s" % e, file=sys.stderr) loop = GLib.MainLoop() app = App(args.host, args.port, args.width, args.height, present, backlight, args.kmsg) _start_keys(args, app, loop, None if args.no_i2c_keys else fd) platform.run(loop) app.close() try: panel.display(False) except OSError: pass return 0 def run_drm(args): from gi.repository import GLib from .app import App from .drm import DrmError, DrmPanel try: dev = DrmPanel() except (DrmError, OSError) as e: print("cannot set up the display: %s" % e, file=sys.stderr) 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) except DrmError as e: print(e, file=sys.stderr) 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, 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 for sig in (2, 15): # SIGINT, SIGTERM GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, sig, lambda *_: Gtk.main_quit() or False) return sim.run(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, "keytest": run_keytest}[args.frontend](args) if __name__ == "__main__": sys.exit(main())