"""Step 2: the strike and the status LED.

Nothing opens it yet: the door opens for 5 seconds every 10 seconds, on its own.
The strike is the Pico's own LED. The bicolor LED is red when closed,
green when open.
"""

from machine import Pin
import time

OPEN_MS = 5000
CYCLE_MS = 10000

red = Pin(14, Pin.OUT, value=0)
green = Pin(15, Pin.OUT, value=0)
strike = Pin(25, Pin.OUT, value=0)


class Door:
    """Closed, or open until a given time. The strike follows it."""

    def __init__(self):
        self.open_until = time.ticks_ms()

    def open_for(self, milliseconds):
        self.open_until = time.ticks_add(time.ticks_ms(), milliseconds)

    def is_open(self):
        return time.ticks_diff(self.open_until, time.ticks_ms()) > 0


door = Door()
last_cycle = time.ticks_ms()

while True:
    if time.ticks_diff(time.ticks_ms(), last_cycle) >= CYCLE_MS:
        last_cycle = time.ticks_ms()
        door.open_for(OPEN_MS)
        print("Door opens for", OPEN_MS // 1000, "s")

    strike.value(door.is_open())
    red.value(not door.is_open())
    green.value(door.is_open())
    time.sleep_ms(10)
