-
Notifications
You must be signed in to change notification settings - Fork 3
/
power.cpp
250 lines (214 loc) · 5.42 KB
/
power.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* (c) 2021, Micro:bit Educational Foundation and contributors
*
* SPDX-License-Identifier: MIT
*/
#include "pxt.h"
#if MICROBIT_CODAL
#include "Timer.h"
#include "MicroBitCompat.h"
#ifndef MICROBIT_ID_MAKECODE_POWER
#define MICROBIT_ID_MAKECODE_POWER 9000
#endif
#endif // MICROBIT_CODAL
enum class FullPowerSource {
//% block="button A"
A = MICROBIT_ID_BUTTON_A,
//% block="button B"
B = MICROBIT_ID_BUTTON_B,
//% block="pin P0"
P0 = MICROBIT_ID_IO_P0,
//% block="pin P1"
P1 = MICROBIT_ID_IO_P1,
//% block="pin P2"
P2 = MICROBIT_ID_IO_P2
};
enum class LowPowerMode {
//% block="continue"
Continue = 0,
//% block="wait"
Wait = 1
};
enum class LowPowerEnable {
//% block="prevent"
Prevent = 0,
//% block="allow"
Allow = 1
};
//% block="Power"
//% icon="\uf011"
//% color=#AA278D
namespace power {
#if MICROBIT_CODAL
int timerEventValue = 1;
#endif // MICROBIT_CODAL
void _lowPowerRequest(LowPowerMode mode = LowPowerMode::Continue);
/**
* Request low power when the next idle
* @param mode If Continue, then return immediately; if Wait, then pause until a power-up event occurs
*/
//% help=power/low-power-request
//% group="micro:bit (V2)"
//% weight=700
//% block="request low power||and $mode"
//%
void _lowPowerRequest(LowPowerMode mode) {
#if MICROBIT_CODAL
if ( mode == LowPowerMode::Wait)
uBit.power.deepSleep();
else
uBit.power.deepSleepAsync();
#else
uBit.sleep(0);
#endif
}
/**
* Pause for a fixed interval, and request low power when idle.
* @param interval The period of time to pause, in milliseconds.
*/
//% help=power/low-power-for
//% group="micro:bit (V2)"
//% weight=600
//% interval.shadow=longTimePicker
//% block="request low power for $interval ms"
//%
void _lowPowerPause(int interval) {
#if MICROBIT_CODAL
uBit.power.deepSleep(interval);
#else
uBit.sleep(interval);
#endif
}
/**
* Prevent or allow low power.
* Prevent and allow requests should occur in pairs.
* The default is to allow.
*/
//% help=power/low-power-enable
//% weight=500
//% block="low power %enable"
//%
void _lowPowerEnable(LowPowerEnable enable) {
#if MICROBIT_CODAL
switch ( enable)
{
case LowPowerEnable::Prevent:
uBit.power.powerDownDisable();
break;
case LowPowerEnable::Allow:
uBit.power.powerDownEnable();
break;
default:
break;
}
#endif
}
/**
* Determine if low power is enabled
*/
//% help=power/low-power-is-enabled
//%
bool _lowPowerIsEnabled() {
#if MICROBIT_CODAL
return uBit.power.powerDownIsEnabled();
#else
return false;
#endif
}
/**
* Do something repeatedy with full power using a timer.
* @param interval the time (in ms) for the timer.
* @param code the code to execute
*/
//% help=power/full-power-every
//% group="micro:bit (V2)"
//% weight=800
//% blockAllowMultiple=1
//% interval.shadow=longTimePicker
//% afterOnStart=true
//% block="full power every $interval ms"
//%
void _fullPowerEvery(int interval, Action code) {
#if MICROBIT_CODAL
registerWithDal( MICROBIT_ID_MAKECODE_POWER, timerEventValue, code);
// CODAL_TIMER_EVENT_FLAGS_WAKEUP makes the timer event trigger power up
system_timer_event_after( 0, MICROBIT_ID_MAKECODE_POWER, timerEventValue, CODAL_TIMER_EVENT_FLAGS_WAKEUP);
system_timer_event_every( interval, MICROBIT_ID_MAKECODE_POWER, timerEventValue++, CODAL_TIMER_EVENT_FLAGS_WAKEUP);
#else
target_panic(PANIC_VARIANT_NOT_SUPPORTED);
#endif
}
/**
* Set whether the source should trigger full power.
* @param source the source to set
* @param enable true to trigger full power
*/
//% help=power/full-power-source-enable
//%
void _fullPowerSourceEnable(FullPowerSource source, bool enable) {
#if MICROBIT_CODAL
switch ( source)
{
case FullPowerSource::A:
uBit.buttonA.wakeOnActive(enable ? 1 : 0);
break;
case FullPowerSource::B:
uBit.buttonB.wakeOnActive(enable ? 1 : 0);
break;
case FullPowerSource::P0:
case FullPowerSource::P1:
case FullPowerSource::P2:
{
MicroBitPin *pin = getPin((int)source);
pin->wakeOnActive(enable ? 1 : 0);
break;
}
default:
break;
}
#endif
}
/**
* Determine if the source will trigger full power.
* @param source the source to check
* @return true if the source will trigger full power
*/
//% help=power/full-power-source-is-enabled
//%
bool _fullPowerSourceIsEnabled(FullPowerSource source) {
#if MICROBIT_CODAL
switch ( source)
{
case FullPowerSource::A:
return uBit.buttonA.isWakeOnActive() ? true : false;
break;
case FullPowerSource::B:
return uBit.buttonB.isWakeOnActive() ? true : false;
break;
case FullPowerSource::P0:
case FullPowerSource::P1:
case FullPowerSource::P2:
{
MicroBitPin *pin = getPin((int)source);
return pin->isWakeOnActive() ? true : false;
break;
}
default:
break;
}
#endif
return false;
}
/**
* Set the source to trigger full power.
* @param source the source to set
*/
//% help=power/full-power-on
//% group="micro:bit (V2)"
//% weight=900
//% block="full power on %source"
//%
void _fullPowerOn(FullPowerSource source) {
_fullPowerSourceEnable(source, true);
}
} // namespace power