From 76e227ae9e878e05b85fa1c7d3ca666fe3a3d079 Mon Sep 17 00:00:00 2001 From: Marijn Doeve Date: Sun, 5 Jun 2022 14:20:14 +0200 Subject: [PATCH] initial commit --- .gitignore | 4 ++ __main__.py | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++ projector.py | 12 ++++++ 3 files changed, 136 insertions(+) create mode 100644 .gitignore create mode 100755 __main__.py create mode 100644 projector.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..495abed --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +__pycache__/ +*.pyc +*.swp + diff --git a/__main__.py b/__main__.py new file mode 100755 index 0000000..6c65e1b --- /dev/null +++ b/__main__.py @@ -0,0 +1,120 @@ +#! /usr/bin/env python3 +from serial import Serial +from argparse import ArgumentParser +from time import sleep + + +# w == escape +# | == carriage return +POWER_SAVE_ON = "w1PSAV|" +POWER_SAVE_OFF = "w0PSAV|" +POWER_SAVE = "wpsav|" +INPUT = lambda x: f"{x}!" + +EDIDS = { + "automatic": 0, + "1280x800": 14, + "720p60": 34, + "1080p60": 45, +} + +ERRORS = { + "E01": "Invalid input number", + "E06": "Invalid switch attempt in this mode", + "E10": "Invalid command", + "E11": "Invalid preset number", + "E12": "Invalid port number", + "E13": "Invalid parameter", + "E14": "Not valid for this configuration", + "E17": "Invalid command for signal type", + "E22": "Busy", +} + +verbose = False + + +def send_command(command: str) -> str: + with Serial("/dev/serial/by-id/usb-Extron_Product-if00", 9600, timeout=1) as ser: + if verbose: + print("Send:", command) + + ser.write(command.encode()) + # response = ser.readline().decode().strip() + response = None + + while not (response := ser.readline()): + pass + response = response.decode().strip() + + if verbose: + print("Resp:", response) + if not response: + print("no reponse") + exit(1) + if response[0] == "E": + print(response, ERRORS.get(response, "Unknown")) + exit(1) + + return response + + +def send_command_projector(command: str) -> str: + with Serial("/dev/serial0", 9600, timeout=1) as ser: + ser.write(command.encode()) + response = ser.readline() + + if response: + print(response.decode()) + + +def check_power_save(): + resp = send_command(POWER_SAVE) + + return int(resp) + + +def sleep_extron(): + send_command(POWER_SAVE_ON) + + +def wake(): + send_command(POWER_SAVE_OFF) + + +def change_input(input: int): + send_command(INPUT(input)) + + +if __name__ == "__main__": + parser = ArgumentParser() + + parser.add_argument( + "-v", "--verbose", action="store_true", help="increase output verbosity" + ) + + sleep_or_wake = parser.add_mutually_exclusive_group() + sleep_or_wake.add_argument( + "input", + nargs="?", + type=int, + choices=[1, 2, 3, 4], + help="wake and select input", + ) + sleep_or_wake.add_argument("--sleep", action="store_true", help="put in sleep mode") + args = parser.parse_args() + + verbose = args.verbose + + if args.input: + send_command_projector("\x02ADZZ;PON\x03") + wake() + change_input(args.input) + + if verbose: + print("Waking projector") + #send_command_projector("\x02ADZZ;PON\x03") + + elif args.sleep: + send_command_projector("\x02ADZZ;POF\x03") + + sleep_extron() diff --git a/projector.py b/projector.py new file mode 100644 index 0000000..372e0ae --- /dev/null +++ b/projector.py @@ -0,0 +1,12 @@ +from serial import Serial + +STX = chr(0x02) +ETX = chr(0x03) + +if __name__ == "__main__": + with Serial("/dev/ttyAMA0", 9600, timeout=1) as ser: + ser.write((STX + "ADZZ" + ";" + "PON" + ETX).encode()) + while not (response := ser.readline()): + pass + print(response) +