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

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)

  1. from microbit import *
  2. display.show(Image.HAPPY)

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

  1. import music
  2. music.play(music.NYAN)

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

Creating an Extension for micro:bit

Step 1: Sign in to Extension Builder

Visit https://ext.mblock.cc. :::info 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. ::: image.png

Step 2: Create an extension

After signing in to Extension Builder, choose My Plugin > My Extension in the navigation tree on the left side. :::info 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 sprites. ::: For details about the concepts of device and extension, see “What types of extensions can you develop with mBlock 5 Extension Builder?

In this development example, we click Add extension, instead of Add device, to create an enhaced extension for micro:bit.
[micro:bit] Create an Extension for micro:bit - 图2

Select Universal template in teh dialog box that appears, and click OK.
image.png

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.
[micro:bit] Create an Extension for micro:bit - 图4
After enterng the basic information, click OK.
The extension list is displayed, and you can click the extension name to enter the editing page.
[micro:bit] Create an Extension for micro:bit - 图5

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.
[micro:bit] Create an Extension for micro:bit - 图6
Click + at the bottom of Blocks list to add a block.
[micro:bit] Create an Extension for micro:bit - 图7
A new block is displayed, as shown in the following figure.
[micro:bit] Create an Extension for micro:bit - 图8

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:

  1. import music
  2. music.play(music.NYAN)

The API statement contains a parameter with a list of parameter values, as described in the following:

  • music.DADADADUM
  • music.ENTERTAINER
  • music.PRELUDE
  • music.ODE
  • music.NYAN
  • music.RINGTONE
  • music.FUNK
  • music.BLUES
  • music.BIRTHDAY
  • music.WEDDING
  • music.FUNERAL
  • music.PUNCHLINE
  • music.PYTHON
  • music.BADDY
  • music.CHASE
  • music.BA_DING
  • music.WAWAWAWAA
  • music.JUMP_UP
  • music.JUMP_DOWN
  • music.POWER_UP
  • music.POWER_DOWN

(1) 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.
[micro:bit] Create an Extension for micro:bit - 图9
Press Enter. The block is displayed, as shown in the following figure.
image.png
(2) Set Parameter name to musicType. The parameter name will be used later.
[micro:bit] Create an Extension for micro:bit - 图11
(3) Click + to add parameter values in batches.
[micro:bit] Create an Extension for micro:bit - 图12
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.
[micro:bit] Create an Extension for micro:bit - 图13
The block with a parameter and multiple parameter values is created, as shown in the following.
[micro:bit] Create an Extension for micro:bit - 图14
(4) Select the support platform(s) and click Save. Make sure that you save the configuration.
[micro:bit] Create an Extension for micro:bit - 图15

Step 6: Set transcoding 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, you need to set the language into which blocks are to be converted.
(1) Click the Transcode settings tab, set Transcoding language support to Python, and click Save at the bottom.
[micro:bit] Create an Extension for micro:bit - 图16
(2) Click the Blocks settings tab, and click Upload transcode.
(3) Double-click the black area, the Code edit page is displayed on the right side.
(4) Enter information, as shown in the following.
[micro:bit] Create an Extension for micro:bit - 图17 :::info 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 .
  • 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. ::: [micro:bit] Create an Extension for micro:bit - 图18
    (5) 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

[micro:bit] Create an Extension for micro:bit - 图19
[micro:bit] Create an Extension for micro:bit - 图20
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.
[micro:bit] Create an Extension for micro:bit - 图21

Click the Devices tab and click + add to add the micro:bit device.
[micro:bit] Create an Extension for micro:bit - 图22
You can see Enhanced Blocks in the Blocks area.
[micro:bit] Create an Extension for micro:bit - 图23
Click + extension at the bottom of the Blocks area.
The extension we created is displayed in the extension center.
[micro:bit] Create an Extension for micro:bit - 图24

Step 3: Test the block

Drag the block to the Scripts area, and click the icon on the side bar for displaying code conversion.
[micro:bit] Create an Extension for micro:bit - 图25
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.
[micro:bit] Create an Extension for micro:bit - 图26
Click Upload.
[micro:bit] Create an Extension for micro:bit - 图27
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

Main steps

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

  1. Determine an API statement > Configure blocks > Set transcoding information > Load the extension to mBlock 5 > Upload a program to the device

You can create an image block by referring to the preceding steps.
[micro:bit] Create an Extension for micro:bit - 图28

Extension Storage Path

The extensions you drag to mBlock 5 are stored in a local directory of your PC.

  1. For Windows: C:\Users\username\mblock\exts\
  2. For Mac: /Users/username/mblock/exts/

Download and import the micro:bit extension package

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

You can also import the .mext file you have downloaded to Extension Builder to view parameter configuration. For details, see “Importing an Extension 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.