|
| 1 | +# Makecode Extension to enable power management on micro:bit (V2) |
1 | 2 |
|
2 | | -## Use as Extension |
| 3 | +Use this extension to add all the blocks you will need to power the micro:bit on and off in your program when you are using the [latest micro:bit](https://microbit.org/new-microbit/). |
3 | 4 |
|
4 | | -This repository can be added as an **extension** in MakeCode. |
| 5 | +This extension might be useful when you want to conserve battery power, such as during a data logging activity. |
5 | 6 |
|
6 | | -* open [https://makecode.microbit.org/](https://makecode.microbit.org/) |
7 | | -* click on **New Project** |
8 | | -* click on **Extensions** under the gearwheel menu |
9 | | -* search for **https://github.com/microbit-foundation/pxt-power** and import |
| 7 | +## Usage |
10 | 8 |
|
11 | | -## Edit this project  |
| 9 | +### Put the micro:bit to sleep 💤 |
12 | 10 |
|
13 | | -To edit this repository in MakeCode. |
| 11 | +To make the micro:bit sleep, you need to send a request to power it down. The ``||power.powerDownRequest||`` block will ask the micro:bit to power down at the next opportunity, such as when the current code operation has been allowed to complete. |
14 | 12 |
|
15 | | -* open [https://makecode.microbit.org/](https://makecode.microbit.org/) |
16 | | -* click on **Import** then click on **Import URL** |
17 | | -* paste **https://github.com/microbit-foundation/pxt-power** and click import |
| 13 | +```blocks |
| 14 | +input.onButtonPressed(Button.B, function () { |
| 15 | + power.powerDownRequest() |
| 16 | +}) |
| 17 | +``` |
18 | 18 |
|
19 | | -## Blocks preview |
| 19 | +You can also ask the micro:bit to enter a ``||power.deepSleep||`` where it will pause until a wake up event occurs and power down at the next opportunity. |
20 | 20 |
|
21 | | -This image shows the blocks code from the last commit in master. |
22 | | -This image may take a few minutes to refresh. |
| 21 | +The ``||power.deepSleepPause(ms)||`` block will also ask the micro:bit to sleep for a set interval in milliseconds. |
23 | 22 |
|
24 | | - |
| 23 | +You can also use the ``||PowerDown.prevent||`` and ``||PowerDown.allow||`` blocks to block a power down request until the code inside the two blocks has finished running. It is expected that you would use these blocks in pairs. |
| 24 | + |
| 25 | +```blocks |
| 26 | +basic.forever(function () { |
| 27 | + power.powerDownEnable(PowerDown.prevent) |
| 28 | + led.plot(2, 2) |
| 29 | + basic.pause(1000) |
| 30 | + led.unplot(2, 2) |
| 31 | + led.plot(2, 1) |
| 32 | + basic.pause(1000) |
| 33 | + led.unplot(2, 1) |
| 34 | + power.powerDownEnable(PowerDown.allow) |
| 35 | + power.powerDownRequest() |
| 36 | +}) |
| 37 | +``` |
| 38 | + |
| 39 | +### Wake the micro:bit from sleep ⏰ |
| 40 | + |
| 41 | +In order to wake the micro:bit, you need to define an event to trigger the wake up call. |
| 42 | + |
| 43 | +You can wake the micro:bit when a button or pin is pressed. In this example, the micro:bit will wake up when Button A or Pin 0 has been pressed. |
| 44 | + |
| 45 | +```blocks |
| 46 | +power.wakeOnEnable(PowerWakeup.A) |
| 47 | +power.wakeOnEnable(PowerWakeup.P0) |
| 48 | +``` |
| 49 | + |
| 50 | +You can also wake the micro:bit at a set time interval in milliseconds. In this example, the micro:bit will wake up every minute and show a smiley face on the screen |
| 51 | + |
| 52 | +```blocks |
| 53 | +power.wakeEvery(60000, function () { |
| 54 | + basic.showIcon(IconNames.Happy) |
| 55 | + basic.clearScreen() |
| 56 | +}) |
| 57 | +``` |
25 | 58 |
|
26 | 59 | ## License |
27 | 60 |
|
|
0 commit comments