from machine import PWM, Pin
from time import sleep
from servo import Servo
from encoder import RotaryEncoder

servo1 = Servo(12)
servo2 = Servo(14)
servo3 = Servo(25)
servo4 = Servo(26)

pir_sensor = Pin(34, Pin.IN)

def rotation_handler(direction):
    step = 9
    if direction == RotaryEncoder.ROT_CW:
        servo1.rotate(step)
        servo2.rotate(-step)
        servo3.rotate(step)
        servo4.rotate(-step)
    else:
        servo1.rotate(-step)
        servo2.rotate(step)
        servo3.rotate(-step)
        servo4.rotate(step)

myEncoder = RotaryEncoder(27, 33, rotation_handler)

default_angle1 = 90
default_angle2 = 90
default_angle3 = 90
default_angle4 = 90

def main():
    servo1.move(default_angle1)
    servo2.move(default_angle2)
    servo3.move(default_angle3)
    servo4.move(default_angle4)
    sleep(0.1)

if __name__ == '__main__':
    main()
    while True:
        if pir_sensor.value() == 1:
            servo1.move(0)
            servo2.move(180)
            servo3.move(45)
            servo4.move(135)
        else:
            servo1.move(default_angle1)
            servo2.move(default_angle2)
            servo3.move(default_angle3)
            servo4.move(default_angle4)
        sleep(0.1)