本帖最后由 她叫小红 于 2016-12-28 13:54 编辑

       最近在学习mBlock扩展,想编写一个新的模块,根据虎子哥的“如何为mBlock添加自定义扩展模块”还有论坛上一篇相关的文档,可是试了很久都没有成功,添加扩展的的时候提示“该文件不是一个有效的ZIP扩展包!” 直接贴代码吧,希望大神帮助好学的孩子~~ .s2e文件:
      
[JavaScript] 纯文本查看 复制代码
{ 
        "extensionName": "Voice",
        "description": "Voice module",
        "version": "1.0",
        "author": "XiaoKai([url=mailto:850637704@qq.com]850637704@qq.com[/url])",
        "homepage": "http://bbs.makeblock.com",
        "sort":0,
        "javascriptURL":"js\voice.js",
          "firmware":"1.0",
          "extensionPort":0,
        "blockSpecs": 
        [
                [
                        "w",
                        "speak(%d.name)",
                        "speak",
                        "Forward",
                        {
                                /*"src":"Voice",*/
                                "inc":"#include \"RcVoice.h\"; \n",
                                "def":"RcVoice voice(2); \n",
                                "setup":"",
                                "work":"voice.speak({0}); \n",
                                "loop":""
                        }
                ]
        ],
        "menus": 
        {
                "name":
                [
                        "Forward",
                        "Backwards",
                        "TurnLeft",
                        "TurnRight"
                ]
        },
        "values":
        {
                "Forward"  :0,
                "Backwards":1,
                "TurnLeft" :2,
                "TurnRight":3
        },
        "translators":
        {
                "zh_CN":
                {
                        "Forward":"前进",
                        "Backwards":"后退",
                        "TurnLeft":"左转",
                        "TurnRight":"右转",
                        "speak(%d.name)":"语音提示(%d.name)"
                }
        }
}
   
.js文件:
        
[JavaScript] 纯文本查看 复制代码
// Voice.js

(function(ext) {
    var device = null;
    
        var levels = {
                Forward:0,
                Backwards:1,
                TurnLeft :2,
                TurnRight:3
        };
    
        ext.resetAll = function(){};
        
        ext.runArduino = function(){
                
        };
        ext.speak = function(level) {
       runPackage(52,levels[level]);
    };

    function processData(bytes) {
        trace(bytes);
    }
    
    function sendPackage(argList, type){
            var bytes = [0xff, 0x55, 0, 0, type];
            for(var i=0;i<argList.length;++i){
                    var val = argList[i];
                    if(val.constructor == "[class Array]"){
                            bytes = bytes.concat(val);
                    }else{
                            bytes.push(val);
                    }
            }
            bytes[2] = bytes.length - 3;
            device.send(bytes);
        }
        function runPackage(){
                sendPackage(arguments, 2);
        }

   // Extension API interactions
    var potentialDevices = [];
    ext._deviceConnected = function(dev) {
        potentialDevices.push(dev);
        if (!device) {
            tryNextDevice();
        }
    }

    function tryNextDevice() {
        // If potentialDevices is empty, device will be undefined.
        // That will get us back here next time a device is connected.
        device = potentialDevices.shift();
        if (device) {
            device.open({ stopBits: 0, bitRate: 115200, ctsFlowControl: 0 }, deviceOpened);
        }
    }

    var watchdog = null;
    function deviceOpened(dev) {
        if (!dev) {
            // Opening the port failed.
            tryNextDevice();
            return;
        }
        device.set_receive_handler('voice',processData);
    };

    ext._deviceRemoved = function(dev) {
        if(device != dev) return;
        device = null;
    };

    ext._shutdown = function() {
        if(device) device.close();
        device = null;
    };

    ext._getStatus = function() {
        if(!device) return {status: 1, msg: 'voice disconnected'};
        if(watchdog) return {status: 1, msg: 'Probing for voice'};
        return {status: 2, msg: 'voice connected'};
    }
    var descriptor = {};
        ScratchExtensions.register('voice', descriptor, ext, {type: 'serial'});
})({});


收藏
8 条回帖
她叫小红中级会员2016-12-28 14:17:16
刚刚我只把把.s2e中的代码添加到mBot的扩展包中,然后重启mBlock之后,在机器人模块中出现了我加入的积木,这个积木的Arduino离线模式是可以运行的,不加入对应的.js文件也是可以得么?但是我之前也只写了.s2e但是一样提示“扩展包不可用!”  我点懵现在,大神~   快快现身啊
她叫小红中级会员2016-12-28 14:18:31
她叫小红 发表于 2016-12-28 14:17
刚刚我只把把.s2e中的代码添加到mBot的扩展包中,然后重启mBlock之后,在机器人模块中出现了我加入的积木, ...

C:\Users\Administrator\Desktop\积木.png
她叫小红中级会员2016-12-28 14:23:06
她叫小红中级会员2016-12-28 14:40:08
她叫小红中级会员2016-12-28 14:43:21
她叫小红中级会员2016-12-28 14:45:32
她叫小红中级会员2016-12-28 14:46:53
不好意思,我想放一张图片上去,结果不会弄,试了这么多次才成功, 抱歉抱歉   
她叫小红中级会员2016-12-28 16:18:44
解决的,一个符号搞错了,"javascriptURL":"js\voice.js", 这句应该改为"javascriptURL":"js/voice.js",
需要登陆后才可进行回复 登录

返回顶部
现在加入我们,注册一个账号 账号登陆 QQ账号登陆 微博账号登陆