首页 Scratch扩展

Scratch扩展

举报
开通vip

Scratch扩展CreatingScratch2.0Extensions进行Scratch2.0扩展JohnMaloney约翰·马拉纳MITMediaLaboratory麻省多媒体实验室September,20132013年9月NOTE1:Thisspecificationsupersedesallearlierdrafts.注解1:这份说明书取代了所有早期版本NOTE2:Thisspecificationisstillpreliminaryandmaychange.注解2:这份说明书仍然是初稿并且会被修改Introduction...

Scratch扩展
CreatingScratch2.0Extensions进行Scratch2.0扩展JohnMaloney约翰·马拉纳MITMediaLaboratory麻省多媒体实验室September,20132013年9月NOTE1:Thisspecificationsupersedesallearlierdrafts.注解1:这份说明书取代了所有早期版本NOTE2:Thisspecificationisstillpreliminaryandmaychange.注解2:这份说明书仍然是初稿并且会被修改Introduction介绍Scratch2.0canbeextendedtocontrolexternaldevices(e.g.roboticskits,musicalinstruments)andtoaccessdatafromexternalsensorhardware(e.g.sensorboards).AScratch2.0extensionextendsScratchwithacollectionofcommandandreporterblocksthatcanbeusedtointeractwithaparticulardevice.Whenanextensionisenabled,itsblocksappearinthe"MoreBlocks"palette.Scratch2.0可以被扩展到外部设备(例如:机器配件,乐器),也可以获取来自外部硬件传感器(例如:传感器板)的数据。Scratch2.0扩展模块通过命令收集和记录组块,使Scratch可以被用于和特殊设备进行互动。当扩展启动时,它的扩展组块在”MoreBlocks”控制面板显示。Duetobrowsersecurityrestrictions,Scratch2.0cannotinteractwithhardwaredevicesdirectly.Instead,hardwareextensionscomewithahelperapp,aseparateapplicationthattheusermustinstallandrunontheircomputer.ScratchcommunicateswiththehelperappviaHTTPrequests,andthehelpera ppt 关于艾滋病ppt课件精益管理ppt下载地图下载ppt可编辑假如ppt教学课件下载triz基础知识ppt alkstothehardware.Inthefuture,someextensionsmaypackagetheirhelperappsasbrowserplugins,butthatmechanismisnotdescribedhere.由于浏览器安全限制,Scratch2.0不能和硬件设备直接互动。而是通过一个用户必须安装和运行在电脑上的独立应用程序实现对硬件的扩展。Scratch通过HTTP请求与帮助程序交流,而帮助程序则和硬件通信。未来,一些拓展会把帮助程序打包成浏览器插件,但原理不在此描述。ThisdocumentisaimedatScratch2.0extensiondevelopers.Itdescribestheextensiondescriptionfileformat,theprotocolusedtocommunicatebetweenScratchextensionhelperapps,andtheextensiondevelopmentprocess.这个文档主要为Scratch2.0扩展研发者服务。这里描述了扩展开发过程的文件格式,曾经用于连接Scratch扩展辅助程序和扩展开发过程的协议。Extensiondescriptionfile扩展描述文件AnextensiondescriptionfileisatextfileinJSONformat(www.json.org)thatdescribestheextension.Byconvention,aScratch2.0extensionfileendsin.s2e.TheJSONobjectintheextensiondescriptionfileincludestheextension'sname,theTCP/IPportNumberusedtocommunicatewiththeextensionshelperapp,andalistofScratchblockspecifications.Hereisanexample:一个扩展描述文件是一个JSON格式的文本文件,用以描述扩展。通常,Scratch2.0扩展文件是以“.s2e”作为文件扩展名的。在扩展描述文件中,一个JSON实例包含扩展的名字,用来和辅助应用程序通信的TCP/IP端口号码和一组组件清单。下面是一个例子。{"extensionName":"ExtensionExample",//扩展组件集的名称"extensionPort":12345,//通信端口号码"blockSpecs":[//组块规格(包含详尽组块代码块)["","beep","playBeep"],["","setbeepvolumeto%n","setVolume",5],["r","beepvolume","volume"],]}Thisextension'snameis"ExtensionExample"anditconnectstoitshelperapponport12345.The"blockSpecs"fielddescribestheextensionblocksthatwillappearinthe"MoreBlocks"palette.Inthiscase,therearethreeblocks:(1)acommandblockthatplaysabeep;(2)acommandblockthatsetsthebeepvolume;and(3)areporter(value-returningblock)thatreportsthecurrentbeepvolumesetting.这个扩展的名字是“ExtensionExample”,它在12345端口上和辅助程序连接。“组块功能”字段描述了将出现在“MoreBlocks”调色板中的扩展组块。在这个例子中,有三个模块:(1)播放蜂鸣的命令模块;(2)设置蜂鸣声音大小的命令模块;(3)反馈当前奉命音量大小设置的反馈模块。Blockdescription块说明Eachblockisdescribedbyanarraywiththefollowingfields:每个块被带有字段的数组所描述:*blocktype//块类型*blockformat//块格式*operationorremotevariablename//操作或遥控变量名*(optional)zeroormoredefaultparametervalues//零和其他的默认参数值Theblocktypeisoneofthesestrings:///模块类型有如下字串""-commandblock//命令块"w"-commandblockthatwaits//等待命令块"r"-numberreporterblock(roundends)//数值反馈模块(两段圆)"b"-booleanreporterblock(pointyends)//真假反馈模块(两段尖)Theblockformatisastringthatdescribesthelabelsandparameterslotsthatappearontheblock.功能块格式是一个字符串,用来描述出现在功能块上的标签和参数插槽Parameterslotsareindicatedbyawordstartingwith"%"andcanbeoneof:参数插槽用“%”开头的一个词 关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf 示出来,有下面几种:%n-numberparameter(roundends)//数字参数(两段圆)%s-stringparameter(squareends)//字符串参数(两段方)%b-booleanparameter(pointyends)//真假参数(两段尖)Intheexample,the"setbeepvolumeto%n"takesonenumericparameter.Theblockdescriptionforthatcommandalsoincludesadefaultvalue:5.Thisdefaultvaluewillbedisplayedintheparameterslotwhenthatblockappearsinthepalette.Defaultparametersallowuserstoeasilytestblocksandsuggesttherangeofagivenparameter.在这个例子中,“setbeepvolumeto%n”设置了一个数字参数。功能块为那个命令描述并且设置了默认值5。当功能块出现在面板中时,这个默认值将显示在参数插槽中。默认参数让用户容易的测试功能块和提供一个给定参数的范围。Theoperationfieldinablockspecificationisusedintwoways.Forcommandblocks,itissenttothehelperapplication,alongwithanyparametervalues,toinvokeanoperation.Forreporterblocks,itisthenameofasensorvariable.Sensorvariablevaluesarekeptinadictionary.Executingareporterblocksimplyreturnsthemostrecentlyreportedvalueforthatsensorvariable.功能块清单上的操作字段有两个用途。用于命令功能块,它连同参数值被发送给辅助程序,从而调用操作。用于反馈功能块,它是传感器变量的名称。传感变量的值被保存在一个字典中。执行反馈功能块直接返回传感器变量的值。Menuparameters菜单参数Bothcommandandreporterblockscanincludemenuparameters:命令和反馈功能模块都包含菜单参数%m.menuName-menuparameter(noteditable)菜单参数(不可以编辑)%d.menuName-editablenumberparameterwithmenu可编辑的菜单参数Thefirstoftheseprovidesasimpledrop-downmenuparameterslot,similartotheparameterofthe"broadcast"block.Thesecondprovidesanumericparameterslotwithanauxiliarymenu,similartothe"pointindirection"block.Inbothofthesecases,thestringaftertheperiodcharacteristhenameofthemenu.Thecontentsofthemenuaresuppliedbytheoptional"menu"fieldintheextensiondescriptor.Forexample,aMicrosoftKinectextensionmightlooklikethis:第一部分提供了一个简单的下拉菜单参数插槽,类似于“广播节目”功能的参数。第二部分提供了一个辅助菜单数字参数插槽,类似于“地址标点”。在这两种情况中,在终结字符后的字符串就是菜单的名字。菜单中的内容由扩展描述符中的可选菜单字段提供。例如,“MicrosoftKinect”扩展有可能是这样的:{"extensionName":"Kinect","extensionPort":12345,"blockSpecs":[["r","get%m.coordinatepositionof%m.bodyPart","position"],],"menus":{"coordinate":["x","y","z"],"bodyPart":["head","shoulder","elbow","hand"],},}Communicatingwiththehelperapp和辅助程序通信Ahelperapprunsinthebackground,readyforusebyScratchprojectsthatusesthatextension.Eachextensionhasauniqueportnumber.Scratchlooksforthehelperappatthegivenportnumberonthelocalcomputer.辅助程序在后台运行,为Scratch项目使用扩展做准备。每个扩展都有一个唯一的端口号。Scratch在本机上从已知的端口号上寻找辅助程序。ScratchcommunicateswiththehelperappusingtheHTTPprotocol.ScratchsendscommandstothehelperappandthehelperappsendssensorvaluesandstatusinformationbacktoScratchviaHTTPGETrequests.SincetheprotocolisstandardHTTP,anybrowsercanbeusedtotestanddebughelperapps.Scratch使用HTTP协议和辅助程序通信。Scratch发送命令给辅助程序,辅助程序用HTTPGet请求发送传感器数值和状态信息给Scratch。因为协议是 标准 excel标准偏差excel标准偏差函数exl标准差函数国标检验抽样标准表免费下载红头文件格式标准下载 的HTTP,任何浏览器都可以测试和调试辅助程序。Polling反复检测Scratchtoretrievessensorvaluesandstatusinformationfromthehelperappbysendingapollcommand:Scratch通过发送轮询命令来获取来自辅助应用的传感器值和状态信息:Inresponsetoapollcommand,thehelperappsendsbackalistof(sensorname,value)pairs,onepairperline.Eachlineinthepollresponseshouldendwithanewlinecharacter(0xA)andthesensornameandvalueshouldbeseparatedbyaspacecharacter.Stringvaluesshouldbe“URLencoded”.Scratchsendspollcommandsroughly30timespersecond.响应轮询命令时,辅助程序返回(传感器名称,值)一对的列表,每对一行。每行轮询反馈应该以换行符(0xA)结束,传感器名和数值应该被空格隔开。字符串数值应该是URL编码。Scratch每秒30次循环发送轮询命令。Here'sanexamplepollresponse:下面是一个轮询响应的例子Brightness75亮度75slider17滑块值17Reportingproblems问 快递公司问题件快递公司问题件货款处理关于圆的周长面积重点题型关于解方程组的题及答案关于南海问题 报告 软件系统测试报告下载sgs报告如何下载关于路面塌陷情况报告535n,sgs报告怎么下载竣工报告下载 Acommonproblemwithhardwareextensionsisthattherequiredhardwareisnotattachedtothecomputer.Theresponsetoapollrequestcanincludeaproblemreporttohelpuserstroubleshoot.Aproblemreportconsistsofthestring"_problem"followedbyaspacefollowedbyashortdescriptionoftheproblem.Forexample:_problemTheScratchSensorboardisnotconnected.硬件扩展很普遍的一个问题是要求的硬件没有连接到电脑。ReservedsensornamesScratchusesspecialsensornamestoreportextensionproblems,trackbusycommands,etc.Thesespecialnamesalwaysstartwithanunderscorecharacter("_").Toavoidpossiblenameconflictswithpotentialfuturefeature,extensionswritersshouldavoidstartingeithercommandorsensornameswithanunderscore.CommandsAcommandanditsparametersareformattedasaURLrequest,usingtheforwardslash("/")asaseparator.Someexamples:/beep(commandwithnoparameters)/setVolume/5(commandwithanumericparameter)StringparametersmustbeURL-encodedsothattherequestisawell-formedURL.CommandsthatwaitSomecommandswaituntilsomeactioncompletes.Forexample,theblock:turnmotoronfor3secondsturnsonthemotor,waitsthreeseconds,thenturnsitoffagain.Whenthisblockisusedinascript,executiondoesnotcontinuetothenextblockuntilthecommandcompletes.Acommandthatwaitsisindicatedbythe"w"blocktypeinthecommanddescriptor.Whena"w"commandisinvoked,Scratchaddsauniquecommand_idparametertotherequest(beforeanyotherparameters).Forexample,forthemotorcommandaboveScratchwouldsend:/motorOn/2437/3Thefirstparameter,2437,isauniqueidentifierforthisinvocationofthecommand.Forthethreesecondsthatthiscommandtakestocomplete,thehelperappaddsabusylinetothepollrequest:_busy2437…Abusylineconsistsofthestring"_busy"followedbyalistofuniqueidentifiersseparatedbyspaces.WhenScratchgetsapollresultthatdoesn'tinclude2437inthebusyline(ordoesn'tevenhaveabusyline),itknowsthatthecommandiscompleteandallowsthescriptthatinvokedthatcommandtoproceed.Reportersthatwait(futurefeature)Note:ThisfeatureisnotyetimplementedbecausetheScratchinterpreterdoesnotyetsupportreporterblocksthatcansuspendtheirthreaduntiltheresultisready.Inthefuture,theextensionmechanismmaybeextendedtosupportextensionreporterblocksthatrequestdatafromthehelperappandandwaituntiltheresultisreturnedbeforeproceeding.Tohandlesuchrequests(sometimecalleda"blocking"request),Scratchwilladdauniquerequest_idparametertotherequest(beforeanyotherparameters).Forexample,aweatherextensionmightincludeablocktogetthecurrenttemperatureinagivencity:temperatureincity_nameTogetthetemperatureinBoston,thiscommandwouldsendthehelperapparequestlikethis:/getTemperature/7639/BostonThefirstparameter,7639,isauniquerequestIDstring.Inresponse,thehelperappwouldinitiatearequesttogettheweatherinformationforBoston.Sometimelater,whentheresultofthatrequestisreturnedfromtheserver,thehelperappwouldincludethefollowinglineinitsresponsetothenextpollrequest:_result763982Thislineconsistsofthespecialstring"_result",therequestID,andtheresultvalueseparatedbyspacecharacters.Aswithothersensorvalues,resultvalueshouldbeURL-encoded.ResetcommandScratchextensionscancontrolmotorsormusicsynthesizers.Usersexpecttobeabletostopeverything--turnoffmotors,silencemusicsynthesizers,andresethardwarebacktoit'soriginalstate--byclickingthestopbuttonintheScratcheditor.Thus,whenthestopbuttonisclicked,Scratchsendsaresetcommandtoallactiveextensions:/reset_allInresponsetothiscommand,thehelperappshouldturnoffmotors,lights,sounds,etc.andresetthehardwaretoitspower-upstate.Forexample,iftheScratchprogramhadsetthevolumeofmusicsynthesizertozero,itshouldberesettoit'sdefaultlevel.Thehelperappshouldalsocancelanyongoingprocessesandclearitslistofbusycommands.Cross-DomainpolicyrequestsThere'soneotherrequirementforthehelperapp:itmustrespondtoFlash'srequestforacrossdomainpolicyfile.ThisgivesFlashpermissiontosendHTTPrequeststothehelperapp.Flashsendsthefollowingrequesttotheapp:/crossdomain.xmlThehelperappmustrespondbysendinganull-terminatedpolicyfilelikethis:"/>whereistheportnumberofthehelperapp.BesuretoincludeazerobyteafterthepolicyfileorFlashwon'trecognizeit.Unfortunately,Flashdoesn'tallowawildcard(asterisk)inthe"to-ports"field,soyouhavetofillthatinwiththecorrectportnumber.Theexamplehelperappcodeshowshowtohandleapolicyrequest.OnceFlashhasreceivedandcheckedthepolicyrequest,itcancommunicatewiththehelperapp.BuildingandTestingExtensionsTheScratch2.0extensionmechanismisstillunderdevelopment.Eventually,theuserwillbeabletobrowseandimportextensionsfromalibraryofpublishedextensions,justastheycurrentlyimportcostumes,backdrops,spritesandsoundsfromtheScratchmedialibrary,butthatmechanismisnotyetimplemented.Meanwhile,toallowextensiondevelopmentandtesting,asemi-hiddenmenucommandcanbeusedtoimportanextensionfromalocalfile.HerearethestepsforcreatingandtestingaScratchextension:1.Createanextensiondescriptionfile2.Createyourhelperappandstartit3.OpentheScratch2OfflineEditor4.Importtheextensiondescription(shift-clickon"File"andselect"ImportExperimentalExtension"fromthemenu)5.ThenewextensionblockswillappearintheMoreBlockspalette6.Testyourextensionanditerate!Helperappscanbewritteninanylanguagethatsupportsserversockets,suchasPython,Node.js,Java,C,etc.Eventually,theScratchTeammayassignsocketnumbersforpopularextensionstoavoidpossibleconflicts.Fornow,anyunusedsocketnumberover1024canbeused.DistributingExtensionsAsofthiswriting,anextensiondistributionstrategyisstillbeingworkedout.TheScratchteamwillprobablyhostasmalllibraryof"supported"extensions.UserswillbeabletobrowseandselectextensionsfromthislibraryfromwithintheScratcheditor,andwillbeabletodownloadthenecessaryhelperappfromtheScratchwebsite.SupportedextensionswouldbecheckedforqualityandsafetybytheScratchteam.TherearelikelybestrictcriteriaforincludinganextensionintheScratch-teamsupportedextensionslibrary,suchascommandsetclarityandeaseofuse,sizeofthepotentialaudience,wide-spreadavailabilityofanyassociatedhardware,andalong-termcommitmenttosupporttheextension.ToavoidpotentialconfusionontheScratchwebsite,projectswithunsupportedextensionsshouldonlybeusedwiththeScratchOfflineEditorandshouldbesavedonlylocally,notuploadedtothewebsite.Extensiondeveloperscansharetheirextensionsbydistributingcopiesoftheextensiondescriptionfileandhelperapp.Userswouldthenusethe"ImportExperimentalExtension"commandintheOfflineeditortoimporttheextension.ProtocolSummaryScratchsendsthefollowingcommandstoahelperapp:/poll/reset_allTheresponsetoa/pollrequestisoneormorelinesseparatedbynewlinecharacters(0xA).Theresponsemayincludeoneormoreofthefollowing:-asensornameandvalueseparatedbywhitespace-"_busy"followedbyalistofuniqueID'sforcommandsinprogress-"_problem"followedbyaproblemdescription-"_result"followedbyauniqueIDandvalue(notyetimplemented)
本文档为【Scratch扩展】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑, 图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。
下载需要: ¥14.0 已有0 人下载
最新资料
资料动态
专题动态
个人认证用户
仙人指路88
暂无简介~
格式:doc
大小:165KB
软件:Word
页数:0
分类:小学语文
上传时间:2021-07-21
浏览量:138