# event_loop_template.py

# Program structure example. For discussion, please see
# https://courses.ideate.cmu.edu/16-223/f2021/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.

# Some boards include a onboard LED on an I/O pin.
# led = digitalio.DigitalInOut(board.D13)
# led.direction = digitalio.Direction.OUTPUT

# ----------------------------------------------------------------
# Initialize global variables for the main loop.

# Specify the integer time stamp for a future event.
next_blink_time = time.monotonic_ns()

# ----------------------------------------------------------------
# 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
    
    # Other timed events could go here, including sensor processing, program
    # logic, or other outputs.

    # At the end, control continues on without delay to the next iteration of
    # 'while True'.

