"""Print the limit switch state of every connected board once a second. Ctrl-C to quit.

The switches are wired normally closed, so a board also reports PRESSED when nothing is wired to it.
"""
import time
from utils import find_devices, print_connected


def main():
    devices = find_devices()
    if not devices:
        print("No boards found. Check the USB cables (ls /dev/cu*).")
        return
    print_connected(devices)
    print()
    try:
        while True:
            parts = []
            for mid in sorted(devices):
                _, client = devices[mid]
                try:
                    state = "PRESSED" if client.get_states().limit else "open"
                except Exception as e:
                    state = f"error ({e})"
                parts.append(f"motor {mid}: {state}")
            print(" | ".join(parts))
            time.sleep(1)
    except KeyboardInterrupt:
        print("\nExiting")
    finally:
        for _, client in devices.values():
            client.close()


if __name__ == "__main__":
    main()
