Replies: 3 comments 3 replies
-
This is very nice and would be great for someone learning Morse! I do have some comments about your use of I would rewrite your example as follows: def ex():
morse = AMorse("73 de iv3wjr", wpm=15, speaker=Pin(16))
async def read(morse):
while True:
await morse.wait()
answ = input("Send more? ")
if answ:
morse.append(answ)
else:
break
async def forever():
send_task = asyncio.create_task(morse.sender())
await read(morse)
send_task.cancel()
await asyncio.sleep(0) # Let cancellation occur
asyncio.run(forever()) |
Beta Was this translation helpful? Give feedback.
-
Thank you Peter for your precious comment, it is for me a real pleasure to read your rewrite of my example. I promise that I will read with more attention the tutorial but reading how your code should be written is an unforgettable lesson. Really thank you for your time. Unfortunately I noticed that in real use my AMorse.sender method does not behave as I expected if the rest of the code is not asynchronous. If the main program for example is waiting for a urequests.get call and the sender method is "sending" morse code, the timing is (obviously) incorrect. Some dots and/or dashes are longer and the resulting sound is unsatisfactory. But to my knowledge the is no way to await an urequests.get. Working on a Raspberry PICO W however, I have two cores available so now I'm trying to run the sender method (without async) in a separate thread. I understand that uasyncio is better than _thread for many already discussed reasons but at present I have no other ideas to solve this problem. |
Beta Was this translation helpful? Give feedback.
-
Functions or methods that block are the enemy of async def unblock(func, *args, **kwargs):
def wrap(func, message, args, kwargs):
message.set(func(*args, **kwargs)) # Run the blocking function.
msg = Message()
_thread.start_new_thread(wrap, (func, msg, args, kwargs))
return await msg See this ref for documentation. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi to all,
developing an application for my Raspberry PICO W with no display I added visual feedback of the program behavior.
Being a ham I wrote a Morse class to flash the onboard LED (and sound optionally an 8 Ohm speaker) with a message. Now I am happy to share it with the community that helped me so many times.
The ex function is a usage example.
I use it to know when the application connects to WiFi, mounts the SD card , downloads data from the server and to monitor its correct operation.
The speaker I use is a small 0.2 W 8 Ohm connected between GND and pin 16 on my PICO W through a 270 Ohm resistor.
speaker.duty_u16(1024) results in a weak sound, use greater values to make it louder.
Comments and suggestions are of course welcome.
73 de IV3WJR Bob
Beta Was this translation helpful? Give feedback.
All reactions