One year of MM Board Reform (Kodi based Debian mediacenter)

Today marks the first birthday of my Kodi based mediacenter setup called “MM Board Reform” (mister-muffin Reform on a wooden board). This is not ISO grain on the photo below. In the absence of the MNT Desktop Reform aluminum case it has gathered a lot of dust:

The system is running Debian from an SD card (which probably only survives because there are not many writes during normal operation) and under the cooler is the A311D which is perfect for a Kodi based mediacenter setup because:

  • not only do I get hardware accelerated video output but I can also
  • control kodi using an infrared remote of the TV attached via HDMI

To switch this on I’m using a RP2040 board with the following micropython script running on it:

from machine import Pin, Timer, UART
from micropython import schedule

led = Pin("LED", Pin.OUT)
uart0 = UART(0, baudrate=57600, tx=Pin(0), rx=Pin(1))

def write_both(msg):
    led.on()
    uart0.write(msg + b"\r\n")


def timercb(timer, pin, msg):
    timer.deinit()
    led.off()
    pin.irq(lambda pin: irqcb(pin, msg), Pin.IRQ_FALLING)


def irqcb(pin, msg):
    # disable further interrupts
    pin.irq(handler=None)
    # write to uart outside of irq handler
    schedule(write_both, msg)
    # re-enable irq after a short while for debouncing
    Timer(
        mode=Timer.ONE_SHOT, period=200, callback=lambda timer: timercb(timer, pin, msg)
    )


Pin("GP14", Pin.IN, Pin.PULL_UP).irq(lambda pin: irqcb(pin, b"1p"), Pin.IRQ_FALLING)
Pin("GP17", Pin.IN, Pin.PULL_UP).irq(lambda pin: irqcb(pin, b"0p"), Pin.IRQ_FALLING)

The user user is getting automatically logged in via /etc/greetd/config.toml:

[initial_session]
command = "sway"
user = "user"

sway itself does not start anything: no waybar, no pasystray, no nm-applet. Instead kodi is started via a /etc/NetworkManager/dispatcher.d/tv-on.sh:

#!/bin/sh
set -eu
if [ "$1" = "wlan0" ] && [ "$2" = "up" ] && [ "$CONNECTION_UUID" = "48d2a8ea-b9ca-4f5b-b1ed-c78bd4ab7f78" ]; then
	ip route add 192.168.1.184 dev wlan0 || :
	curl http://192.168.1.184/cm?cmnd=Power%20on
	sleep 17
	export SWAYSOCK="$(ls /run/user/1000/sway-ipc.1000.*.sock)"
	swaymsg output eDP-1 disable
	sleep 1
	swaymsg output HDMI-1 enable
	sleep 2
	swaymsg exec kodi
fi

This has an ugly sleep in it but this is necessary or otherwise kodi might start before the TV is on (it’s turned on by sending “Power%20on” to a esp32 power switch to which the TV is connected) and if that happens, then kodi has to be restarted. 17 seconds is how long the TV needs to turn on. The output eDP-1 disable && output HDMI-1 enable dance is what is needed on a311d because one cannot have both the internal display (which is not connected) and HDMI at the same time. Again, we have to wait a bit and make sure that kodi does not start too early. Kode must only be started once the TV is on and sway configured to output onto it.

The system is turned off via a /etc/systemd/system/tvoff.service:

[Unit]
Description=Turn off tv
Requires=network-online.target
After=network-online.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStop=curl http://192.168.1.184/cm?cmnd=Power%%20off

[Install]
WantedBy=multi-user.target
6 Likes