# mPy - flashing led with USB interface to update led frequency parameter
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2026-02-12  initial release

# on FKS0001 connect IO27 with GREEN

import	time
from	machine	import	Pin
from	drivers	import	nbInput

# Implemented:
# Controller()
#    Controller.taskLed()
#    Controller.taskUsb()

# ============================== Controller ============================

class Controller:
    
    def __init__(self):
        self.led        = Pin(27, Pin.OUT, value=0)
        self.led_dly    = 500							# mSec
        self.led_toggle = time.ticks_ms()				# time for next toggle
        while True:
            self.taskLed()
            self.taskUsb()
        
       
    # toggle led at self.led_dly
    def taskLed(self):
        now = time.ticks_ms()
        if now >= self.led_toggle:
            self.led_toggle = time.ticks_add(now, self.led_dly)
            self.led.toggle()
            
            
    # accept integer 100..10000 [mSec] through USB to update self.led_dly and print 'ok' or 'error'
    def taskUsb(self):
        if s_rx := nbInput():
            try:
                dly = int(s_rx)
                assert 100 <= dly <= 10000
                self.led_dly 	= dly
                self.led_toggle = time.ticks_add(time.ticks_ms(), self.led_dly)		# reset delay
                print('ok')
            except: print('error')


Controller()

# ================================ end ==============================
