#!/usr/bin/env python3
"""
Emergency cleanup script - forces all GPIO pins LOW
Run this if your main program crashes and leaves pins high
"""

import lgpio
from config import PINS

def force_pins_low():
    """Force all control pins to LOW state"""
    try:
        chip = lgpio.gpiochip_open(0)
        
        # Claim and set all pins LOW
        for pin_name, pin_num in PINS.items():
            if pin_name != "PPS":  # Skip PPS (it's input only)
                lgpio.gpio_claim_output(chip, pin_num)
                lgpio.gpio_write(chip, pin_num, 0)
                print(f"[CLEANUP] {pin_name} (pin {pin_num}) set to LOW")
        
        lgpio.gpiochip_close(chip)
        print("[CLEANUP] All pins forced LOW successfully")
        
    except Exception as e:
        print(f"[CLEANUP] Error: {e}")
        print("You may need to run with sudo or check if another process is using the pins")

if __name__ == "__main__":
    print("[CLEANUP] Emergency GPIO cleanup starting...")
    force_pins_low()
    print("[CLEANUP] Done")