"""
Chapter 2 - Cooperative multitasking using asyncio, page 37
Beningo, Jacob.
MicroPython Projects; A do-it-yourself guide to embedded
developers to build a range of applications using Python.
Packt, 2020
https://github.com/peterhinch/micropython-async/tree/master
https://docs.micropython.org/en/latest/library/asyncio.html
https://randomnerdtutorials.com/micropython-esp32-esp8266-asynchronous-programming/
"""
import asyncio
from machine import Pin
led_blue = Pin(23, Pin.OUT, value= 1)
led_yellow = Pin(22, Pin.OUT, value= 0)
# Define coroutine function
async def task1():
while True:
led_blue.value(not led_blue.value())
await asyncio.sleep_ms(500)
# Define coroutine function
async def task2():
while True:
led_yellow.value(not led_yellow.value())
await asyncio.sleep_ms(500)
# Define the main function to run the event loop
async def main():
# Create tasks for blinking two LEDs concurrently
asyncio.create_task(task1())
asyncio.create_task(task2())
# Create and run the event loop
loop = asyncio.get_event_loop()
loop.create_task(main()) # Create a task to run the main function
loop.run_forever() # Run the event loop indefinitely