Snap! and MicroBlocks can exchange string messages over a USB cable using WebSerial. Messages are a flexible communciation mechanism that can be used in many ways.
For example, "on" and "off" command messages might be sent from Snap! to MicroBlocks to turn an LED on and off. Or a MicroBlocks program might stream x and y tilt sensor data to Snap! using messsages containing numeric values, such as "tiltXY -50 20".
MicroBlocks sends messages to Snap! using the broadcast block and receives messages using the when _ received block. Snap! programs use a library (see download link below) to send messages to MicroBlocks and to process messages from MicroBlocks.
Sending messages from MicroBlocks to Snap! is fast enough to use a MicroBlocks board as the controller for a game or to graph sensor values in real time. Sending messages from Snap! to MicroBlocks is not as fast; it currently takes about 33 millisecond per message.
Snap! projects communicate with MicroBlocks using the MicroBlocks messaging library. The API consists of just four blocks:
Open a WebSerial connection to a MicroBlocks board.
Close the WebSerial connection.
Send a message string to MicroBlocks.
Process incoming messages from MicroBlocks. The body of the block is executed once for each message with the msg variable set to the current message string. A warp block is used internally to allow multiple messages to be processed during a single Snap! display frame.
The following Snap! program blinks the MicroBlocks user LED:
The MicroBlocks side of this example is just two scripts:
Incoming Snap! messages trigger the corresponding when _ received scripts in MicroBlocks. If there is no script for a given message, the message is simply ignored.
MicroBlocks can stream sensor data to Snap!, allowing Snap! to collect or graph that data. Sensor data can also be used to control Snap! projects. For example, the tilt sensor might be used to control the bird in a Flappy Bird game.
This Snap! program uses the x and y tilt values from an accelerometer to control the position of a Snap! sprite:
Every 15 milliseconds, MicroBlocks sends the tilt x and y values as two numbers separted by a space:
Snap! splits the incoming message strings into words to extract the x and y values for sprite positioning.
Many interesting applications can be created with simple messages like those above. However, to support more complex applications, parameters can be added after an initial command. For example:
set_power 60
turn_on_LED_at 3 5
scroll_text Hello, Rosa!
In the last example, the string parameter would be all the text following the scroll_text command.
To receive such messages in MicroBlocks, we use a when _ received hat with an empty string as the message. That hat will be triggered by every message. The actual message is returned by the last message block (an advanced block).
For example, if Snap! sends a message to scroll some text on the micro:bit display:
MicroBlocks could handle that message with this script:
Of course, one could add more cases to handle additional commands.