揭秘:慧编程的积木块是怎么来的?
第一次接触慧编程的时候,你是不是和我一样,有过这样的疑惑:
为什么在慧编程里,我们不需要像专业的开发者那样敲代码,只要拖动积木块就可以实现想要的功能?
其实,这里涉及到编程中所谈及的“封装”的概念。
所谓封装,就是把可以实现一段功能的算法写在一个子程序里(在一些主流的编程语言中也叫函数或者方法),然后给这个子程序起一个名字,我们想要实现这个功能的时候直接调用这个子程序就可以了,而这个子程序就是我们在慧编程里看到的各种积木块。
今天,我们特别邀请了慧编程的开发小哥,一起来看看他是怎么为慧编程创作积木块的吧~
注:本文将使用扩展设计器为慧编程添加新设备的积木块。扩展设计器是专门提供给用户快速开发扩展的一个工具,使用它,你只要略懂代码就可以支持想要的新设备/传感器/新功能啦~

1.登录慧编程扩展设计器
打开慧编程扩展设计器登录界面 https://ext.makeblock.com,输入邮箱和密码进入设计器主页。(若还没有帐号,可直接免费注册)

2.新建设备
在设计器主页的左侧点击“我的插件”,选择“新建设备”

3. 填写设备基本信息
填写“标识 ID ,版本号,名称”,上传设备图片,并勾选支持的平台,点击“确定”完成填写。
注:红框信息为必填,其他信息已默认设置好,除特殊情况外,一般不需要更改。

4. 为设备添加积木块
1)点击设备名称进入积木编辑页

2)点击“新增分类”

3) 输入分类名,点击“确定”完成积木分类的新建

4)现在,我们要为这个积木类型添加一个积木,点击积木列表最下面的“+”

5)为积木设置内容
注:空格+@即可添加参数

6)在 “下拉框”添加“高电平、低电平”选项

7)勾选支持的平台,点击“保存”

5.转码Arduino C
切换到“转码设置”,选择ArduinoC,点击“保存”

代码如下:
// generated by mBlock5 for// codes make you happy//( include //)//( 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 //)_loop();}这段代码用于延时以及自定义_loop

6.连接配置
1)切换到“连接配置”,设置vendorID为:0x2A03(这个参数作用是筛选串口,这里uno的vendorID是0x2A03)

2)配置中间件,名称设为“arduino”


3)切换到“上传代码驱动设置”,选择“stk500v1”,保存

7.上传转码给积木块
1) 回到“积木设置”,并切换到“上传转码”

2)配置如下内容(注:双击编辑区就可以编写代码啦~)
setup: pinMode(/*{number_1}*/,OUTPUT); // 这里/*{number_1}*/是变量的写法,里面的number_1为之前的数字口变量名code: digitalWrite(/*{number_1}*/,/*{fieldMenu_2}*/);
3)切换到“事件”分类,添加一个积木

4)输入内容如下,注意设置积木类型为“事件”

5)设置转码,点击“保存”
include: , , code: /*{$BRANCH}*/ // 这里 $BRANCH 指代的是与其拼接的积木的转码
8. 预览效果
1)点击页面右上角的“预览”

2)连接串口

3)编辑积木如图所示,然后点击慧编程页面左下角的“上传到设备”


4)至此,Uno黄灯会闪烁10下,说明代码已上传成功!我们为慧编程增加了一个新扩展啦~
我们再来探讨一下:为什么我们选择使用积木块来封装代码呢?
封装成积木块的代码看起来更简单,尤其对于想通过编程提高逻辑能力的小朋友来说,只要识字,通过认识积木块并加以练习,就可以用积木块实现想要的功能,进而创作出动画、游戏还有控制机器人!
--end--
声明:本文章由网友投稿作为教育分享用途,如有侵权原作者可通过邮件及时和我们联系删除:freemanzk@qq.com