This projects demonstrates how data from MicroBlocks attached devices can be streamed into the SNAP environment with the help of the WebSerial Library optimized for this purpose.
To keep the project simple, x-coordinate values are broadcast from the attached micro:bit device to the SNAP project and processed and displayed on the screen as an animated sprite moving from left to right, in real time.
The project consists of two programs:
stream2snap.ubp written in MicroBLocks - streams x-coordinate values to SNAP.
stream2snap.xml written in SNAP - receives streaming data and animates sprite on screen.
Screenshots of both programs are presented below.
This project uses the WebSerial Library to be added to the anticipated SNAP v7 release.
The WebSerial Library provides USB based communication to the attached micro:bit device, and allows SNAP script to access the VM (virtual machine) on the MicroBlocks side. This enables the SNAP script to receive the streamed x-coordinate values from the micro:bit device. While this project is specifically using a micro:bit, any MicroBlocks supported device can be substituted using the same code.
On the MicroBlocks side is a very simple program that steadily broadcasts x coordinate values from the attached micro:bit device.
On the SNAP side is a bit more complicated program that does three things:
Let's examine the blocks in a bit more detail.
Two sets of code under When button A pressed and When button B pressed
enables the user to control the start and stop of the streaming from the micro device.
Once the streaming is started, the micro device will also display the current setting for the speed parameter on its LED display. This parameter is controlled from the SNAP side by the use of the up / down arrows, and will increment or decrement the speed of the moving sprite. Two way communication between the two sides allows for the real-time updating of the speed variable on the MicroBlocks side.
At the start of the program, the input buffer in is initialized and the port variable is set according to the selected USB connection at a speed of 115200bps. This is default communication speed for the MicroBlocks VM.
The speed variable for the moving sprite is initialized to 1 pixel increments. This value can be changed by the use of the up / down arrow keys and will increment or decrement the value accordingly, as well as synchronize the same parameter on the MicroBlocks side. The code for these are:
The forever-loop section of the script is where all the action takes place.
The streaming data arriving from the micro:bit is read into the data variable. After a quick integrity check, it is appended to the input buffer (in).
is a custom block expressly created by J.Maloney for the speedy processing of the incoming streaming data. It processes all the incoming data from the micro:bit and parses out the specific broadcast messages carrying the x-coordinate values for the sprite movement.
The VM messages of the type broadcast contain the value 27 at the beginning of the message. This is used to isolate and extract the message types of interest to this project.
Afterwards, the sprite movement is adjusted according to the x-values received. A separate block of code located in the Stage section of the SNAP script calculates the processing speed of the data and updates the FPS variable on screen.
NOTE:
MicroBlocks VM emits various messages based on the internal events it handles. For this project, we are only interested in the broadcast messages.
For a more detailed information on VM message details, please refer to the MicroBlocks VM TO SNAP Library tutorial in the SNAP section of the WIKI.
MicroBlocks Program: stream2snap.ubp