import ufp
import time
from utils import ExtrusionStepper, find_devices, print_connected

MOTOR_IDS = (1, 2, 3, 4, 5) # plain, black, red
MOTORS : list[ExtrusionStepper] = []

def main():
    devices = find_devices().items()
    for mid, (port, client) in devices: 
        if mid in MOTOR_IDS:
            # reuse the connection find_devices already opened
            MOTORS.append(ExtrusionStepper(port, 0.99, client=client))
        else:
            client.close()
            
    print_connected(m.m.get_id() for m in MOTORS)
    try:
        while True:
            for motor in MOTORS:
                print("turning", motor.m.get_id(), "once")
                motor._turn_n(1)
                time.sleep(1)
                motor._turn_n(-1)
                print()
    except KeyboardInterrupt:
        print("\nExiting")
    finally:
        for motor in MOTORS:
            motor.close()

if __name__ == "__main__":
    main()