Screeneo U5 turn on direct to HDMI2

I created this to switch hdmi port or power up/down the projector with keyboard shortcuts via an usb to RS232 serial port cable on Ubuntu 22.04. Maybe it saves someone else time who also wants to automate this. Currently hardcoded to work with F4 (Ctrl, Meta, Backspace) and F11 (Homepage) function keys on my Logitech K810 keyboard, but should be adaptable if you read the keycodes with the evtest tool.

/usr/bin/projector_control.py

#!/usr/bin/env python3

from evdev import InputDevice, list_devices, categorize, ecodes, KeyEvent
from serial import Serial
import asyncio

# Initialize the serial connection to the projector
projector = Serial('/dev/ttyUSB0', 38400)

# This dictionary will track the state of the keys we are interested in
key_states = {'KEY_LEFTCTRL': False, 'KEY_LEFTMETA': False, 'KEY_BACKSPACE': False}

# Variables to store the projector state
power_state = False  # Starts in powered-off state
hdmi_state = 1  # Starts in HDMI1 state

# Asynchronous function to handle input from one device
async def handle_device(dev):
    global power_state
    global hdmi_state
    async for event in dev.async_read_loop():
        if event.type == ecodes.EV_KEY:
            key_event = categorize(event)
            # Check if one of the keys we're interested in has been released
            if key_event.keystate == KeyEvent.key_up and key_event.keycode in key_states:
                key_states[key_event.keycode] = not key_states[key_event.keycode]

                # If all keys have been released, switch HDMI state
                if all(key_states.values()):
                    if hdmi_state == 1:
                        projector.write(b'\x0D\x35\x33\x39\x34\x0D')
                        hdmi_state = 2
                    else:
                        projector.write(b'\x0D\x35\x33\x38\x33\x0D')
                        hdmi_state = 1

            # Check if the 'XF86HomePage' key (corresponding to keycode 180) has been released
            elif key_event.keystate == KeyEvent.key_up and key_event.keycode == 'KEY_HOMEPAGE':
                if power_state:
                    projector.write(b'\x0D\x35\x33\x36\x31\x0D')
                    power_state = False
                else:
                    projector.write(b'\x0D\x41\x37\x33\x35\x30\x41\x42\x43\x0D')
                    power_state = True

# Get the list of all input devices
devices = [InputDevice(path) for path in list_devices()]

# Create a new event loop
loop = asyncio.new_event_loop()

# Set the event loop as the current one
asyncio.set_event_loop(loop)

# Create and run tasks for all keyboards
for dev in devices:
    if "keyboard" in dev.name.lower():
        loop.create_task(handle_device(dev))

# Run the event loop
loop.run_forever()

/etc/systemd/system/projector-control.service

[Unit]
Description=Projector Control

[Service]
ExecStart=/usr/bin/projector_control.py
Restart=always
User=root
Group=root
Environment=PATH=/usr/bin:/usr/local/bin
Environment=PYTHONUNBUFFERED=1

[Install]
WantedBy=multi-user.target

Setup

# Remove braille reader package as it conflicts with the usb to serial cable mapping at /dev/ttyUSB0
sudo apt remove brltty

sudo apt install python3-evdev

sudo chmod +x /usr/bin/projector_control.py

sudo systemctl daemon-reload

sudo systemctl enable projector-control

sudo systemctl start projector-control
1 Like