How Can We Help?

Transcoding Setting for Upload Mode

In Upload mode, mBlock 5 converts the blocks in the scripts area into Python, C, or Javascript statements that you can upload to your hardware device.

Set the transcoding language

Use Python as an example. After setting the transcoding language to Python, you need to configure the transcoding template.

Configure the Python transcoding template

1    # generated by mBlock5 for <product>
2    # codes make you happy
3    ### import #$$
4    ### lib #$$
5    ###
6    # initialize variables
7    ###{
8         if (this.$VARIABLES.length !== 0) {
9          this.$VARIABLES.map(n => n + ‘= 0’).join(‘\n’)
10       }
11    }#$$
12   #$$
13   ### code #$$

The template declares the final transcoding formats. You can click the </> button on the right to view the corresponding Python statements.

Structure of the template

### ??? indicates a template format, for example:

  • ### import #$$ indicates that the import field is displayed.
  • ### code #$$ indicates that the code is displayed.

 

Corresponding to Upload transcode setting of each block

If multiple blocks are configured with the same field, the multiple values of the field are combined as follows:

import = the import field of block A + the import field of block B + ...

This is processed by the code generator. Generally, you can use the default settings of the generator; however, if it doesn’t work as expected, you can modify the settings in the generator on the Transcode settings tab.
The following figure shows the Python generator.

Transcoding template description

A transcoding template embeds parameters (placeholders) into the specified character strings and generates the corresponding code when being executed. mBlock 5 Extension Builder provides the following two types of templates:

  • Block template
  • Code framework template

Block template

Used to generate the code of a block, for example, when button () pressed for Codey.

1    @event.button_{BUTTONS}_pressed
2    def on_button_{BUTTONS}_pressed{$INDEX}():
3          {$BRANCH}

In the preceding statements, BUTTONS is a parameter configured for the block and {BUTTONS} is replaced with the actual value when the statements are executed.

Syntax of block templates

  1. template can be a general parameter, for example, {args_1}
  2. template is embeddable, for example, {i am {args_1} }:

If the value of args_1 is "somebody", the template is translated as "i am somebody" ; and if args_1 is empty, the template is translated as ("").

  1. template can be embedded with JavaScript code in the format of { ... }+right, without space, for example, {{ console.log('log'), 123 }}:

The value printed is 123.

Examples

If the parameters and their values for a block are as follows:

  • name: move
  • speed: 50
  • time: 10

The template is translated as follows:

  • {name}() is translated as move().
  • {name}({speed}, {time}) is translated as move(50, 10).
  • { {name} } is translated as move. // When a template contains another template, the value of the embedded template is obtained first. Note that spaces are used in the statement.
  • {no_exist} is translated as null.//Because the variable doesn’t exist.
  • { time: {time} } is translated as time: 10. //The template contains another template.
  • { time: {no_exist} } is translated as null. //A template is translated as null if the template it contains is assigned no value.
  • { {no_exist} } is translated as null.//A template is translated as null if the template it contains is assigned no value. Note that spaces are used in the statement.
  • asdf is translated as asdf. //JavaScript expression in the template.
  • 3 is translated as 3. //JavaScript expression in the template.

Pseudo parameters

In addition, mBlock 5 Extension Builder provides some pseudo parameters, which are configured for specific purposes.

  • $INDEX: number of a block among the ones of the same type (applicable to the hat blocks), for example:

1    def on_start{$INDEX}( ):
2    //def on_start( ): is generated in the translation of the first time.
3    //def on_start1( ) : is generated in the translation of the second time.

  • $BRANCH: generates the code under a hat block, for example:

1    def on_start( ):
2          {$BRANCH}
3    //The following code is generated in the translation:
4    def on_start():
5          code1
6          code2
7          …

  • $ALL_VARIABLES:variable of mBlock 5, for example:
1    //If you have created the variables va and vb
2    {{ “global” + $ALL_VARIABLES.join(‘, ‘) }}  //JavaScript template
3    //The following is generated:
4    global va, vb

Code framework template

Template of the overall code structure, for example:

1    # generated by mBlock5 for codey
2    # codes make you happy
3    from codey import *
4    from rocky import *
5    ### import #$$
6    ### lib #$$
7    ###
8    # initialize variables
9    ###{
10           (this.$ALL_VARIABLES.length !== 0)? this.$ALL_VARIABLES.map(n=> n + ‘ = 0’).join(‘\n’):undefined
11    }#$$
12    #$$
13    ### code #$$
14    while True:
15          # every tick
16          ### loop #$$
17    #$$

Field template

Configure the required fields in the import lib code of the framework (### indicates the left parenthesis, and #$$ indicates the right parenthesis.

Javascript template

1    ###{
2    (this.$ALL_VARIABLES.length !== 0)? this.$ALL_VARIABLES.map(n=> n + ‘ = 0’).join(‘\n’):undefined
3    }#$$ // The character string obtained by the JavaScript statement holds places here.

Embedded template

1    ###
2    while True:
3          # every tick
4          ### loop #$$
5    #$$
6    // When the embedded field (loop in this example) is assigned a value, a complete code segment is generated; and if the embedded field is assigned no value, no code segment starting from “while” is generated.

Appendix 1: Arduino C framework template

1    // generated by mBlock5 for <your product>
2    // codes make you happy
3    //( include //)
4    #include <Arduino.h>
5    //( lib //)
6    //({
7

this.$ALL_VARIABLES.length==0?”:this.$ALL_VARIABLES.map(v=>”float “+v+” = 0;”).join(‘\\n’)
8    }//)
9    //( declare //)
10   void _delay(float seconds) {
11       long endTime = millis() + seconds * 1000;
12       while(millis() < endTime) _loop();
13    }
14    //(
15    void setup() {
16        //( setup //)
17        //( code //)
18    }
19    //)
20    void _loop() {
21        //( loop //)
22     }
23    void loop() {
24        _loop();
25    }

Appendix 2: Python framework template

1    # generated by mBlock5 for <product>
2    # codes make you happy
3    ### import #$$
4    ### lib #$$
5    ###
6    # initialize variables
7    ###{
8         (this.$ALL_VARIABLES.length === 0) ? undefined : this.$ALL_VARIABLES.map(n => n + ‘ = 0’).join(‘\\n’)
9    }#$$
10   #$$
11   ### code #$$
12   ###
13   while True:
14        # every tick
15        ### loop #$$
16   #$$