Open this page at GitHub Pages: https://jp-rad.github.io/pxt-mstate/
The pxt-mstate extension is a user-defined extension for micro:bit that enables state machine-based coding.
A state machine is a model that represents transitions from one state to another and can represent complex behavior in a simple way. Usually, state machines are drawn as state diagrams using tools such as UML, from which source code is generated. However, the pxt-mstate extension provides the ability to automatically generate state diagrams from coding.
The pxt-mstate extension allows you to directly define states and transitions using block coding, and to traverse states in response to triggers. You can also visualize them in a state diagram to understand the behavior of your state machine.
Get the pxt-mstate extension into your projects and enjoy programming with state machines! 😊.
This repository can be added as an extension in MakeCode.
- open https://makecode.microbit.org/
- click on New Project
- click on Extensions under the gearwheel menu
- search for https://github.com/jp-rad/pxt-mstate and import
To edit this repository in MakeCode.
- open https://makecode.microbit.org/
- click on Import then click on Import URL
- paste https://github.com/jp-rad/pxt-mstate and click import
Example
mstate.defineState(StateMachines.M0, "State1", function () {
mstate.descriptionUml("Blink Heart Icon")
mstate.onState(500, function (tickcount) {
if (0 == tickcount) {
blinkLED(false)
basic.showIcon(IconNames.Heart, 20)
} else {
blinkLED(true)
}
})
mstate.onExit(function () {
blinkLED(false)
basic.showIcon(IconNames.Happy)
})
mstate.onTrigger("Trigger1", [""], function () {
mstate.traverse(StateMachines.M0, 0)
})
})
function blinkLED(enabled: boolean) {
if (!(enabled)) {
blink = 1
}
led.setBrightness(blink * 155 + 100)
blink += 1
blink = blink % 2
}
input.onButtonPressed(Button.A, function () {
mstate.start(StateMachines.M0, "State1")
})
input.onButtonPressed(Button.B, function () {
mstate.send(StateMachines.M0, "Trigger1")
})
let blink = 0
mstate.exportUml(StateMachines.M0, "State1")
basic.showString("M")
UML
- for PXT/microbit