# led.py

# Raspberry Pi Pico - LED Blinker

# Blink controller for the onboard LED, intended to run inside an event loop system.

import board
import digitalio

class Blinker:
    def __init__(self):
        """Interface to the onboard LED featuring variable rate blinking."""
        # Set up built-in green LED for output.
        self.led = digitalio.DigitalInOut(board.LED)  # GP25
        self.led.direction = digitalio.Direction.OUTPUT
        self.update_timer = 0
        self.set_rate(1.0)
            
    def poll(self, elapsed):
        """Polling function to be called as frequently as possible from the event loop
        with the nanoseconds elapsed since the last cycle."""
        self.update_timer -= elapsed
        if self.update_timer < 0:
            self.update_timer += self.update_interval
            self.led.value = not self.led.value

    def set_rate(self, Hz):
        self.update_interval = int(500000000 / Hz) # blink half-period in nanoseconds
        
