# state_machine_template.py

# Program structure example. For discussion, please see
# https://courses.ideate.cmu.edu/16-223/s2021/text/code/structure.html

# ----------------------------------------------------------------
# Import any needed standard Python modules.
import time

# Import any needed microcontroller or board-specific library modules.
import board, digitalio

# ----------------------------------------------------------------
# Initialize hardware.

# Assume that an LED output is available.
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT

# Assume that an active-high switch input is available.
switch = digitalio.DigitalInOut(board.D4)
switch.direction = digitalio.Direction.INPUT

# ----------------------------------------------------------------
# Initialize global variables for the main loop.

# The integer time stamp for a future blink event.
next_blink_time = time.monotonic_ns()

# The integer count of blinks to perform.
num_blinks = 0

# The label or index representing the state machine program pointer.
current_state = 'start'

# ----------------------------------------------------------------
# Enter the main event loop.
while True:

    # Read the current integer clock.
    now = time.monotonic_ns()

    # Poll time stamps to decide if specific timed events should occur.
    if now >= next_blink_time:

        # Advance the time stamp to the next event time.
        next_blink_time += 1000000000 # one second in nanosecond units

        # Perform the timed action
        # led.value = not led.value

    # Evaluate the state machine logic.  Only one clause executes per cycle.
    if current_state == 'start':
        pass

    elif current_state == 'blinking':
        pass

    elif current_state == 'waiting':
        pass

    # At the end, control continues on without delay to the next iteration of 'while True'.
