#!/usr/bin/python3

# Script to configure the new pi-top hub, enabling or disabling the
# HDMI to I2S audio conversion on a pi-top V2

from ptcommon.i2c_device import I2CDevice
import sys

AUD__CONFIG = 0xC0
AUD__CONFIG__HDMI = 0x01
AUD__CONFIG__HPDET = 0x02

if (len(sys.argv) != 2):
    print("Usage: " + sys.argv[0] + " <enable|disable>")
    sys.exit(1)
elif (sys.argv[1] == "enable" or sys.argv[1] == "disable"):
    print("Usage: " + sys.argv[0] + " <enable|disable>")
    sys.exit(1)

try:
    hub = I2CDevice("/dev/i2c-1", 0x10)
    hub.connect()

    audio_control = hub.read_unsigned_byte(AUD__CONFIG)

    if (sys.argv[1] == "enable"):
        hub.write_byte(AUD__CONFIG, audio_settings | AUD__CONFIG__HDMI)
    elif (sys.argv[1] == "disable"):
        hub.write_byte(AUD__CONFIG, audio_settings & (~AUD__CONFIG__HDMI & 0xFF))

except Exception as e:

    print("Error communicating with hub: " + str(e))
