Here is a simple question:
When calling an await uasyncio.sleep(), does this put the MCU in idle?
Putting an explicit machine.idle() right above an await uasyncio.sleep() line will result in the latter never to be executed. So, that is not a solution to save some power.
machine.idle() during await uasyncio.sleep()?
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: machine.idle() during await uasyncio.sleep()?
No, the scheduler continues to run. If one task issues a sleep the presumption is that other tasks are also queued for scheduling and may be granted execution. For example another task's sleep may come to an end first.
A power-saving adaptation to uasyncio is an interesting proposition, but it would need some serious thought
However if your application can sleep for extended periods you could design it so that the scheduler stops (run_until_complete), the unit goes to sleep, with wakeup restarting the scheduler.
[EDIT]
I've given a little thought to the idea of a power saving scheduler. One problem is the IORead mechanism. Pending coroutines are stored in a queue sorted on time due. So the task at the top of the queue is always the first to be scheduled. The scheduler checks the time due of the topmost task and waits for that period. However the method which does this waiting (EventLoop.wait) is handled by the PollEventLoop subclass which polls stream I/O devices. So the C code which does this (moduselect.c) would need to be adapted.
Alternatively if an application did not require the IORead mechanism EventLoop could be subclassed with a modified wait method which would use a low power mode.
A power-saving adaptation to uasyncio is an interesting proposition, but it would need some serious thought
However if your application can sleep for extended periods you could design it so that the scheduler stops (run_until_complete), the unit goes to sleep, with wakeup restarting the scheduler.
[EDIT]
I've given a little thought to the idea of a power saving scheduler. One problem is the IORead mechanism. Pending coroutines are stored in a queue sorted on time due. So the task at the top of the queue is always the first to be scheduled. The scheduler checks the time due of the topmost task and waits for that period. However the method which does this waiting (EventLoop.wait) is handled by the PollEventLoop subclass which polls stream I/O devices. So the C code which does this (moduselect.c) would need to be adapted.
Alternatively if an application did not require the IORead mechanism EventLoop could be subclassed with a modified wait method which would use a low power mode.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: machine.idle() during await uasyncio.sleep()?
That would indeed be a more clever solution.pythoncoder wrote: ↑Mon Jan 22, 2018 5:33 amHowever if your application can sleep for extended periods you could design it so that the scheduler stops (run_until_complete), the unit goes to sleep, with wakeup restarting the scheduler.
It is still very tricky, though. Having the Pyboard lingering in machine.idle() broke my USB connection without any opportunity to connect again using rshell.
Eventually, I had to put the Pyboard into safe mode.
As an absolute beginner, I am really missing seeing some more examples of uasyncio or threading loops for that matter.
Serge
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: machine.idle() during await uasyncio.sleep()?
For a simple start with uasyncio try this script which flashes the four Pyboard LED's at arbitrary independent frequencies for a period, then quits.
Code: Select all
import pyb
import uasyncio as asyncio
async def killer(duration):
await asyncio.sleep(duration)
async def toggle(objLED, time_ms):
while True:
await asyncio.sleep_ms(time_ms)
objLED.toggle()
def test(duration):
loop = asyncio.get_event_loop()
duration = int(duration)
if duration > 0:
print("Flash LED's for {:3d} seconds".format(duration))
leds = [pyb.LED(x) for x in range(1,5)] # Initialise all four on board LED's
for x, led in enumerate(leds): # Create a coroutine for each LED
t = int((0.2 + x/2) * 1000)
loop.create_task(toggle(leds[x], t))
loop.run_until_complete(killer(duration))
loop.close()
test(10)
I plan to update my tutorial to improve the beginners' section.
Incidentally the interactions between low power modes and USB communication are nothing to do with uasyncio. There is an introduction to the Pyboard's low power modes here.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: machine.idle() during await uasyncio.sleep()?
I am trying tu run the latest asyncio library from PyPi with my pyboard v1.1 and get the error
I found your note about the firmware version
Where can download it?
Code: Select all
Traceback (most recent call last):
File "main.py", line 24, in <module>
File "main.py", line 13, in test
File "uasyncio/core.py", line 224, in get_event_loop
File "uasyncio/__init__.py", line 21, in __init__
File "uasyncio/core.py", line 30, in __init__
AttributeError: 'module' object has no attribute 'deque'
MicroPython v1.9.3 on 2017-11-01; PYBv1.1 with STM32F405RG
Type "help()" for more information.
On the download page I can not find the 2018 year firmware version for the pyboard.The documentation and code in this repository are based on uasyncio version 2.0, which is the version on PyPi. This requires firmware dated 22nd Feb 2018 or later.
Where can download it?
Re: machine.idle() during await uasyncio.sleep()?
nevermind. compiled the latest git firmware version and everything is working
Code: Select all
[INFO] boot.py
Flash LED's for 10 seconds
MicroPython v1.9.3-469-g5edce453 on 2018-03-16; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>>
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: machine.idle() during await uasyncio.sleep()?
The builds on the download page marked (latest) are daily builds so should work fine.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.