#!/usr/bin/python3

from ptcommon.i2c_device import I2CDevice

try:

    device = I2CDevice("/dev/i2c-1", 0x10)
    device.connect()

    print("----------------------------")
    print("pi-topHUB v2 Register Values")
    print("----------------------------")

    print("")
    print("[DISPLAY]")

    shutdown_ctrl = device.read_unsigned_byte(0xA0)
    butt_short_hold_turnon = device.read_unsigned_byte(0xA1)
    butt_short_hold_turnoff = device.read_unsigned_byte(0xA2)
    butt_long_hold = device.read_unsigned_byte(0xA3)
    m1_timeout_min = device.read_unsigned_word(0xAA)
    m1_timeout_max = device.read_unsigned_word(0xAB)
    m2_timeout_min = device.read_unsigned_word(0xAC)
    m2_timeout_max = device.read_unsigned_word(0xAD)
    m3_timeout = device.read_unsigned_word(0xAE)

    print("shutdown_ctrl: " + str(shutdown_ctrl))
    print("butt_short_hold_turnon: " + str(butt_short_hold_turnon))
    print("butt_short_hold_turnoff: " + str(butt_short_hold_turnoff))
    print("butt_long_hold: " + str(butt_long_hold))
    print("m1_timeout_min: " + str(m1_timeout_min))
    print("m1_timeout_max: " + str(m1_timeout_max))
    print("m2_timeout_min: " + str(m2_timeout_min))
    print("m2_timeout_max: " + str(m2_timeout_max))
    print("m3_timeout: " + str(m3_timeout))

    print("")
    print("[BATTERY]")

    temperature = device.read_unsigned_word(0xB1)
    voltage = device.read_unsigned_word(0xB2)
    current = device.read_unsigned_word(0xB3)
    rsoc = device.read_unsigned_byte(0xB4)
    time_to_empty = device.read_unsigned_word(0xB5)
    time_to_full = device.read_unsigned_word(0xB6)

    print("temperature: " + str(temperature))
    print("voltage: " + str(voltage))
    print("current: " + str(current))
    print("rsoc: " + str(rsoc))
    print("time_to_empty: " + str(time_to_empty))
    print("time_to_full: " + str(time_to_full))

    print("")
    print("[AUDIO]")

    config = device.read_unsigned_byte(0xC0)

    print("config: " + str(config))

    print("")
    print("[DISPLAY]")

    backlight = device.read_unsigned_byte(0xD1)

    print("backlight: " + str(backlight))

    print("")
    print("[VERSION]")

    mcu_soft_vers_major = device.read_unsigned_byte(0xE0)
    mcu_soft_vers_minor = device.read_unsigned_byte(0xE1)
    sch_rev_major = device.read_unsigned_byte(0xE2)
    sch_rev_minor = device.read_unsigned_byte(0xE3)
    brd_rev = device.read_unsigned_byte(0xE4)

    print("mcu_soft_vers_major: " + str(mcu_soft_vers_major))
    print("mcu_soft_vers_minor: " + str(mcu_soft_vers_minor))
    print("sch_rev_major: " + str(sch_rev_major))
    print("sch_rev_minor: " + str(sch_rev_minor))
    print("brd_rev: " + str(brd_rev))

    device.disconnect()

except Exception as e:
    print("Error getting pi-topHUB v2 hardware diagnostics")
    print(str(e))
