Create an Extension for micro:bit

About mBlock 5-micro:bit

mBlock 5 supports the micro:bit device.

You can click the Devices tab and click + add to add micro:bit from the device library.

图片

Currently, the following blocks are supported by mBlock 5.

图片

Conversion Principle

Currently, micro:bit supports program uploading. The following figure shows the programming language conversion of micro:bit.

图片

Drag a block and drop it under the hat block, and then click the icon on the sidebar for displaying the conversion. A block corresponding to a Python API statement of micro:bit. For how to implement this, see the following description.

For the official micro:bit Python API documents, visit https://microbit-micropython.readthedocs.io/en/latest/.

The following figure shows the API reference list.

图片

How to create an extension for micro:bit?

Extension Builder of mBlock 5 allows developers to create extensions. With Extension Builder, you can enable mBlock 5 to support new devices, add new sensors for current devices, or develop enhaced functions for blocks. The following describes how to use Extension Builder to create an enhanced extension for micro: bit in detail.

Extension Design Thinking

Before creating an extension, you need to think about the functions you want to develop for micro:bit. Comparing the micro:bit blocks supported by mBlock 5 with the offical micro:bit API, you can see that some APIs are not implemented on mBlock 5, for example:

API for image displaying (https://microbit-micropython.readthedocs.io/en/latest/tutorials/images.html)

图片

API for external music modules (https://microbit-micropython.readthedocs.io/en/latest/tutorials/images.html)

图片

Using the music API as an example, the following describes how to convert API statements into blocks.

Step 1: Sign in to Extension Builder.

Visit https://ext.mblock.cc.

Note: If you do not have an Extension Builder account, you can simply sign up one. The account can be used in all Makeblock product systems, including the mBlock community, mBlock 5 on mobile terminals, mBlock 5 on PCs, and mBlock 5 webpages. With the account, you can synchronize your projects and use the AI services of mBlock 5.

图片

Step 2: Create an extension.

After signing in to Extension Builder, choose My Plugin > My Extension in the navigation tree on the left side.

Note: On Extension Builder, My Device refer to specific hardware devices, distinguished from sprites; and My Extension refer to extensions that must be used in combination with hardware devices or sprite.

In this development example, we click Add extension, instead of Add device, to create an enhaced extension for micro:bit.

图片

Step 3: Enter basic information.

Enter the ID of the extension to be created. Do not use one that has already been used, for example, microbit.

图片

After enterng the basic information, click OK.

The extension list is displayed, and you can click the extension name to enter the editing page.

图片

Step 4: Set the category and blocks.

Click Add category to set the category.

The Extension category dialog box is displayed for you to enter the information. You can use the default ID.

图片

Click + at the bottom of Blocks list to add a block.

图片

A new block is displayed, as shown in the following figure.

图片

Step 5: Design the block.

The following uses music blocks as an example. First, choose the API we need.

To see the API document, visit https://microbit-micropython.readthedocs.io/en/latest/tutorials/music.html. You can find the following API statement:

import music
music.play(music.NYAN)

The API statement contains only one parameter with a list of parameter values.

图片

Enter play music, a space, and then @ in the Content field. A drop-down list box is displayed, containing all parameter types supported by mBlock 5. In this example, we choose Dropdown.

图片

Press Enter. The block is displayed, as shown in the following figure.

图片

Set Parameter name to musicType. The parameter name will be used later.

图片

图片

Click + to add parameter values in batches.

图片

Click OK.

The configuration is displayed, as shown in the following figure. The boxes on the left side display the descriptions of the parameter values, and those on the right display the actual values.

图片

The block with a parameter and multiple parameter values is created, as shown in the following.

图片

Select the support platform(s) and click Save. Make sure that you save the configuration.

图片

Step 6: Set transcode information.

In this example, we compile only offline operation statements. After a block is added, a line of Python code is generated. All Python code can be uploaded to micro:bit for execution.

Before compiling code, set the language into which blocks are to be converted. Click the Transcode settings tab, set Transcoding language support to Python, and click Save at the bottom.

图片

Click the Blocks settings tab, and click Upload transcode.

Double-click the black area, the Code edit page is displayed on the right side.

Enter information, as shown in the following.

Note: The API statements you enter are actually the character strings into which the block is to be converted. mBlock 5 will combine all lines of code and upload them to micro:bit .

图片

Note: The lib editing area is provided for entering the introduced Python module. Refer to the API document. You can leave it blank if there is no such information.

The code editing area provides the special syntax symbol, braces {}. You can enter a parameter name in them to obtain the parameter value you set.

图片

Click Save at the bottom. The code into which the block is to be converted is compiled.

Test the micro:bit Extension

After performing the preceding steps, we have created a simple extension. Now, let's test whether the extension works properly.

Step 1: Download the extension.

图片

图片

The file microbit_extended.mext is obtained.

Step 2: Drag the file microbit_extened.mext to mBlock 5.

A message is displayed, indicating that the extension is successfully added.

图片

Click the Devices tab and click + add to add the micro:bit device.

图片

You can see Enhanced Blocks in the Blocks area.

图片

Click + extension at the bottom of the Blocks area. The extension we created is displayed in the extension center.

图片

Step 3: Test the block.

Drag the block the Scripts area, and click the icon on the sidebar for displaying code conversion.

图片

The block and its corresponding API statement we have created can be used properly.

Connect the micro:bit device to mBlock 5, and connect a buzzer module to the P0 port of micro:bit.

图片

Click Upload.

图片

After the code is successfully uploaded to micro:bit, you can test the hardware function of micro:bit. Press button A and see what happens.

Summary

You may find that creating an extension is not as difficult as you think before. The main steps are described as follows:

Determine API statement > Configure blocks > Set transcode information > Load extension to mBlock 5 > Upload program to device API

You can create an image block by referring to the preceding steps.

图片

Extension Storage Path

The extensions you drag to Extension Builder are stored in a local directory of your PC.

For Windows: C:\Users\username\mblock\exts\
For mac: /Users/username/mblock/exts/

Download and Import micro:bit Extension Package Source

The extension we created in this example has been published. You can download it and drag it to mBlock 5 to use it.

Download Extension Package

You can also refer to the following tutorial to import the .mext file you have downloaded to Extension Builder to view parameter configuration:

Import Extension File into Extension Builder

Try creating an extension for micro:bit by yourself. After creating one, you can publish it to the official mBlock 5 software to share it with other mBlock 5 users.

More Tutorials

Stay tuned for more!

results matching ""

    No results matching ""