Skip to content

Commit 18a60c6

Browse files
First pass help docs and docstring test
1 parent 8bfb85a commit 18a60c6

File tree

7 files changed

+171
-6
lines changed

7 files changed

+171
-6
lines changed

docs/ml_get_event_certainty.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 is not considered when its certainty is below the recognition point). Some programs may want to access 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 that the current action is `unknown` every second.
22+
23+
```blocks
24+
loops.everyInterval(1000, function () {
25+
basic.showNumber(ml.getCertainty(ml.event.Unknown))
26+
})
27+
```
28+
29+
```package
30+
machine-learning=github:microbit-foundation/pxt-microbit-ml
31+
```

docs/ml_is_event_detected.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 block compares the latest value of the estimated value to the action you choose. 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 `unknown` at the time the conditional statement is checked.
24+
25+
```blocks
26+
basic.forever(function () {
27+
if (ml.isDetected(ml.event.Unknown)) {
28+
basic.showIcon(IconNames.Yes)
29+
}
30+
})
31+
```
32+
33+
```package
34+
machine-learning=github:microbit-foundation/pxt-microbit-ml
35+
```

docs/ml_on_event_start.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 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 uses the special `unknown` value and plays a musical melody in the background when no action has a certainty above the recognition point.
19+
20+
```blocks
21+
ml.onStart(ml.event.Unknown, function () {
22+
music._playDefaultBackground(music.builtInPlayableMelody(Melodies.Dadadadum), music.PlaybackMode.InBackground)
23+
})
24+
```
25+
26+
```package
27+
machine-learning=github:microbit-foundation/pxt-microbit-ml
28+
```

docs/ml_on_event_stop.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 when the ML model’s estimated action changes to 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 uses the special `unknown` option and stops playing a musical melody when any other action has a certainty above the recognition point.
21+
22+
```blocks
23+
ml.onStop(ml.event.Unknown, function () {
24+
music.stopMelody(MelodyStopOptions.All)
25+
})
26+
```
27+
28+
```package
29+
machine-learning=github:microbit-foundation/pxt-microbit-ml
30+
```

docs/ml_on_event_stop_detailed.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 when the ML model’s estimated action changes to 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+
The event handler is passed a `duration` parameter. The duration is the 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.
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+
- **duration**: the [number](/types/number) of milliseconds the action was the estimated action.
18+
19+
## Example
20+
21+
This example uses the special `unknown` option and stops playing a musical melody when any other action has a certainty above the recognition point.
22+
23+
```blocks
24+
ml.onStopDetailed(ml.event.Unknown, function (duration) {
25+
basic.showNumber(duration / 1000)
26+
})
27+
```
28+
29+
```package
30+
machine-learning=github:microbit-foundation/pxt-microbit-ml
31+
```

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: 10 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+
*
57+
* @param event one of the actions the machine learning model was trained on.
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 = () => {
@@ -82,7 +87,7 @@ namespace ml {
8287
//% weight=40
8388
//% parts="v2"
8489
//% group="micro:bit (V2)"
85-
//% help=none
90+
//% help=github:machine-learning/docs/ml_on_event_stop
8691
export function onStop(event: MlEvent, body: () => void): void {
8792
if (!isRunning()) {
8893
startRunning();
@@ -96,7 +101,7 @@ namespace ml {
96101
//% draggableParameters="reporter"
97102
//% parts="v2"
98103
//% group="micro:bit (V2)"
99-
//% help=none
104+
//% help=github:machine-learning/docs/ml_on_event_stop_detailed
100105
export function onStopDetailed(
101106
event: MlEvent,
102107
body: (duration: number) => void
@@ -112,7 +117,7 @@ namespace ml {
112117
//% weight=20
113118
//% parts="v2"
114119
//% group="micro:bit (V2)"
115-
//% help=none
120+
//% help=github:machine-learning/docs/ml_is_event_detected
116121
export function isDetected(event: MlEvent): boolean {
117122
if (!isRunning()) {
118123
startRunning();
@@ -125,7 +130,7 @@ namespace ml {
125130
//% block="certainty (\\%) ML $event"
126131
//% weight=10
127132
//% parts="v2"
128-
//% help=none
133+
//% help=github:machine-learning/docs/ml_get_event_certainty
129134
export function getCertainty(event: MlEvent): number {
130135
const eventValue = event.eventValue;
131136
if (eventValue <= 1) {

0 commit comments

Comments
 (0)