#!/usr/bin/env python3 from pyiArduinoI2Cexpander import * import os import argparse import time PROG_NAME = "ATX Control" PROG_VERSION = 'v' + open(os.path.join(os.path.dirname(os.path.realpath(__file__)), "VERSION")).readline() PROG_DESCRIPTION = "" PROG_EPILOG = "" EXP_ADDR = 0x08 POWER_PIN = 0 RESET_PIN = 1 STATUS_PIN = 2 PRESS_TIME_SEC = 1 LONG_PRESS_TIME_SEC = 5 def exp_init(exp_addr, power_pin, reset_pin, status_pin): exp = pyiArduinoI2Cexpander(exp_addr) try: exp.pinMode(power_pin, OUTPUT, DIGITAL) exp.digitalWrite(power_pin, LOW) exp.pinMode(reset_pin, OUTPUT, DIGITAL) exp.digitalWrite(reset_pin, LOW) exp.pinMode(status_pin, INPUT, DIGITAL) exp.pinPull(status_pin, PULL_DOWN) except Exception as E: print(f"Error initializing expander hat: {E}") exit(1) return exp def get_status(exp, status_pin): print("Getting status...") try: status_raw = exp.digitalRead(status_pin) except Exception as E: print(f"Error reading status: {E}") exit(1) return status_raw def press_power(exp, power_pin): print("Switching power...") try: exp.digitalWrite(power_pin, HIGH) time.sleep(PRESS_TIME_SEC) exp.digitalWrite(power_pin, LOW) except Exception as E: print(f"Error pressing power: {E}") exit(1) def press_reset(exp, reset_pin): print("Resetting...") try: exp.digitalWrite(reset_pin, HIGH) time.sleep(PRESS_TIME_SEC) exp.digitalWrite(reset_pin, LOW) except Exception as E: print(f"Error pressing reset: {E}") exit(1) def power_off(exp, power_pin): print("Powering off...") try: exp.digitalWrite(power_pin, HIGH) time.sleep(LONG_PRESS_TIME_SEC) exp.digitalWrite(power_pin, LOW) except Exception as E: print(f"Error powering off: {E}") exit(1) def main(): parser = argparse.ArgumentParser(description="ATX Power Control") parser.add_argument('-s', '--status', action='store_true', help='Get status of the host') parser.add_argument('-p', '--press-power', action='store_true', help='Press power button') parser.add_argument('-r', '--press-reset', action='store_true', help='Press reset button') parser.add_argument('--power-off', action='store_true', help='Force power off (long press power button)') args = parser.parse_args() exp = exp_init(EXP_ADDR, POWER_PIN, RESET_PIN, STATUS_PIN) if args.status: if get_status(exp, STATUS_PIN): print("Host is powered on") else: print("Host is powered off") elif args.press_power: press_power(exp, POWER_PIN) elif args.press_reset: press_reset(exp, RESET_PIN) elif args.power_off: power_off(exp, POWER_PIN) else: parser.print_help() if __name__ == "__main__": main()