#help-with-asyncio
1 messages · Page 1 of 1 (latest)
It was recommended that I make a separate thread for this, rather than requesting help from an individual.
I'll be sure to leave my questions here as I figure out the most appropriate ways to ask them
I'm working on a project where I have a device checking different input states and serial communications.
I wanted to have these inputs do different things depending on which "mode" the device was in.
There's a "Standard mode", and "audio player mode", and an "audio menu mode"(the device doesn't have any displays so audio feedback is used)
I just wanted to lend some contacts to help explain some of my choices.
To help me keep things organized, I thought I'd have three different "main()" (main1(), main2(), main3(), for example) functions and that my program could exit one function and start another depending on which "mode" the device was in.
I've managed to make this happen in two ways reliably. One was by cancelling coroutines and leaving the function using exception handling, I was using .gather() in this case. The other was by having main() hold a nested function and that nested function had a while loop that checked the status of a key.
So something like:
async def nested_func()
while not key.is_set:
# do stuff
So within this function I can have it loop continuously and set its own key whenever I meet certain conditions, like by pressing the correct button or something. Then it would jump back to the main function, and then that main function would also complete
I can post better examples of my actual code a little later on today, but I was curious if this is the best method to go about entering and leaving different functions where you might want to loop until conditions are met.
I'll start sharing examples. I'm not certain what my pins will be soo they'll have generic strings for names, for now.
This is my main code loop
asyncio.run(boot_up())
while "digital pin 1".value:
"digital pin 1".update()
while bb.mode == "Standard Mode":
# run some function
while bb.mode == "Vocal Menu Mode":
# run some function
while bb.mode == "iSaber Mode":
# run some function```
This starts with the boot_up function.
d_pin_1_engage = asyncio.Event()
# the following function takes four arguments: a pin(shown here as the string "digital pin 1",) a function to do when the pins rises, a function for when the pin falls, and an event)
check_d_pin_1 = asyncio.create_task(catch_pin_transitions("digital pin 1", d_pin_1_in, d_pin_1_out, d_pin_1_engage))
# the checking of the pin state here probably isn't needed
if "digital pin 1".value:
leds.duty_cycle = 65535
time.sleep(1)
leds.duty_cycle = 0```
And here's the catch_pin_transitions() function:
while not aevent.is_set():
pin.update()
if pin.rose():
await task_1(aevent)
elif pin.fell():
await task_2(aevent)```
So it keeps running as long as the event isn't set, which only happens if pin.rose():. task_1() in this case takes the event and makes it true. This allows the catch_pin_transitions() func to return and then allows the boot_up() func to return back to the main loop