Skip to content

Commit 2a38b7a

Browse files
docs: Update Power Mgm to change run_every behaviour. (#769)
* docs: Update Power Mgm to change run_every behaviour. From waking up the board when the next function is scheduled, to allow the scheduled functions to continue running indefinitely while the boards sleeps. * docs: Tweak Power Mgm run_every description & indicate UART wakeup.
1 parent cbcd4a9 commit 2a38b7a

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

docs/power.rst

+20-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ There are two micro:bit board low power modes that can be requested from
99
MicroPython:
1010

1111
- **Deep Sleep**: Low power mode where the board can be woken up via
12-
multiple sources (pins, button presses, or a timer) and resume
12+
multiple sources (pins, button presses, uart data, or a timer) and resume
1313
operation.
1414
- **Off**: The power mode with the lowest power consumption, the only way to
1515
wake up the board is via the reset button, or by plugging the USB cable while
@@ -37,7 +37,7 @@ Functions
3737
will start running from the beginning.
3838

3939

40-
.. py:function:: deep_sleep(ms=None, wake_on=None, run_every=False)
40+
.. py:function:: deep_sleep(ms=None, wake_on=None, run_every=True)
4141
4242
Set the micro:bit into a low power mode where it can wake up and continue
4343
operation.
@@ -49,16 +49,21 @@ Functions
4949

5050
The wake up sources are configured via arguments.
5151

52-
If no wake up sources have been configured it will sleep until the reset
52+
The board will always wake up when receiving UART data, when the reset
5353
button is pressed (which resets the board) or, in battery power,
5454
when the USB cable is inserted.
5555

56+
When the ``run_every`` parameter is set to ``True`` (the default), any
57+
function scheduled with :py:meth:`microbit.run_every<microbit.run_every>`
58+
will momentarily wake up the board to run and when it finishes it will go
59+
back to sleep.
60+
5661
:param ms: A time in milliseconds to wait before it wakes up.
5762
:param wake_on: A single instance or a tuple of pins and/or buttons to
5863
wake up the board, e.g. ``deep_sleep(wake_on=button_a)`` or
5964
``deep_sleep(wake_on=(pin0, pin2, button_b))``.
60-
:param run_every: Set to ``True`` to wake up with each
61-
``microbit.run_every`` scheduled run.
65+
:param run_every: A boolean to configure if the functions scheduled with
66+
``microbit.run_every`` will continue to run while it sleeps.
6267

6368
Examples
6469
========
@@ -124,3 +129,13 @@ the USB connection.
124129
+------------------+-----------------+--------------------+
125130
| **Off** | 📴 Power Down | 📴 Power Down |
126131
+------------------+-----------------+--------------------+
132+
133+
Deep Sleep & run_every
134+
----------------------
135+
136+
To make sure the :py:meth:`microbit.run_every<microbit.run_every>`
137+
functions continue to run during "Deep Sleep", the micro:bit will wake up
138+
at the correct time to run the next scheduled ``run_every``,
139+
and then go back to "Deep Sleep" mode as soon as that ``run_every`` completes.
140+
It will continue to do this until the deep sleep finishes due
141+
to the ``ms`` timeout being reached, or a ``wake_on`` event occurs.

examples/datalog-sleep.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import log
44

55
# Log the temperature every 5 minutes
6-
@run_every(s=5)
6+
@run_every(min=5)
77
def log_temperature():
88
log.add(temp=temperature())
99

1010
while True:
11+
# Display the temperature when button A is pressed
1112
if button_a.is_pressed():
12-
# Display the temperature when button A is pressed
1313
display.scroll(temperature())
14-
# To go sleep and wake up with run_every or button A
14+
# To go sleep, wake up when button A is pressed, and ensure the
15+
# function scheduled with run_every still executes in the background
1516
power.deep_sleep(wake_on=button_a, run_every=True)

0 commit comments

Comments
 (0)