Skip to content

Commit 43447bb

Browse files
authored
Merge pull request #6 from martinwork/low-full
Change terminology: deep sleep, power down and wake to low/full power
2 parents f5c6adc + 531ab1c commit 43447bb

File tree

5 files changed

+281
-216
lines changed

5 files changed

+281
-216
lines changed

README.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,62 @@
11
# Makecode Extension to enable power management on micro:bit (V2)
22

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+
Use this extension to add all the blocks you will need to use less power in your program when you are using the [latest micro:bit](https://microbit.org/new-microbit/).
44

55
This extension might be useful when you want to conserve battery power, such as during a data logging activity.
66

7+
78
## Usage
89

9-
### Put the micro:bit to sleep 💤
10+
### Put the micro:bit to sleep in a low power mode 💤
1011

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.
12+
The ``||power.lowPowerRequest||`` block will ask the micro:bit to switch to low power mode at the next opportunity, such as when the current code operation has been allowed to complete, or inside ``||basic.pause(ms)||``.
1213

1314
```blocks
1415
input.onButtonPressed(Button.B, function () {
15-
power.powerDownRequest()
16+
power.lowPowerRequest()
1617
})
1718
```
1819

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+
You can send ``||power.lowPowerRequest(LowPowerMode.Wait)||``. Then micro:bit will also pause until a full power event occurs.
21+
22+
The ``||power.lowPowerPause(ms)||`` block will ask the micro:bit to sleep for a set interval in milliseconds.
2023

21-
The ``||power.deepSleepPause(ms)||`` block will also ask the micro:bit to sleep for a set interval in milliseconds.
24+
In low power mode, the micro:bit is asleep, and your program is paused. When the micro:bit wakes up to full power mode, your program continues from the point it stopped.
2225

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.
26+
You can use the ``||power.lowPowerEnable(PowerDown.prevent)||`` and ``||power.lowPowerEnable(PowerDown.allow)||`` blocks to block low power requests until the code between the two blocks has finished running. It is expected that you would use these blocks in pairs.
2427

2528
```blocks
2629
basic.forever(function () {
27-
power.powerDownEnable(PowerDown.prevent)
30+
power.lowPowerEnable(PowerDown.prevent)
2831
led.plot(2, 2)
2932
basic.pause(1000)
3033
led.unplot(2, 2)
3134
led.plot(2, 1)
3235
basic.pause(1000)
3336
led.unplot(2, 1)
34-
power.powerDownEnable(PowerDown.allow)
35-
power.powerDownRequest()
37+
power.lowPowerEnable(PowerDown.allow)
38+
power.lowPowerRequest()
3639
})
3740
```
3841

39-
### Wake the micro:bit from sleep
42+
### Wake up the micro:bit to full power mode
4043

41-
In order to wake the micro:bit, you need to define an event to trigger the wake up call.
44+
In order to wake up the micro:bit to full power mode, you need to define an event to trigger the wake up call.
4245

4346
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.
4447

4548
```blocks
46-
power.wakeOnEnable(PowerWakeup.A)
47-
power.wakeOnEnable(PowerWakeup.P0)
49+
power.fullPowerOn(FullPowerSource.A)
50+
power.fullPowerOn(FullPowerSource.P0)
4851
```
4952

5053
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
5154

5255
```blocks
53-
power.wakeEvery(60000, function () {
56+
power.fullPowerEvery(60000, function () {
5457
basic.showIcon(IconNames.Happy)
5558
basic.clearScreen()
59+
power.lowPowerRequest()
5660
})
5761
```
5862

power.cpp

Lines changed: 113 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -18,58 +18,77 @@
1818
#endif // MICROBIT_CODAL
1919

2020

21-
enum class PowerWakeup {
21+
enum class FullPowerSource {
22+
//% block="button A"
2223
A = MICROBIT_ID_BUTTON_A,
24+
//% block="button B"
2325
B = MICROBIT_ID_BUTTON_B,
26+
//% block="pin P0"
2427
P0 = MICROBIT_ID_IO_P0,
28+
//% block="pin P1"
2529
P1 = MICROBIT_ID_IO_P1,
30+
//% block="pin P2"
2631
P2 = MICROBIT_ID_IO_P2
2732
};
2833

29-
enum class PowerDown {
30-
prevent,
31-
allow
34+
enum class LowPowerMode {
35+
//% block="continue"
36+
Continue = 0,
37+
//% block="wait"
38+
Wait = 1
39+
};
40+
41+
enum class LowPowerEnable {
42+
//% block="prevent"
43+
Prevent,
44+
//% block="allow"
45+
Allow
3246
};
3347

3448

49+
//% block="Power"
50+
//% icon="\uf011"
51+
//% color=#AA278D
3552
namespace power {
3653

3754
#if MICROBIT_CODAL
3855
int timerEventValue = 1;
3956
#endif // MICROBIT_CODAL
4057

41-
/**
42-
* Pause until a wake up event occurs, and request power down when idle.
43-
*/
44-
//%
45-
void deepSleep() {
46-
#if MICROBIT_CODAL
47-
uBit.power.deepSleep();
48-
#else
49-
uBit.sleep(0);
50-
#endif
51-
}
52-
58+
void lowPowerRequest(LowPowerMode mode = LowPowerMode::Continue);
5359

5460
/**
55-
* Request power down when idle, and return immediately.
61+
* Request low power when the next idle
62+
* @param mode If Continue, then return immediately; if Wait, then pause until a power-up event occurs
5663
*/
64+
//% help=power/low-power-request
65+
//% group="micro:bit (V2)"
66+
//% weight=700
67+
//% block="request low power||and $mode"
5768
//%
58-
void powerDownRequest() {
69+
void lowPowerRequest(LowPowerMode mode) {
5970
#if MICROBIT_CODAL
60-
uBit.power.deepSleepAsync();
71+
if ( mode == LowPowerMode::Wait)
72+
uBit.power.deepSleep();
73+
else
74+
uBit.power.deepSleepAsync();
6175
#else
6276
uBit.sleep(0);
6377
#endif
64-
}
78+
}
6579

6680

6781
/**
68-
* Pause for a fixed interval, and request power down when idle.
82+
* Pause for a fixed interval, and request low power when idle.
6983
* @param interval The period of time to pause, in milliseconds.
7084
*/
85+
//% help=power/low-power-for
86+
//% group="micro:bit (V2)"
87+
//% weight=600
88+
//% interval.shadow=longTimePicker
89+
//% block="request low power for $interval ms"
7190
//%
72-
void deepSleepPause(unsigned interval) {
91+
void lowPowerPause(int interval) {
7392
#if MICROBIT_CODAL
7493
uBit.power.deepSleep(interval);
7594
#else
@@ -79,38 +98,23 @@ void deepSleepPause(unsigned interval) {
7998

8099

81100
/**
82-
* Do something repeatedy using a wake-up timer.
83-
* @param interval time (in ms) for the timer.
84-
* @param body code to execute
85-
*/
86-
//%
87-
void wakeEvery(unsigned interval, Action body) {
88-
#if MICROBIT_CODAL
89-
registerWithDal( MICROBIT_ID_MAKECODE_POWER, timerEventValue, body);
90-
// CODAL_TIMER_EVENT_FLAGS_WAKEUP makes the timer event trigger power up
91-
system_timer_event_after( 0, MICROBIT_ID_MAKECODE_POWER, timerEventValue, CODAL_TIMER_EVENT_FLAGS_WAKEUP);
92-
system_timer_event_every( interval, MICROBIT_ID_MAKECODE_POWER, timerEventValue++, CODAL_TIMER_EVENT_FLAGS_WAKEUP);
93-
#else
94-
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
95-
#endif
96-
}
97-
98-
99-
/**
100-
* Prevent or allow power down during deepSleep.
101+
* Prevent or allow low power.
101102
* Prevent and allow requests should occur in pairs.
102103
* The default is to allow.
103104
*/
105+
//% help=power/low-power-enable
106+
//% weight=500
107+
//% block="low power %enable"
104108
//%
105-
void powerDownEnable(PowerDown choice) {
109+
void lowPowerEnable(LowPowerEnable enable) {
106110
#if MICROBIT_CODAL
107-
switch ( choice)
111+
switch ( enable)
108112
{
109-
case PowerDown::prevent:
113+
case LowPowerEnable::Prevent:
110114
uBit.power.powerDownDisable();
111115
break;
112116

113-
case PowerDown::allow:
117+
case LowPowerEnable::Allow:
114118
uBit.power.powerDownEnable();
115119
break;
116120

@@ -122,10 +126,11 @@ void powerDownEnable(PowerDown choice) {
122126

123127

124128
/**
125-
* Determine if power down during deepSleep is enabled
129+
* Determine if low power is enabled
126130
*/
131+
//% help=power/low-power-is-enabled
127132
//%
128-
bool powerDownIsEnabled() {
133+
bool lowPowerIsEnabled() {
129134
#if MICROBIT_CODAL
130135
return uBit.power.powerDownIsEnabled();
131136
#else
@@ -135,29 +140,55 @@ bool powerDownIsEnabled() {
135140

136141

137142
/**
138-
* Set whether the source should trigger power save wake-up.
143+
* Do something repeatedy with full power using a timer.
144+
* @param interval the time (in ms) for the timer.
145+
* @param code the code to execute
146+
*/
147+
//% help=power/full-power-every
148+
//% group="micro:bit (V2)"
149+
//% weight=800
150+
//% blockAllowMultiple=1
151+
//% interval.shadow=longTimePicker
152+
//% afterOnStart=true
153+
//% block="full power every $interval ms"
154+
//%
155+
void fullPowerEvery(int interval, Action code) {
156+
#if MICROBIT_CODAL
157+
registerWithDal( MICROBIT_ID_MAKECODE_POWER, timerEventValue, code);
158+
// CODAL_TIMER_EVENT_FLAGS_WAKEUP makes the timer event trigger power up
159+
system_timer_event_after( 0, MICROBIT_ID_MAKECODE_POWER, timerEventValue, CODAL_TIMER_EVENT_FLAGS_WAKEUP);
160+
system_timer_event_every( interval, MICROBIT_ID_MAKECODE_POWER, timerEventValue++, CODAL_TIMER_EVENT_FLAGS_WAKEUP);
161+
#else
162+
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
163+
#endif
164+
}
165+
166+
167+
/**
168+
* Set whether the source should trigger full power.
139169
* @param source the source to set
140-
* @param wake true to trigger wake-up or false for no wake-up
170+
* @param enable true to trigger full power
141171
*/
172+
//% help=power/full-power-source-enable
142173
//%
143-
void wakeOn(PowerWakeup source, bool wake) {
174+
void fullPowerSourceEnable(FullPowerSource source, bool enable) {
144175
#if MICROBIT_CODAL
145176
switch ( source)
146177
{
147-
case PowerWakeup::A:
148-
uBit.buttonA.wakeOnActive(wake ? 1 : 0);
178+
case FullPowerSource::A:
179+
uBit.buttonA.wakeOnActive(enable ? 1 : 0);
149180
break;
150181

151-
case PowerWakeup::B:
152-
uBit.buttonB.wakeOnActive(wake ? 1 : 0);
182+
case FullPowerSource::B:
183+
uBit.buttonB.wakeOnActive(enable ? 1 : 0);
153184
break;
154185

155-
case PowerWakeup::P0:
156-
case PowerWakeup::P1:
157-
case PowerWakeup::P2:
186+
case FullPowerSource::P0:
187+
case FullPowerSource::P1:
188+
case FullPowerSource::P2:
158189
{
159190
MicroBitPin *pin = getPin((int)source);
160-
pin->wakeOnActive(wake ? 1 : 0);
191+
pin->wakeOnActive(enable ? 1 : 0);
161192
break;
162193
}
163194
default:
@@ -168,35 +199,27 @@ void wakeOn(PowerWakeup source, bool wake) {
168199

169200

170201
/**
171-
* Set the source to trigger power save wake-up.
172-
* @param source the source to set
173-
*/
174-
//%
175-
void wakeOnEnable(PowerWakeup source) {
176-
wakeOn(source, true);
177-
}
178-
179-
/**
180-
* Determine if the source will trigger power save wake-up.
181-
* @param source the source to set
182-
* @return true is wake-up is enabled
202+
* Determine if the source will trigger full power.
203+
* @param source the source to check
204+
* @return true if the source will trigger full power
183205
*/
206+
//% help=power/full-power-source-is-enabled
184207
//%
185-
bool wakeOnIsEnabled(PowerWakeup source) {
208+
bool fullPowerSourceIsEnabled(FullPowerSource source) {
186209
#if MICROBIT_CODAL
187210
switch ( source)
188211
{
189-
case PowerWakeup::A:
212+
case FullPowerSource::A:
190213
return uBit.buttonA.isWakeOnActive() ? true : false;
191214
break;
192215

193-
case PowerWakeup::B:
216+
case FullPowerSource::B:
194217
return uBit.buttonB.isWakeOnActive() ? true : false;
195218
break;
196219

197-
case PowerWakeup::P0:
198-
case PowerWakeup::P1:
199-
case PowerWakeup::P2:
220+
case FullPowerSource::P0:
221+
case FullPowerSource::P1:
222+
case FullPowerSource::P2:
200223
{
201224
MicroBitPin *pin = getPin((int)source);
202225
return pin->isWakeOnActive() ? true : false;
@@ -210,4 +233,18 @@ bool wakeOnIsEnabled(PowerWakeup source) {
210233
}
211234

212235

236+
/**
237+
* Set the source to trigger full power.
238+
* @param source the source to set
239+
*/
240+
//% help=power/full-power-on
241+
//% group="micro:bit (V2)"
242+
//% weight=900
243+
//% block="full power on %source"
244+
//%
245+
void fullPowerOn(FullPowerSource source) {
246+
fullPowerSourceEnable(source, true);
247+
}
248+
249+
213250
} // namespace power

0 commit comments

Comments
 (0)