Skip to content

Commit 5517c6c

Browse files
Create simple and detailed stop block (#12)
The detailed stop block takes the duration as a callback argument.
1 parent d189d8d commit 5517c6c

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ ml.onStart(ml.event.DrawCircle, function () {
2121
ml.onStart(ml.event.Unknown, function () {
2222
basic.clearScreen();
2323
});
24-
ml.onStop(ml.event.Still, function (duration) {
24+
ml.onStop(ml.event.Shake, function () {
25+
serial.writeLine("Stopped shaking" + "\n");
26+
});
27+
ml.onStopDetailed(ml.event.Still, function (duration) {
2528
timeStill += duration;
2629
});
2730
basic.forever(function () {

pxtextension.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ class MlEvent {
44
eventValue: number;
55
eventLabel: string;
66
lastDuration: number;
7-
onStopHandler: (duration: number) => void;
7+
onStopHandler: () => void;
8+
onStopDetailedHandler: (duration: number) => void;
89

910
constructor(value: number, label: string) {
1011
this.eventValue = value;
@@ -31,8 +32,12 @@ namespace ml {
3132
let now = input.runningTime();
3233
prevEventInstance.lastDuration = now - lastEventTimestamp;
3334

35+
if (prevEventInstance.onStopDetailedHandler) {
36+
prevEventInstance.onStopDetailedHandler(prevEventInstance.lastDuration);
37+
}
38+
3439
if (prevEventInstance.onStopHandler) {
35-
prevEventInstance.onStopHandler(prevEventInstance.lastDuration);
40+
prevEventInstance.onStopHandler();
3641
}
3742

3843
lastEventTimestamp = now;
@@ -54,7 +59,7 @@ namespace ml {
5459
*/
5560
//% blockId=ml_on_event_start
5661
//% block="on ML $event start"
57-
//% weight=40
62+
//% weight=50
5863
//% parts="v2"
5964
//% group="micro:bit (V2)"
6065
export function onStart(event: MlEvent, body: () => void): void {
@@ -77,25 +82,38 @@ namespace ml {
7782
}
7883

7984
//% blockId=ml_on_event_stop
80-
//% block="on ML $event stop after $duration (ms)"
85+
//% block="on ML $event stop"
86+
//% weight=40
87+
//% parts="v2"
88+
//% group="micro:bit (V2)"
89+
export function onStop(event: MlEvent, body: () => void): void {
90+
if (!isRunning()) {
91+
startRunning();
92+
}
93+
event.onStopHandler = body;
94+
}
95+
96+
//% blockId=ml_on_event_stop_detailed
97+
//% block="on ML $event stop $duration (ms)"
8198
//% weight=30
8299
//% draggableParameters="reporter"
83100
//% parts="v2"
84101
//% group="micro:bit (V2)"
85-
export function onStop(
102+
export function onStopDetailed(
86103
event: MlEvent,
87104
body: (duration: number) => void
88105
): void {
89106
if (!isRunning()) {
90107
startRunning();
91108
}
92-
event.onStopHandler = body;
109+
event.onStopDetailedHandler = body;
93110
}
94111

95112
//% blockId=ml_is_event_detected
96113
//% block="is ML $event detected"
97114
//% weight=20
98115
//% parts="v2"
116+
//% group="micro:bit (V2)"
99117
export function isDetected(event: MlEvent): boolean {
100118
if (!isRunning()) {
101119
startRunning();
@@ -108,7 +126,6 @@ namespace ml {
108126
//% block="certainty (\\%) ML $event"
109127
//% weight=10
110128
//% parts="v2"
111-
//% group="micro:bit (V2)"
112129
export function getCertainty(event: MlEvent): number {
113130
const eventValue = event.eventValue;
114131
if (eventValue <= 1) {

0 commit comments

Comments
 (0)