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
**
# generated by mBlock5 for <product>
# codes make you happy
### import #$$
### lib #$$
###
# initialize variables
###{
if (this.$VARIABLES.length !== 0) {
this.$VARIABLES.map(n => n + '= 0').join('\n')
}
}#$$
#$$
### 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 theimport
field is displayed.### code #$$
indicates that thecode
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 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:
Used to generate the code of a block, for example, when button () pressed for Codey.
@event.button_{BUTTONS}_pressed
def on_button_{BUTTONS}_pressed{$INDEX}():
{$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
template
can be a general parameter, for example,{args_1}
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 ("")
.
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 asmove()
.{name}({speed}, {time})
is translated asmove(50, 10)
.{ {name} }
is translated asmove
. // 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 astime: 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 asasdf
. //JavaScript expression in the template3
is translated as3
. //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:
def on_start{$INDEX}():
//def on_start(): is generated in the translation of the first time.
//def on_start1(): is generated in the translation of the second time.
$BRANCH
: generates the code under a hat block, for example:
def on_start():
{$BRANCH}
//The following code is generated in the translation:
def on_start():
code1
code2
...
$ALL_VARIABLES
:variable of mBlock 5, for example:
//If you have created the variables va and vb
{{ "global" + $ALL_VARIABLES.join(', ') }} //JavaScript template
//The following is generated:
global va, vb
Code framework template
Template of the overall code structure, for example:
# generated by mBlock5 for codey
# codes make you happy
from codey import *
from rocky import *
### import #$$
### lib #$$
###
# initialize variables
###{
(this.$ALL_VARIABLES.length !== 0)? this.$ALL_VARIABLES.map(n=> n + ' = 0').join('\n'):undefined
}#$$
#$$
### code #$$
while True:
# every tick
### loop #$$
#$$
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
###{
(this.$ALL_VARIABLES.length !== 0)? this.$ALL_VARIABLES.map(n=> n + ' = 0').join('\n'):undefined
}#$$ // The character string obtained by the JavaScript statement holds places here.
Embedded template
###
while True:
# every tick
### loop #$$
#$$
// When the embedded field (loop in this exmaple) 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
// generated by mBlock5 for <your product>
// codes make you happy
//( include //)
#include <Arduino.h>
//( lib //)
//({
this.$ALL_VARIABLES.length==0?'':this.$ALL_VARIABLES.map(v=>"float "+v+" = 0;").join('\\n')
}//)
//( declare //)
void _delay(float seconds) {
long endTime = millis() + seconds * 1000;
while(millis() < endTime) _loop();
}
//(
void setup() {
//( setup //)
//( code //)
}
//)
void _loop() {
//( loop //)
}
void loop() {
_loop();
}
Appendix 2: Python framework template**
**
# generated by mBlock5 for <product>
# codes make you happy
### import #$$
### lib #$$
###
# initialize variables
###{
(this.$ALL_VARIABLES.length === 0) ? undefined : this.$ALL_VARIABLES.map(n => n + ' = 0').join('\\n')
}#$$
#$$
### code #$$
###
while True:
# every tick
### loop #$$
#$$