6.1 Upload Mode Code Configuration
Under "Upload Mode", blocks in workspace can be transcoded to python / c /javascript
programs, which can be uploaded to the device by users.
Transcode settings
Python transcode 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 #$$
This template displays the formats of transcoded results. You can check the Scripts area for actual formating:
Structure of the template
### ???
represents a template style, for instance:
### import #$$
means to transcode to theimport
part of the program### code #$$
means to transcode to the body of the program
Upload mode transcode setting of each part
If several blocks share the same part, the transcoding process will combine the values of the shared part. As follows:
import = import part of block A + import part of block B...
This transcoding process is called "generator", and does not require any change to the default settings. If the generator doesn't not work well, you can change the settings in Transcode settings/Python generator
Introduction to transcoding template
Transcoding template contains strings which will be replaced by actual parameters to generate running codes. There are two types of template in the mBlock Extension Builder:
- Block template
- Transcoding language template
Block template
Block template is used to generate code for each block, such as Codey's when button (A) is pressed。
@event.button_{BUTTONS}_pressed
def on_button_{BUTTONS}_pressed{$INDEX}():
{$BRANCH}
In this case BUTTONS
refers to the name of the parameter, and {BUTTONS}
will be replaced by actual parameter value.
Block template introduction:
template
can be general parameter like{args_1}
template
can be nested: The value of{i am {args_1} }
depends on the value ofargs_1
. Ifargs_1
is“somebody"
, the result will be"i am somebody"
; ifargs_1
is empty, the result will be("")
template
can embed Javascript code, the basic rule being{ ... }+right
(no extra space). For instance, the value of{{ console.log('log'), 123 }}
is123
Following are some examples:
If the actual parameters of the block are:
name: move
speed: 50
time: 10
The transcoding rules are:
{name}()
will be transcoded tomove()
{name}({speed}, {time} )
will be transcoded tomove(50, 10)
{ {name} }
will be transcoded tomove
// Nested template, inner-level first (with space){no_exist}
will be transcoded toNull
// No variable{ time: {time} }
will be transcoded totime: 10
//Template within template{ time: {no_exist} }
will be transcoded toNull
,// The inner template has no variable, leading to the same result for the outer template{ {no_exist} }
will be transcoded toNull
// The inner-level has no variableasdf
will be transcoded toasdf
// The embeded JavaScript code3
will be transcoded to3
// The embeded JavaScript code
Besides, there are some pseudo parameter for special purposes:
$INDEX
: index number of blocks of the same type (for Hat Blocks). For instance:
def on_start{$INDEX}():,
First-time transcoding: def on_start():
Second-time transcoding: def on_start1():
$BRANCH
: codes following the Hat block. For instance:
def on_start():
{$BRANCH}
will replace the following codes
def on_start():
code1
code2
...
$ALL_VARIABLES
: variables of mBlock. For instance:
If variables va and vb are created
{{ "global" + $ALL_VARIABLES.join(', ') }} //javascript template
will generate
global va, vb
Transcoding language template
The general template is as follows:
# 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 #$$
#$$
Code field template
In the template import lib code
has to be configured in "Transcode settings" (###
refers to the left part, #$$
refers to the right part).
Javascript template
###{
(this.$ALL_VARIABLES.length !== 0)? this.$ALL_VARIABLES.map(n=> n + ' = 0').join('\n'):undefined
}#$$ // The returned value will be the placeholder here
Nesting template
###
while True:
# every tick
### loop #$$
#$$
// When the nested code contains valid value, corresponding code will be generated; if not, code will not be generated
Appendix I: the default transcoding language template
Arduino C:
// generated by mBlock5 for <your product>
// codes make you happy
//( include //)
//( lib //)
//( declare //)
//(
void setup() {
//( code //)
}
//)
//(
void loop() {
//( loop //)
}
//)
Python:
# generated by mBlock5 for <your product>
# codes make you happy
from codey import *
### import #$$
### lib #$$
###
# initialize variables
###{
(this.$ALL_VARIABLES.length !== 0)? this.$ALL_VARIABLES.map(function(n){return n + ' = 0'}).join('\n'):undefined
}#$$
#$$
### code #$$
###
while True:
# every tick
### loop #$$
#$$
Appendix II: transcoding template of variables
The template concerning the variables of blocks:
data_variable
{{this.getSafeName(this.VARIABLE)}}
data_setvariableto
{{this.getSafeName(this.VARIABLE)}} = {VALUE}
data_changevariableby
{{this.getSafeName(this.VARIABLE)}} = {{this.getSafeName(this.VARIABLE)}} + {VALUE}