import sys
from encoder import RotaryIRQ
import uasyncio as asyncio
import machine

machine.freq(240000000)
# Use heartbeat to keep event loop not empty
async def heartbeat():
    while True:
        await asyncio.sleep_ms(10)

event = asyncio.Event()


def callback():
    event.set()


async def main():
    r = RotaryIRQ(pin_num_clk=14,
                  pin_num_dt=13)
    r.add_listener(callback)
    
    asyncio.create_task(heartbeat())
    while True:
        await event.wait()
        print('result =', r.value())
        event.clear()

try:
    asyncio.run(main())
except (KeyboardInterrupt, Exception) as e:
    print('Exception {} {}\n'.format(type(e).__name__, e))
finally:
    ret = asyncio.new_event_loop()  # Clear retained uasyncio state