# radio-ui: MPD front panel on an S1D13700 LCD The front panel of an internet radio: an MPD client that shows the current song, time, volume and playlist on a 240×160 S1D13700 graphic LCD and takes 13 keys. The LCD and the keys hang off two TCA9539 I²C expanders on the Linux board (an Orange Pi Zero); this replaces an earlier LPC1114 microcontroller firmware. It runs on the radio, through the DRM driver or i2c-dev, and on a PC with a USB I²C bridge or a simulated panel in a window. The panel core (C), its simulator and the DRM driver are a separate repository, [s1d13700-tca](../s1d13700-tca), included here as the git submodule `panel/`. The keys' wiring and device tree are in [radio-keyboard-tca](../radio-keyboard-tca), the submodule `keyboard/`. | Path | What | |---|---| | `radio/` | The application, in Python: UI, MPD client, I²C key polling and the `sim`, `i2c`, `drm` and `keytest` frontends | | `radio/libpanel.c` | The panel core and simulator as a library for the application | | `radio-ui` | Starts the application | | `tests/` | Application tests, a fake MPD server and `hw_test.sh` | | `tools/install-service.sh` | Installs radio-ui as a systemd, sysvinit or OpenRC service | | `panel/` | Submodule: s1d13700-tca (panel core, simulator, DRM driver, device tree overlay) | | `keyboard/` | Submodule: radio-keyboard-tca (key expander wiring, device tree overlay) | | `docs/REPORT.md` | The port from the LPC1114 firmware: decisions, verification, history | ## Building git clone --recurse-submodules /radio-ui.git # or, in an existing clone: git submodule update --init make lib # build/libradiopanel.so, needed by the application make test # application tests, no hardware or display make panel-test # the panel core's tests (in panel/) make module KDIR=/path/to/kernel # the DRM driver, in panel/driver/ `PANEL_DIR=/path/to/s1d13700-tca` uses another checkout of the panel repository instead of the submodule (`make`, `tests/hw_test.sh` and the tests all read it). The key tests check `radio/keypad.py` and `radio/platform.py` against `dts/radio-keys.dts` in radio-keyboard-tca; `KEYBOARD_DIR` points them at another checkout of it. The application runs on Python 3.7.3 (Debian buster) to 3.14 and needs PyGObject with GLib, Pango and PangoCairo, pycairo and its PyGObject bridge; GTK 3 for `sim` and `--window-keys`; libdrm for `drm`. On Debian: apt install python3-gi python3-gi-cairo gir1.2-pango-1.0 gir1.2-gtk-3.0 `RADIO_PANEL_LIB` points it at the library if it is not in `build/`. Volume is set through MPD (`volume ±1`), so MPD needs a working mixer. The default hardware mixer looks for a `PCM` control, which the H3 codec does not have; the firmware's old `volup`/`voldown` scripts used `DAC`, so the ALSA output in `/etc/mpd.conf` needs: mixer_type "hardware" mixer_device "default" mixer_control "DAC" Without it MPD's status has no `volume:` line and the volume keys do nothing. ## Running Window with the simulated panel, using a real MPD: ./radio-ui sim --host radio.lo The window shows the simulator's display memory, and the traffic each update costs. Keys: `Z X C V B` for prev/play/pause/stop/next, arrows, `Enter`, `Esc`, `M`, `L`, `Space` for pause, and a multimedia keyboard's media keys. Real panel through i2c-dev, for bring-up or on a kernel too old for the driver, or through the DRM driver: ./radio-ui i2c --bus N --pattern geometry ./radio-ui i2c --bus N --pattern gray ./radio-ui i2c --bus N --host radio.lo ./radio-ui drm --host radio.lo On the main screen the keys are prev/play/pause/stop/next, up/down for the volume and Esc to refresh. List opens the playlist: the whole MPD queue, the current song centered and selected. Up/down move the selection, OK plays it, and Menu, Esc, List or 30 s without a key go back. With more than 1 bpp the selection is a gray bar, otherwise inverted. The transport keys keep working there. `--bus` must name a known adapter; motherboard and display buses are always refused. Only use the bus number of the adapter the panel is wired to. `./radio-ui FRONTEND --help` lists all options. ### Hardware test: panel and keys `tests/hw_test.sh` finds the i2c-tiny-usb bus, checks that no kernel driver holds 0x74 or 0x76, builds the library if needed and runs: tests/hw_test.sh # panel shows the 13 keys; pressed ones inverted tests/hw_test.sh keys-only # keys printed to the terminal, panel untouched tests/hw_test.sh pattern # geometry and gray patterns, then the keys tests/hw_test.sh ui # full UI with the I2C keys, against the fake MPD tests/hw_test.sh init # panel and U2 set up over i2c-dev, left on tests/hw_test.sh drm # sudo: bind the DRM driver to 0x74, pattern, # backlight on, U2 set up; left active tests/hw_test.sh release # sudo: unbind the DRM driver again tests/hw_test.sh -b 11 keys # a given bus The key screen (`radio-ui keytest --bus N`) also shows the raw input ports, press and repeat counts and read errors; each event is printed too. Ctrl-C quits and switches the panel off. The i2c-dev modes refuse to run while a kernel driver holds 0x74 or 0x76; `release` frees 0x74 after `drm`. After `drm`, `./radio-ui drm --host radio.lo` drives the panel through the driver and reads the keys from U2 on the same bus. ### Keys over I²C `radio-ui i2c` polls U2 at 0x76 on its own bus every 20 ms (`--keys-addr`, `--keys-poll`; `--no-i2c-keys` falls back to evdev). `radio-ui drm` uses the evdev device `radio-keys` if it exists (the overlay on the target), otherwise U2 on the bus the DRM panel is on; `--keys-bus N` names another bus and `--no-i2c-keys` disables this. The polling reads debounce over two samples and autorepeat like gpio-keys (250 ms, then every 33 ms; the app's 300 ms lockout applies). Keys held at start are ignored until released, and a reading with more than 3 keys down is taken as a bus fault, so a fault cannot send player commands. Do not load the U2 node (`keyboard/dts/radio-keys.dts`) while doing this. ### Test mode: real panel, PC keyboard `--window-keys` opens a small window on the X display; keys typed into it (the same as in `sim`) go to the app, which drives the real panel and the real MPD: ./radio-ui i2c --bus 11 --host radio.lo --window-keys ./radio-ui drm --host radio.lo --window-keys It also has a button per key. Closing the window quits. Evdev is not read in this mode unless `--input` is given as well. Text uses Verdana and Andale Mono (Microsoft core fonts) if installed, otherwise Liberation Sans and Mono, drawn without antialiasing. ### Test rig on the development PC The panel hangs off an ATmega8U2 i2c-tiny-usb bridge. Load the driver for 400 kHz (its default, `delay=10`, is only 50 kHz), then find the bus number: sudo modprobe -r i2c_tiny_usb; sudo modprobe i2c_tiny_usb delay=1 grep -l i2c-tiny-usb /sys/bus/i2c/devices/i2c-*/name To try the keys without touching the real player, run the fake MPD and point the panel at it: tests/fake_mpd.py 6601 build/mpd-commands.log 5 & ./radio-ui i2c --bus 11 --host localhost --port 6601 --window-keys Never scan or use the other buses on this PC: some reach the CPU regulators. ### Simulating keys The panel frontends read keys from evdev: the device named `radio-keys`, or whatever `--input` names. On the PC without the U2 keypad: - **PC keyboard:** `--input /dev/input/by-id/-event-kbd` (needs read access, group `input`). Arrows and Esc map directly; the media keys of a multimedia keyboard map to Prev/Next/Stop if they send `KEY_PREVIOUSSONG`, `KEY_NEXTSONG`, `KEY_STOPCD`. Play, Pause, OK, Menu and List have no ordinary key. The keyboard keeps typing into the focused window too. - **Virtual device:** with `CONFIG_INPUT_UINPUT`, create a device named `radio-keys` (for example `evemu-device` or python-evdev's `UInput`) and send the codes in `radio/platform.py`. `radio-ui` finds it without `--input`. The development kernel has uinput disabled. - **Window:** `--window-keys` (above), or `radio-ui sim`, which draws on the simulated panel only. ## Wiring **U1, TCA9539 at 0x74** (A1=L, A0=L), driven by the panel core (`panel/README.md`) | Pin | Signal | Module pin | |---|---|---| | P0.0-7 | D0-D7 | D0-D7 | | P1.0 | Backlight enable (active high) | | | P1.1 | RES# | RST | | P1.2 | A0 | A15 | | P1.3 | WR# | WE | | P1.4 | RD# | RE | | P1.5 | CS# (held low after reset; was hardwired low on the old board) | CS0 | | P1.6-7 | spare | | **U2, TCA9539 at 0x76** (A1=H, A0=L), the keys: polled by `radio-ui i2c`, or stock `gpio-pca953x` and `gpio-keys` with the device tree. Pin map, key codes and overlay are in `keyboard/README.md`. `INT` is not connected on the USB test board, so the keys are polled there; on the Orange Pi Zero it is on PA6. ## On the Orange Pi Zero The radio's kernel, U-Boot and board device tree are built by the OPi0 project (`build-kernel.sh`), which copies the driver from the s1d13700-tca repository and builds it in, together with `gpio-pca953x` and `gpio-keys` for U2. There `radio-ui drm` finds the panel and the `radio-keys` input device by itself. The backlight and input device permissions (udev rule, `video` and `input` groups) are described in `panel/README.md`, `keyboard/README.md` and the OPi0 README. On a kernel without the driver (Armbian 4.19), use `radio-ui i2c --bus 1`; set the bus to 400 kHz (`clock-frequency` on i2c1), the mv64xxx default of 100 kHz makes a playlist redraw take about half a second. ### As a service `tools/install-service.sh` (run with sudo) writes a service that starts `radio-ui` from this checkout at boot, after MPD, as the user who ran sudo, then enables and starts it. The init system is detected (`-i systemd`, `sysvinit` or `openrc` overrides); the arguments after `--` go to radio-ui, `drm` by default: make lib # as yourself, first sudo tools/install-service.sh # radio-ui drm sudo tools/install-service.sh -- i2c --bus 1 --host radio.lo tools/install-service.sh -p -i openrc # print the file only sudo tools/install-service.sh -r # stop, disable and remove `-u USER` names another user; it needs the `video`, `input` and `i2c` groups (the script warns if not). systemd logs to the journal, sysvinit and OpenRC to `/var/log/radio-ui.log`. `-N` writes the file without enabling or starting it. ## Not settled - TC/R (`--tcr`) for the frame rate; 162 comes from the AVR project's 2 bpp setup - Key autorepeat timing on held keys