在“上传模式”下慧编程会将工作区(workspace)转为 python / c /javascript
语言,并由用户主动上传到硬件设备上。
配置转码语言
以 Python 为例。将支持的转码语言设置为 Python 后,配置其转码模板。
配置 Python 的转码模板
**
# 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 #$$
这份模板声明了最终转码的样式,你可以在代码对照区看到实际发生的转码:
模板的结构
**### ???
表示一个模板样式,比如:
### import #$$
表示了这里将会实例成 import字段### code #$$
表示了这里将会实例成 code 字段
对应每个块 上传转码 配置
如果有多个块拥有同一个字段,转码过程中,会将同一个字段的多个值进行全并,如下:
import = 积木块A的 import 字段 + 积木块B的 import 字段...
这个过程叫做 转码器,通常来说,你不需要修改转码器默认实现,但不符合预期时,可以在 转码设置/转码器 里进行配置。如下为 Python 示例:
转码模板说明
转码模板指的是将参数(占位符)嵌入到模板字符串中,运行时刻生成实际代码。在扩展设计器里存在两类模板。
用于生成一个积木块的代码,如 codey 的 当按下按钮(A)。
@event.button_{BUTTONS}_pressed
def on_button_{BUTTONS}_pressed{$INDEX}():
{$BRANCH}
这里 BUTTONS
为该积木块配置的参数名,{BUTTONS}
会替换成该积木块实际的参数值。
积木块模板语法说明:
template
可以是 一般参数:如{args_1}
template
可以嵌套: 如{i am {args_1} }
, 求值策略为,如果args_1
为“somebody”
,该模板会实例化为"i am somebody"
,如果为空,整体代码生成为空("")
template
可以嵌入 Javascript 代码, 规则为{ ... }+right
, 无空格,例:{{ console.log('log'), 123 }}
, 打印后,求值为123
下面是例子:
如果积木块的的实际参数如下:
name: move
speed: 50
time: 10
模板转码规则如下:
{name}()
将翻译成move()
{name}({speed}, {time})
将翻译成move(50, 10)
{ {name} }
将翻译成move
// 模板内的模板,先求值内部模板,(注意这里有空格){no_exist}
将翻译成空
//因为该模板变量不存在{ time: {time} }
将翻译成time: 10
//模板内的模板{ time: {no_exist} }
将翻译成空
,//模板内的模板如果未赋值, 外部模板求值 为空{ {no_exist} }
将翻译成空
//模板内的模板如果未赋值, 外部模板求值 为空(注意空格)asdf
将翻译成asdf
//表示模板里的js表达式3
将翻译成3
//表示模板里的js表达式
另外,设计器提供了一些”伪参数“,这些伪参数有特别的意义:
$INDEX
:(适用于帽子块)同一类型积木块的序号,例子:
def on_start{$INDEX}():,
第一次转换时生成 def on_start():
第二次转换成生成 def on_start1():
$BRANCH
: 指带帽子头以下代码生成,例子:
def on_start():
{$BRANCH}
将会替换该块以下的代码生成
def on_start():
code1
code2
...
$ALL_VARIABLES
:慧编程的变量(VARIABLES), 例子如下:
如果创建了变量 va, vb
{{ "global" + $ALL_VARIABLES.join(', ') }} //javascript 模板
将会生成
global va, vb
代码框架模板
整体代码结构的模板,如:
# 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 #$$
#$$
字段模板
在框架里的 import lib code
, 为在设计器里需要配置的字段, (###
为模板左括号, #$$
为模板右括号)
Javascript 模板
###{
(this.$ALL_VARIABLES.length !== 0)? this.$ALL_VARIABLES.map(n=> n + ' = 0').join('\n'):undefined
}#$$ // 这里javascript求值后回的字串在此占位
嵌套模板
###
while True:
# every tick
### loop #$$
#$$
// 当嵌入的字段(这里是loop) 存在值时, 整体代码会生成,loop字段为空时,从while起始的整体代码段不生成
附录一:Arduino C 框架模板
// 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();
}
附录二:Python 框架模板**
**
# 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 #$$
#$$