#!/usr/bin/env python3 """ GPIO Test Utility - Verify power button GPIO configuration """ import sys import time import os try: from gpiozero import Button GPIOZERO_AVAILABLE = True except ImportError: GPIOZERO_AVAILABLE = False try: import RPi.GPIO as GPIO except ImportError: print("ERROR: Neither gpiozero nor RPi.GPIO could be imported") print("Install with: pip3 install gpiozero RPi.GPIO") sys.exit(1) def test_gpio_gpiozero(pin): """Test GPIO with gpiozero library.""" print(f"Testing GPIO pin {pin} with gpiozero...") try: button = Button(pin) print(f"✓ Button object created for pin {pin}") # Test set high print(f"Setting pin {pin} HIGH (simulating button press)...") button.pin.drive_high() time.sleep(1) print("✓ Pin set HIGH") # Test set low print(f"Setting pin {pin} LOW (releasing button)...") button.pin.drive_low() time.sleep(0.5) print("✓ Pin set LOW") print("✓ GPIO test successful with gpiozero\n") return True except Exception as e: print(f"✗ Error with gpiozero: {e}\n") return False def test_gpio_rpi(pin): """Test GPIO with RPi.GPIO library.""" print(f"Testing GPIO pin {pin} with RPi.GPIO...") try: GPIO.setmode(GPIO.BCM) GPIO.setup(pin, GPIO.OUT, initial=GPIO.LOW) print(f"✓ GPIO pin {pin} configured") # Test set high print(f"Setting pin {pin} HIGH (simulating button press)...") GPIO.output(pin, GPIO.HIGH) time.sleep(1) print("✓ Pin set HIGH") # Test set low print(f"Setting pin {pin} LOW (releasing button)...") GPIO.output(pin, GPIO.LOW) time.sleep(0.5) print("✓ Pin set LOW") GPIO.cleanup() print("✓ GPIO test successful with RPi.GPIO\n") return True except Exception as e: print(f"✗ Error with RPi.GPIO: {e}\n") return False def main(): print("=" * 60) print("PiKVM Power Button GPIO Test Utility") print("=" * 60) print() # Get GPIO pin from environment or default gpio_pin = int(os.getenv("POWER_BUTTON_GPIO", 17)) print(f"Testing GPIO pin: {gpio_pin}") print("This will briefly activate the GPIO pin (1 second).") print("Make sure this is safe before proceeding!\n") response = input("Continue? (yes/no): ").strip().lower() if response not in ['yes', 'y']: print("Cancelled.") sys.exit(0) print("\n" + "=" * 60) # Try gpiozero first if GPIOZERO_AVAILABLE: success = test_gpio_gpiozero(gpio_pin) if success: print("✓ GPIO is working correctly with gpiozero") print("\nYou can use this pin in your .env configuration:") print(f" POWER_BUTTON_GPIO={gpio_pin}") sys.exit(0) # Fallback to RPi.GPIO if not GPIOZERO_AVAILABLE or not success: print("Trying RPi.GPIO fallback...\n") success = test_gpio_rpi(gpio_pin) if success: print("✓ GPIO is working correctly with RPi.GPIO") print("\nYou can use this pin in your .env configuration:") print(f" POWER_BUTTON_GPIO={gpio_pin}") sys.exit(0) print("\n✗ GPIO test failed") print("\nTroubleshooting:") print("1. Verify the GPIO pin number is correct for your PiKVM") print("2. Check that the GPIO pins are not already in use") print("3. Ensure you're running with appropriate permissions (sudo)") print("4. Check PiKVM documentation for correct pin configuration") sys.exit(1) if __name__ == "__main__": main()