# mPy - Toggle Leds on button press on FSK0001
#	and control using Micrdot ready framework
# Copyright by Anthony Vogelaar, Perth Scotland
# revision 0.10  2025-11-29  initial release

from	machine				import	Pin, freq;		freq(240_000_000)
import	asyncio
import	time, json
# ====================== domain =======================

pins_led = (25, 26, 27, 14)
pins_btn = (33, 32, 35, 34)

leds = [Pin(pin, Pin.OUT, value=0)          for pin in pins_led]
btns = {Pin(pin, Pin.IN, Pin.PULL_UP): True for pin in pins_btn}

# return leds status as list
def getLeds():
    return [led() for led in leds]


def toggleLed(d):
    btn_name = d.get('data')
    if len(btn_name) == 4:
        idx = int(btn_name[3])
        leds[idx].toggle()


def domainExe(S={'t_last':time.ticks_ms()}):
    now = time.ticks_ms()
    if time.ticks_diff(now, S['t_last']) > 100:
        S['t_last'] = now
        for i, (k, v) in enumerate(btns.items()):		# iterate through buttons @ 100 mSec
            level = k()									# current status
            if level != v:								# if status changed
                btns[k] = level							# save current status
                if not level:							# if btn down
                    leds[i].toggle()
                    return True							# led(s) modified
    
# ======================== main =========================

async def main():
    pin_test = Pin(12, Pin.OUT)
    while True:
        pin_test(1); pin_test(0)		# typ=500uS, long=800uS max=1700uS
        if domainExe():
            ...
        await asyncio.sleep(0)
        
        
asyncio.run(main())

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