#!/usr/bin/env python3
"""Control one Cytorbit LED through the transistor circuit in the guide.

Raspberry Pi 3 A+, BCM GPIO17 (physical header pin 11).
GPIO17 -> 1 kohm -> 2N3904 base; 100 kohm base-to-emitter.
Pi 5 V -> 220 ohm -> LED anode; LED cathode -> collector.
Emitter -> Pi GND. Never connect the 5 V LED wire to GPIO17.
Run on Raspberry Pi OS: python3 led_control.py
"""

import signal

from gpiozero import DigitalOutputDevice


def stop(_signum, _frame):
    """Allow a normal SIGTERM to use the same cleanup as Ctrl+C."""
    raise KeyboardInterrupt


def main():
    signal.signal(signal.SIGTERM, stop)
    lamp = DigitalOutputDevice(17, active_high=True, initial_value=False)
    try:
        print("LED off. Type on, off or quit; keep this program running.")
        while True:
            command = input("LED> ").strip().lower()
            if command == "on":
                lamp.on()
                print("ON")
            elif command == "off":
                lamp.off()
                print("OFF")
            elif command in ("quit", "exit", "q"):
                break
            else:
                print("Use on, off or quit.")
    except (EOFError, KeyboardInterrupt):
        pass
    finally:
        lamp.off()
        lamp.close()
        print("\nLED off; GPIO released.")


if __name__ == "__main__":
    main()
