Skip to content

Commit 838fbc6

Browse files
Add help documentation and docstrings (#29)
--------- Co-authored-by: Matt Hillsdon <[email protected]>
1 parent 8bfb85a commit 838fbc6

9 files changed

+211
-7
lines changed
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
1-
{}
1+
{
2+
"ml.getCertainty": "Get the certainty of an ML event in percent (0 to 100).",
3+
"ml.getCertainty|param|event": "one of the actions the machine learning model was trained on",
4+
"ml.isDetected": "Tests if an ML event is currently detected.",
5+
"ml.isDetected|param|event": "one of the actions the machine learning model was trained on",
6+
"ml.onStart": "Do something when an ML event is detected.",
7+
"ml.onStart|param|body": "code to execute",
8+
"ml.onStart|param|event": "one of the actions the machine learning model was trained on",
9+
"ml.onStop": "Do something when an ML event is no longer detected.",
10+
"ml.onStopDetailed": "Do something when an ML event is no longer detected.",
11+
"ml.onStopDetailed|param|body": "code to execute",
12+
"ml.onStopDetailed|param|event": "one of the actions the machine learning model was trained on",
13+
"ml.onStop|param|body": "code to execute",
14+
"ml.onStop|param|event": "one of the actions the machine learning model was trained on"
15+
}

_locales/machine-learning-strings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"ml.onStart|block": "on ML $event start",
66
"ml.onStopDetailed|block": "on ML $event stop $duration (ms)",
77
"ml.onStop|block": "on ML $event stop",
8+
"ml|block": "Machine Learning",
89
"{id:category}Ml": "Ml",
910
"{id:category}MlEvent": "MlEvent",
1011
"{id:group}micro:bit (V2)": "micro:bit (V2)"

docs/ml_get_event_certainty.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# on ML start
2+
3+
Gets the latest certainty value for an ML action.
4+
5+
```sig
6+
ml.getCertainty(ml.event.Unknown)
7+
```
8+
9+
The ML model runs several times a second and calculates a certainty value for each action. The estimated action is the action with the highest certainty. An action cannot be the estimated action when its certainty is below the recognition point. Some programs may need to use the certainty values directly, for example to display or log them. Most programs can use the estimated action instead of certainty values.
10+
11+
## Parameters
12+
13+
- **event**: one of the actions the machine learning model was trained on.
14+
15+
## Returns
16+
17+
- a percentage as a [number](/types/number) from 0 to 100, representing the ML model’s certainty that this is the action being performed. The certainty for `unknown` is always 0.
18+
19+
## Example
20+
21+
This example displays the ML model's certainty, in percent, that the current action is `clapping` every second.
22+
23+
```blocks
24+
loops.everyInterval(1000, function () {
25+
basic.showNumber(ml.getCertainty(ml.event.Clapping))
26+
})
27+
```
28+
29+
```package
30+
machine-learning-help-stubs=github:microbit-foundation/pxt-microbit-ml-help-stubs#v0.0.1
31+
machine-learning=github:microbit-foundation/pxt-microbit-ml#v1.0.2
32+
```

docs/ml_is_event_detected.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# is ML detected
2+
3+
Checks if an ML action is the estimated action.
4+
5+
```sig
6+
ml.isDetected(ml.event.Unknown)
7+
```
8+
9+
The ML model updates its estimated action several times a second. This function returns `true` if the chosen action is currently estimated. Use the boolean value to make logical decisions in your program.
10+
11+
Some programs will be easier to write using the “on ML start” and “on ML stop” event handlers instead.
12+
13+
## Parameters
14+
15+
- **event**: one of the actions the machine learning model was trained on. The special value `unknown` represents the case where no action has a certainty above the recognition point.
16+
17+
## Returns
18+
19+
- a [boolean](/types/boolean) value that is `true` if the ML action is the estimated action, `false` if the ML action is not the estimated action.
20+
21+
## Example
22+
23+
This example will show a tick icon on the LED display if the estimated action is `clapping` at the time the conditional statement is checked.
24+
25+
```blocks
26+
basic.forever(function () {
27+
if (ml.isDetected(ml.event.Clapping)) {
28+
basic.showIcon(IconNames.Yes)
29+
}
30+
})
31+
```
32+
33+
```package
34+
machine-learning-help-stubs=github:microbit-foundation/pxt-microbit-ml-help-stubs#v0.0.1
35+
machine-learning=github:microbit-foundation/pxt-microbit-ml#v1.0.2
36+
```

docs/ml_on_event_start.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# on ML start
2+
3+
Start an [event handler](/reference/event-handler) (part of the program that will run when something happens). This handler works when the ML model’s estimated action changes to the action you select.
4+
5+
```sig
6+
ml.onStart(ml.event.Unknown, function () {
7+
})
8+
```
9+
10+
The ML model updates its estimated action several times a second, but this event handler only runs when the estimated action changes.
11+
12+
## Parameters
13+
14+
- **event**: one of the actions the machine learning model was trained on. The special value `unknown` represents the case where no action has a certainty above the recognition point.
15+
16+
## Example
17+
18+
This example plays a musical melody in the background when the action `clapping` has a certainty above the recognition point.
19+
20+
```blocks
21+
ml.onStart(ml.event.Clapping, function () {
22+
music._playDefaultBackground(music.builtInPlayableMelody(Melodies.Dadadadum), music.PlaybackMode.InBackground)
23+
})
24+
```
25+
26+
```package
27+
machine-learning-help-stubs=github:microbit-foundation/pxt-microbit-ml-help-stubs#v0.0.1
28+
machine-learning=github:microbit-foundation/pxt-microbit-ml#v1.0.2
29+
```

docs/ml_on_event_stop.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# on ML stop
2+
3+
Start an [event handler](/reference/event-handler) (part of the program that will run when something happens). This handler works when the ML model’s estimated action changes from the action you select.
4+
5+
```sig
6+
ml.onStop(ml.event.Unknown, function () {
7+
})
8+
```
9+
10+
When an action changes, the stop event handler for the previous action will run, followed by the start event handler for the next action.
11+
12+
For example, if your start event handler for an action starts music playing in the background, you could use a stop event handler to stop it.
13+
14+
## Parameters
15+
16+
- **event**: one of the actions the machine learning model was trained on. The special value `unknown` represents the case where no action has a certainty above the recognition point.
17+
18+
## Example
19+
20+
This example stops playing a musical melody when the estimated action changes from `clapping` to any other action.
21+
22+
```blocks
23+
ml.onStop(ml.event.Clapping, function () {
24+
music.stopMelody(MelodyStopOptions.All)
25+
})
26+
```
27+
28+
```package
29+
machine-learning-help-stubs=github:microbit-foundation/pxt-microbit-ml-help-stubs#v0.0.1
30+
machine-learning=github:microbit-foundation/pxt-microbit-ml#v1.0.2
31+
```

docs/ml_on_event_stop_detailed.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# on ML stop
2+
3+
Start an [event handler](/reference/event-handler) (part of the program that will run when something happens). This handler works when the ML model’s estimated action changes from the action you select.
4+
5+
```sig
6+
ml.onStopDetailed(ml.event.Unknown, function (duration) {
7+
})
8+
```
9+
10+
When an action changes, the stop event handler for the previous action will run, followed by the start event handler for the next action.
11+
12+
For example, if your start event handler for an action starts music playing in the background, you could use a stop event handler to stop it.
13+
14+
The event handler is passed a `duration` parameter. The duration is the [number](/types/number) of milliseconds since this action became the estimated action. You can use the duration parameter in your code, for example displaying it or using a variable to keep a running total.
15+
16+
## Parameters
17+
18+
- **event**: one of the actions the machine learning model was trained on. The special value `unknown` represents the case where no action has a certainty above the recognition point.
19+
20+
## Example
21+
22+
This example shows on the LED display, in seconds, how long the estimated action was `clapping`, when the estimated action changes from `clapping` to any other action.
23+
24+
```blocks
25+
ml.onStopDetailed(ml.event.Clapping, function (duration) {
26+
basic.showNumber(duration / 1000)
27+
})
28+
```
29+
30+
```package
31+
machine-learning-help-stubs=github:microbit-foundation/pxt-microbit-ml-help-stubs#v0.0.1
32+
machine-learning=github:microbit-foundation/pxt-microbit-ml#v1.0.2
33+
```

pxt.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
"README.md",
1111
"enums.d.ts",
1212
"pxtextension.ts",
13-
"pxtextension.cpp"
13+
"pxtextension.cpp",
14+
"docs/ml_is_event_detected.md",
15+
"docs/ml_get_event_certainty.md",
16+
"docs/ml_on_event_start.md",
17+
"docs/ml_on_event_stop.md",
18+
"docs/ml_on_event_stop_detailed.md"
1419
],
1520
"testFiles": [
1621
"autogenerated.ts",

pxtextension.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,17 @@ namespace ml {
5151
}
5252
}
5353

54+
/**
55+
* Do something when an ML event is detected.
56+
* @param event one of the actions the machine learning model was trained on
57+
* @param body code to execute
58+
*/
5459
//% blockId=ml_on_event_start
5560
//% block="on ML $event start"
5661
//% weight=50
5762
//% parts="v2"
5863
//% group="micro:bit (V2)"
59-
//% help=none
64+
//% help=github:machine-learning/docs/ml_on_event_start
6065
export function onStart(event: MlEvent, body: () => void): void {
6166
event.onStartHandler = body;
6267
const wrappedBody = () => {
@@ -77,26 +82,36 @@ namespace ml {
7782
);
7883
}
7984

85+
/**
86+
* Do something when an ML event is no longer detected.
87+
* @param event one of the actions the machine learning model was trained on
88+
* @param body code to execute
89+
*/
8090
//% blockId=ml_on_event_stop
8191
//% block="on ML $event stop"
8292
//% weight=40
8393
//% parts="v2"
8494
//% group="micro:bit (V2)"
85-
//% help=none
95+
//% help=github:machine-learning/docs/ml_on_event_stop
8696
export function onStop(event: MlEvent, body: () => void): void {
8797
if (!isRunning()) {
8898
startRunning();
8999
}
90100
event.onStopHandler = body;
91101
}
92102

103+
/**
104+
* Do something when an ML event is no longer detected.
105+
* @param event one of the actions the machine learning model was trained on
106+
* @param body code to execute
107+
*/
93108
//% blockId=ml_on_event_stop_detailed
94109
//% block="on ML $event stop $duration (ms)"
95110
//% weight=30
96111
//% draggableParameters="reporter"
97112
//% parts="v2"
98113
//% group="micro:bit (V2)"
99-
//% help=none
114+
//% help=github:machine-learning/docs/ml_on_event_stop_detailed
100115
export function onStopDetailed(
101116
event: MlEvent,
102117
body: (duration: number) => void
@@ -107,12 +122,16 @@ namespace ml {
107122
event.onStopDetailedHandler = body;
108123
}
109124

125+
/**
126+
* Tests if an ML event is currently detected.
127+
* @param event one of the actions the machine learning model was trained on
128+
*/
110129
//% blockId=ml_is_event_detected
111130
//% block="is ML $event detected"
112131
//% weight=20
113132
//% parts="v2"
114133
//% group="micro:bit (V2)"
115-
//% help=none
134+
//% help=github:machine-learning/docs/ml_is_event_detected
116135
export function isDetected(event: MlEvent): boolean {
117136
if (!isRunning()) {
118137
startRunning();
@@ -121,11 +140,15 @@ namespace ml {
121140
return event.eventValue == currentEventId();
122141
}
123142

143+
/**
144+
* Get the certainty of an ML event in percent (0 to 100).
145+
* @param event one of the actions the machine learning model was trained on
146+
*/
124147
//% blockId=ml_on_event_certainty
125148
//% block="certainty (\\%) ML $event"
126149
//% weight=10
127150
//% parts="v2"
128-
//% help=none
151+
//% help=github:machine-learning/docs/ml_get_event_certainty
129152
export function getCertainty(event: MlEvent): number {
130153
const eventValue = event.eventValue;
131154
if (eventValue <= 1) {

0 commit comments

Comments
 (0)