匿名
未登录
登录
丢石头百科
搜索
查看“Micro:bit系列教程25:自定义Micro:bit软件”的源代码
来自丢石头百科
名字空间
页面
讨论
更多
更多
页面选项
查看
查看源代码
历史
←
Micro:bit系列教程25:自定义Micro:bit软件
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
<p class="MsoNormal" style="text-indent:21.0pt;line-height:150%"><span style="font-size:12.0pt;line-height:150%;font-family:宋体">当要使用<span lang="EN-US">Micro:bit</span>驱动非板载外设时,制作专用软件包并添加到项目,可提高程序的重复利用率,同时提高开发效率。<span lang="EN-US"></span></span></p> <p class="MsoNormal" style="text-indent:21.0pt;line-height:150%"><span style="font-size:12.0pt;line-height:150%;font-family:宋体">本节将介绍如何制作<span lang="EN-US">Micro:bit</span>扩展软件包:<span lang="EN-US"></span></span></p> <h1><span lang="EN-US" style="font-size:15.0pt;line-height:240%;font-family:黑体"><span style="mso-tab-count:1"> </span>1.</span><span style="font-size:15.0pt; line-height:240%;font-family:黑体">搭载环境<span lang="EN-US"></span></span></h1> <h2><span lang="EN-US" style="font-size:12.0pt;line-height:173%;font-family:宋体"><span style="mso-tab-count:1"> </span></span><span lang="EN-US" style="font-size:12.0pt; line-height:173%;font-family:黑体">1.1.</span><span style="font-size:12.0pt; line-height:173%;font-family:黑体">安装<span lang="EN-US">node.js</span></span></h2> <p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体">下载地址:</span><span lang="EN-US"><span style="font-size:12.0pt;line-height:150%; font-family:宋体">https://nodejs.org/en/</span></span><span style="font-size: 12.0pt;line-height:150%;font-family:宋体">,根据<span lang="EN-US">32</span>位或<span lang="EN-US">64</span>位系统下载不同版本,并安装,推荐安装官方推荐版本:<span lang="EN-US"></span></span></p> <p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-0.png|400px]]</p> <p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体">检查是否安装成功:打开<span lang="EN-US">Windows</span>命令行,输入<span lang="EN-US">node -v,</span>输出版本号则表明安装成功:<span lang="EN-US"></span></span></p> <p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-1.png|400px]]</p> <p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体">若未正常显示版本号,请将安装路径添加到系统环境变量,或重新安装<span lang="EN-US">node.js</span></span></p> <h2><span lang="EN-US" style="font-size:12.0pt;line-height:173%;font-family:黑体">1.2.</span><span style="font-size:12.0pt;line-height:173%;font-family:黑体">安装<span lang="EN-US">pxt</span>命令行工具<span lang="EN-US"></span></span></h2> <p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体"><span style="mso-tab-count:1"> </span></span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">打开<span lang="EN-US">Windows</span>命令行,输入:<span lang="EN-US"></span></span></p> <pre style="line-height:150%;tab-stops:21.75pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt"><span lang="EN-US"><span style="mso-tab-count:1"> </span></span><span style="mso-bookmark:_Hlk524710277"></span><span courier="" lang="EN-US" new="" style="mso-bidi-font-family: ">npm install -g pxt</span></pre> <h2> <span style="font-weight: 400; font-size: 12pt; line-height: 24px; font-family: 宋体;">打若出现以下提示,则成功安装:</span></h2><div><span style="font-weight: 400; font-size: 12pt; line-height: 24px; font-family: 宋体;"><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-2.png|400px]]</p></span></div><h2><span lang="EN-US" style="font-size:12.0pt;line-height:173%;font-family:黑体">1.3.</span><span style="font-size:12.0pt;line-height:173%;font-family:黑体">安装<span lang="EN-US">Visual Studio Code</span></span></h2> <p class="MsoNormal" style="text-indent:21.0pt;line-height:150%"><span style="font-size:12.0pt;line-height:150%;font-family:宋体">下载地址:</span><span lang="EN-US"><span style="font-size: 12.0pt;line-height:150%;font-family:宋体">https://code.visualstudio.com/</span></span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">,根据<span lang="EN-US">32</span>位或<span lang="EN-US">64</span>位系统选择不同的版本下载安装,该工具为官方推荐工具,编辑<span lang="EN-US">TypeScript</span>脚本时有语法提示,错误检查等,推荐使用。<span lang="EN-US"></span></span></p> <p class="MsoNormal" style="text-indent:21.0pt;line-height:150%"><span style="font-size:12.0pt;line-height:150%;font-family:宋体">检查是否安装成功:打开<span lang="EN-US">Windows</span>控制台,输入<span lang="EN-US">code -v,</span>输出版本号则表明安装成功:<span lang="EN-US"></span></span></p> <p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-3.png|400px]]</p> <p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体">若未正常显示版本号,请将安装路径添加到系统环境变量,或重新安装<span lang="EN-US">Visual Studio Code</span>。<span lang="EN-US"></span></span></p><p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体"></span></p><h2><span lang="EN-US" style="font-size: 12pt; line-height: 27.68px; font-family: 黑体;">1.4</span><span lang="EN-US" style="font-size: 12pt; line-height: 27.68px; font-family: 黑体;">.</span><span style="font-size: 12pt; line-height: 27.68px; font-family: 黑体;">安装Git</span></h2><div><span style="font-size: 10.5pt; font-family: 等线; color: rgb(51, 51, 51);">下载地址:</span><span lang="EN-US" style="font-size: 10.5pt; font-family: 等线;"><span courier="" new="" style="font-family:">https://git-scm.com/</span></span><span style="font-size: 10.5pt; font-family: 等线; color: rgb(51, 51, 51);">,</span><span style="font-size: 10.5pt; font-family: 等线;">根据<span lang="EN-US">32</span>位或<span lang="EN-US">64</span>位系统选择不同的版本下载安装,发布软件包的时候需要用到该软件。</span></div><div><h2><span lang="EN-US" style="font-size:12.0pt;line-height:173%;font-family:黑体">1.5</span><span lang="EN-US" new="" roman="" style="font-size:12.0pt;line-height:173%;font-family:黑体;mso-bidi-font-family: " times="">.</span><span new="" roman="" style="font-size:12.0pt;line-height:173%;font-family:黑体;mso-bidi-font-family: " times="">加载工具链<span lang="EN-US"></span></span></h2><div><span style="line-height: 173%;"><div style=""><font face="黑体"><span style="font-size: 16px;">新建文件夹,(注意:制作的软件包也需放至该文件夹的子文件夹下,本次操作是在E:\Program\Project\MicroBit_Project\Server路径建立microbit文件夹)</span></font></div><div style=""><font face="黑体"><span style="font-size: 16px;">在microbit文件夹下加载Micro:bit工具链,打开Windows命令行进入该文件夹,输入:</span></font></div><div style=""><div><pre class="brush:cpp;auto-links:false;">pxt target microbit</pre></div> <br/></div><div style=""><font face="黑体"><span style="font-size: 16px;">该过程可能持续几分钟,如果出现如下提示,则表明Micro:bit工具链加载成功:</span></font></div><div style=""><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-4.png|400px]]</p><p>此时输入:</p><div><pre class="brush:cpp;auto-links:false;">pxt help</pre></div><p>可查看pxt的命令行帮助:</p><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-5.png|400px]]</p><p>打开microbit文件夹,可见该文件夹下新增如下文件:</p><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-6.png|400px]]</p><h1><span lang="EN-US" style="font-size: 15pt; line-height: 48px; font-family: 黑体;"> 2.生成软件包所需文件</span></h1><div><span lang="EN-US" style="line-height: 48px;"><div style="">在microbit文件夹下,建立projects文件夹,(注意,必须命名为projects,当执行pxt serve命令时,将会寻找projects文件夹)。</div><div style="">进入projects文件夹,新建文件夹,并以软件包名称命名(最终会将该文件夹下所有文件上传至Github,本次操作中,在projects文件夹下新建MCP23017文件夹),如下图所示:</div><div style=""><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-7.png|400px]]</p><p>打开Windows命令行,进入到新建的以软件包名称命名的文件夹 (本次进入到MCP23017文件夹),执行以下命令,生成软件包所需的部分文件:</p><div><pre class="brush:cpp;auto-links:false;">pxt init</pre></div><p> </p><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-8.png|400px]]</p><p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体">此操作需输入相关信息,分别如下:<span lang="EN-US"></span></span></p><p class="MsoNormal" style="text-indent:21.0pt;line-height:150%"><span lang="EN-US" style="font-size:12.0pt;line-height:150%;font-family:宋体">name</span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">:模块名称,为软件包名称,该名称将来也将显示到<span lang="EN-US">makecode</span>界面中。<span lang="EN-US"></span></span></p><p class="MsoNormal" style="text-indent:21.0pt;line-height:150%"><span lang="EN-US" style="font-size:12.0pt;line-height:150%;font-family:宋体">description</span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">:对软件包功能的描述,用<span lang="EN-US">makecode</span>加载软件包时将展示。<span lang="EN-US"></span></span></p><p> </p><p class="MsoNormal" style="text-indent:21.0pt;line-height:150%"><span lang="EN-US" style="font-size:12.0pt;line-height:150%;font-family:宋体">license:</span><span lang="EN-US"> </span><span style="font-size:12.0pt;line-height:150%;font-family: 宋体">软件授权条款</span></p><p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体">输入相关信息后,<span lang="EN-US">MCP23017</span>文件夹下新增了如下文件:<span lang="EN-US"></span></span></p><p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体"></span></p><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-9.png|400px]]</p><p><br/></p><p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体">其中<span lang="EN-US">pxt.json</span>则记录了模块名称,软件包描述,软件授权条款等信息,如下图所示:</span></p><p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体"></span></p><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-10.png|400px]]</p><p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体">数据以<span lang="EN-US">json</span>键值对的形式存储,如果需要修改数据,修改键对应的值即可。<span lang="EN-US"></span></span></p><h1><span lang="EN-US" style="font-size:15.0pt;line-height:240%;font-family:黑体">3.</span><span style="font-size:15.0pt;line-height:240%;font-family:黑体">设计驱动代码及图形块<span lang="EN-US"></span></span></h1><p class="MsoNormal" style="text-indent:21.0pt;line-height:150%"><span style="font-size:12.0pt;line-height:150%;font-family:宋体">打开<span lang="EN-US">Windows</span>命令行,进入到新建的以软件包名称命名的文件夹<span lang="EN-US"> (</span>本次进入到<span lang="EN-US">MCP23017</span>文件夹<span lang="EN-US">)</span>,输入以下命令,采用<span lang="EN-US">Visual Studio Code </span>打开该项目<span lang="EN-US">:</span></span></p><p class="MsoNormal" style="text-indent:21.0pt;line-height:150%"><span lang="EN-US" style="font-size:12.0pt;line-height:150%;font-family:宋体">code .</span></p><p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体">输入回车后,<span lang="EN-US">VS Code</span>打开了该项目<span lang="EN-US">(pxt init</span>命令生成的就是一个<span lang="EN-US">VS Code</span>项目<span lang="EN-US">)</span>:<span lang="EN-US"></span></span></p><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-11.png|400px]]</p><p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体">由于是制作软件包,不需要<span lang="EN-US">main.ts</span>文件,因此,将<span lang="EN-US">main.ts</span>重命名为 <span lang="EN-US">mcp23017.ts</span>,并在该文件下采用<span lang="EN-US">TypeScrip</span>编写<span lang="EN-US">MCP23017</span>的驱动代码,设计图形块<span lang="EN-US">(</span>图形块与注释相关联,图形块设计请参考:<span lang="EN-US"></span></span></p><p class="MsoNormal" style="line-height:150%"><span lang="EN-US"><span style="font-size:12.0pt; line-height:150%;font-family:宋体">https://makecode.com/defining-blocks</span></span><span lang="EN-US" style="font-size:12.0pt;line-height:150%;font-family:宋体"></span></p><p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体">),</span><span style="font-size:12.0pt; line-height:150%;font-family:宋体">之后,将<span lang="EN-US">pxt.json</span>文件中<span lang="EN-US">”file”:</span>下的<span lang="EN-US">main.ts</span>修改为<span lang="EN-US">MCP23017.ts</span>,如下图所示: <span lang="EN-US"></span></span></p><p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体"></span></p><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-12.png|400px]]</p><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-13.png|400px]]</p><p> </p><p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体">MCP23017</span><span style="font-size: 12.0pt;line-height:150%;font-family:宋体">驱动及图形块代码如下图所示:<span lang="EN-US"></span></span></p></div></span></div></div><div style="font-family: 黑体; font-size: 12pt;"><div><h3> cpp代码:</h3><syntaxhighlight lang="c">/** * 使用此文件来定义自定义函数和图形块。 * 想了解更详细的信息,请前往 https://makecode.microbit.org/blocks/custom */ /** * 自定义图形块 */ enum REGISTER { IODIRA = 0x00, IODIRB = 0x01, IPOLA = 0x02, IPOLB = 0x03, GPINTENA = 0x04, GPINTENB = 0x05, DEFVALA = 0x06, DEFVALB = 0x07, INTCONA = 0x08, INTCONB = 0x09, IOCONA = 0x0A, IOCONB = 0x0B, GPPUA = 0x0C, GPPUB = 0x0D, INTFA = 0x0E, INTFB = 0x0F, INTCAPA = 0x10, INTCAPB = 0x11, GPIOA = 0x12, GPIOB = 0x13, OLATA = 0x14, OLATB = 0x15 } enum PIN { A = 0, B = 1 } //% weight=5 color=#9900CC icon="\uf53b" namespace MCP23017 { const MCP23017_ADDRESS = 0x20 const MCP23017_IODIRA = 0x00 const MCP23017_IPOLA = 0x02 const MCP23017_GPINTENA = 0x04 const MCP23017_DEFVALA = 0x06 const MCP23017_INTCONA = 0x08 const MCP23017_IOCONA = 0x0A const MCP23017_GPPUA = 0x0C const MCP23017_INTFA = 0x0E const MCP23017_INTCAPA = 0x10 const MCP23017_GPIOA = 0x12 const MCP23017_OLATA = 0x14 const MCP23017_IODIRB = 0x01 const MCP23017_IPOLB = 0x03 const MCP23017_GPINTENB = 0x05 const MCP23017_DEFVALB = 0x07 const MCP23017_INTCONB = 0x09 const MCP23017_IOCONB = 0x0B const MCP23017_GPPUB = 0x0D const MCP23017_INTFB = 0x0F const MCP23017_INTCAPB = 0x11 const MCP23017_GPIOB = 0x13 const MCP23017_OLATB = 0x15 let initialized = false function i2cwrite(addr: number, reg: number, value: number): void { let buf = pins.createBuffer(2); buf[0] = reg; buf[1] = value; pins.i2cWriteBuffer(addr, buf); } function i2cread(addr: number, reg: number): number { pins.i2cWriteNumber(addr, reg, NumberFormat.UInt8BE); let val = pins.i2cReadNumber(addr, NumberFormat.UInt8BE); return val; } function initMCP23017(): void { for (let regAddr = 0; regAddr < 22; regAddr++) { if (regAddr == 0 || regAddr == 1) { i2cwrite(MCP23017_ADDRESS, regAddr, 0xFF); } else { i2cwrite(MCP23017_ADDRESS, regAddr, 0x00); } } //configue all PinA output i2cwrite(MCP23017_ADDRESS, MCP23017_IODIRA, 0x00); //configue all PinB input i2cwrite(MCP23017_ADDRESS, MCP23017_IODIRB, 0xFF); //configue all PinB pullUP i2cwrite(MCP23017_ADDRESS, MCP23017_GPPUB, 0xFF); initialized = true; } /** *Read data from the register * @param reg [0-21] register of mcp23017; eg: 0, 15, 23 */ //% blockId=ReadReg block="Read register |%reg| data" //% weight=65 export function ReadReg(reg: REGISTER): number { let val = i2cread(MCP23017_ADDRESS, reg); return val; } /** * WriteData to PinA or PinB * @param pin [0-1] choose PinA or PinB; eg: 0, 1 * @param value [0-255] pulse of servo; eg: 128, 0, 255 */ //% blockId=WritePin block="Set P |%pin| value |%value|" //% weight=75 //% value.min=0 value.max=255 export function WritePin(pin: PIN, value: number): void { if (!initialized) { initMCP23017(); } if (pin == 0) { i2cwrite(MCP23017_ADDRESS, MCP23017_GPIOA, value); } else { i2cwrite(MCP23017_ADDRESS, MCP23017_GPIOB, value); } } /** *ReadData From PinA or PinB * @param pin [0-1] choose PinA or PinB; eg: 0, 1 */ //% blockId=ReadPin block="Read data from |%pin|" //% weight=85 export function ReadPin(pin: PIN): number { if (!initialized) { initMCP23017(); } if (pin == 0) { let val = i2cread(MCP23017_ADDRESS, MCP23017_GPIOA); return val; } else { let val = i2cread(MCP23017_ADDRESS, MCP23017_GPIOB); return val; } } } </syntaxhighlight></div> <h1><span lang="EN-US" style="font-size: 15pt; line-height: 240%;">4.</span><span style="font-size: 15pt; line-height: 240%;">编写测试代码<span lang="EN-US"></span></span></h1> <p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体"> </span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">在采用<span lang="EN-US">pxt init </span>生成的文件中,<span lang="EN-US">test.ts</span>文件为测试文件,当作为软件包添加到其它项目中时,该文件不会被编译,而作为开发流程中的功能测试时,该文件将会被编译,且单独执行。<span lang="EN-US"></span></span></p> <p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体"> </span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">现编写测试代码,对输出功能进行测试<span lang="EN-US">:</span></span></p> <p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-14.png|400px]]</p> <p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体"> </span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">在该测试代码中,其操作为对<span lang="EN-US">MCP23017</span>的<span lang="EN-US">A</span>引脚的输出寄存器写入十进制数值:<span lang="EN-US">85,</span>其二进制为:<span lang="EN-US">01010101</span>,对应<span lang="EN-US">PA0</span>至<span lang="EN-US">PA7</span>的电平状态分别为:高低高低高低高低。<span lang="EN-US"></span></span></p> <p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体"> </span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">此次测试代码为:<span lang="EN-US"></span></span></p><div><h3> cpp代码:</h3><pre class="brush:cpp;auto-links:false;">/* tests go here; this will not be compiled when this package is used as a library*/ basic.forever(() => { MCP23017.WritePin(0,85); }) </pre></div><h1><span lang="EN-US" style="font-size: 15pt; line-height: 240%;">5.</span><span style="font-size: 15pt; line-height: 240%;">编译检查,测试<span lang="EN-US"></span></span></h1><p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体"> </span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">先进行语法检查,如果有语法出错,<span lang="EN-US">VS code </span>会进行提示,及时在<span lang="EN-US">VS Code</span>中改正即可:<span lang="EN-US"></span></span></p><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-15.png|400px]]</p><p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体"> </span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">打开<span lang="EN-US">Windows</span>命令行,进入到新建的以软件包名称命名的文件夹<span lang="EN-US"> (</span>本次进入到<span lang="EN-US">MCP23017</span>文件夹<span lang="EN-US">)</span>,进行以下操作:<span lang="EN-US"></span></span></p><p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体"> </span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">编译:<span lang="EN-US">(</span>可选<span lang="EN-US">) </span>在命令行输入:<span lang="EN-US">pxt build,</span>该步骤主要方便检查语法错误,实际中,<span lang="EN-US">VS Code</span>能够提示语法错误,可先在<span lang="EN-US">VS Code</span>中修改语法错误。<span lang="EN-US"></span></span></p><p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体"> </span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">编译并下载:<span lang="EN-US">(</span>可选<span lang="EN-US">)</span>在命令行输入:<span lang="EN-US">pxt deploy,</span>该步骤为先编译,编译成功后直接下载,实际中,通过<span lang="EN-US">makecode</span>进行下载更便捷。<span lang="EN-US"></span></span></p><p class="MsoNormal" style="text-indent:21.0pt;line-height:150%"><span style="font-size:12.0pt;line-height:150%;font-family:宋体">打开<span lang="EN-US">makecode</span>进行编辑并下载:(推荐)<span lang="EN-US"></span></span></p><p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体"> </span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">在命令行进入到<span lang="EN-US">microbit</span>路径<span lang="EN-US">(</span>该路径下包含<span lang="EN-US">projects</span>文件夹,运行<span lang="EN-US">pxt serve</span>将寻找<span lang="EN-US">projects</span>文件夹,若找不到,则将自动创建新项目,因此必须先进入到含有<span lang="EN-US">projects</span>子文件夹的<span lang="EN-US">microbit</span>文件夹<span lang="EN-US">)</span>,如下所示:<span lang="EN-US"></span></span></p><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-16.png|400px]]</p><p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体"> </span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">在此基础上,在命令行输入:<span lang="EN-US">pxt serve,</span>运行<span lang="EN-US">makecode </span>服务器,自动打开<span lang="EN-US">makecode</span>网页:<span lang="EN-US"></span></span></p><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-17.png|400px]]</p><p class="MsoNormal" style="text-indent:21.0pt;line-height:150%"><span style="font-size:12.0pt;line-height:150%;font-family:宋体">此时,软件包已经加载到左侧,可预览软件包的颜色及图标,并可对图形块进行移动,如下图所示:<span lang="EN-US"></span></span></p><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-18.png|400px]]</p><p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt; line-height:150%;font-family:宋体">但是,在<span lang="EN-US">test.ts</span>文件中,仅能以<span lang="EN-US">JavaScript</span>代码的形式进行显示。<span lang="EN-US"></span></span></p><p class="MsoNormal" style="text-indent:21.0pt;line-height:150%"><span style="font-size:12.0pt;line-height:150%;font-family:宋体">连接<span lang="EN-US">microbit</span>,点击左下角的<span lang="EN-US">download</span>,程序将下载到<span lang="EN-US">microbit</span>中,运行的是<span lang="EN-US">test.ts</span>文件中的代码。<span lang="EN-US"></span></span></p><p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt;line-height:150%;font-family:宋体"> </span></p><p class="MsoNormal" style="text-indent:21.0pt;line-height:150%"><span style="font-size:12.0pt;line-height:150%;font-family:宋体">下载后,将<span lang="EN-US">MCP23017</span>的<span lang="EN-US">PA0-PA7</span>连接至<span lang="EN-US">8</span>个<span lang="EN-US">LED(</span>低电平亮<span lang="EN-US">)</span>,观察得,<span lang="EN-US">PA0-PA7</span>连接的<span lang="EN-US">LED</span>分别为灭亮灭亮灭亮灭亮,软件包测试通过。<span lang="EN-US"></span></span></p><h1><span lang="EN-US" style="font-size: 15pt; line-height: 240%;">6.</span><span style="font-size: 15pt; line-height: 240%;">发布<span lang="EN-US"></span></span></h1><p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体"> </span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">软件包测试通过后,将其发布到<span lang="EN-US">Github</span>,此次发布的本地仓库为<span lang="EN-US">MCP23017</span>文件夹,具体发布流程请参考廖雪峰的<span lang="EN-US">Git</span>教程:<span lang="EN-US"></span></span></p><p class="MsoNormal" style="line-height:150%"><span lang="EN-US"><span style="font-size:12.0pt;line-height: 150%;font-family:宋体">https://www.liaoxuefeng.com/</span></span><span lang="EN-US" style="font-size:12.0pt;line-height:150%;font-family:宋体"></span></p><p class="MsoNormal" style="text-indent:21.0pt;line-height:150%"><span style="font-size:12.0pt;line-height:150%;font-family:宋体">最后,打开<span lang="EN-US">Windows</span>命令行进入到<span lang="EN-US">MCP23017</span>文件夹,输入<span lang="EN-US">pxt bump</span>命令,修改版本号<span lang="EN-US">(</span>修改<span lang="EN-US">pxt.json</span>文件中<span lang="EN-US">version</span>键对应的值<span lang="EN-US">)</span>。<span lang="EN-US"></span></span></p><p class="MsoNormal" style="line-height:150%"><span lang="EN-US" style="font-size: 12.0pt;line-height:150%;font-family:宋体"> </span><span style="font-size:12.0pt;line-height:150%;font-family:宋体">将软件包发布后,即可根据<span lang="EN-US">Github</span>仓库链接找到软件包,可在<span lang="EN-US">makecode</span>中自由扩展该软件包到项目中了。<span lang="EN-US"></span></span></p><p>[[File:Micro:bit系列教程25:自定义Micro:bit软件包-19.png|400px]]</p><h1><span style="font-size: 15pt; line-height: 240%;">参考链接<span lang="EN-US"></span></span></h1><p class="MsoNormal" style="line-height:150%"><b><span style="font-size:12.0pt;line-height:150%;font-family:宋体">搭建环境,及<span lang="EN-US">Github</span>的使用:<span lang="EN-US"></span></span></b></p><p class="MsoNormal" style="line-height:150%"><span lang="EN-US"><span style="font-size:12.0pt;line-height:150%; font-family:宋体">https://makecode.com/cli</span></span><span lang="EN-US" style="font-size:12.0pt;line-height:150%;font-family:宋体"></span></p><p class="MsoNormal" style="line-height:150%"><span lang="EN-US"><span style="font-size: 12.0pt;line-height:150%;font-family:宋体">https://makecode.com/packages/getting-started</span></span><span lang="EN-US" style="font-size:12.0pt;line-height:150%;font-family:宋体"></span></p><p class="MsoNormal" style="line-height:150%"><span lang="EN-US"><span style="font-size:12.0pt;line-height:150%;font-family:宋体">https://makecode.com/packages/getting-started/vscode</span></span><span class="MsoHyperlink"><span lang="EN-US" style="font-size:12.0pt;line-height:150%; font-family:宋体"></span></span></p><p class="MsoNormal" style="line-height:150%"><b><span style="font-size:12.0pt;line-height:150%;font-family:宋体">图形块定义<span lang="EN-US"></span></span></b></p><p class="MsoNormal" style="line-height:150%"><span lang="EN-US"><span style="font-size:12.0pt; line-height:150%;font-family:宋体">https://makecode.com/defining-blocks</span></span><span class="MsoHyperlink"><span lang="EN-US" style="font-size:12.0pt;line-height:150%; font-family:宋体"></span></span></p><p class="MsoNormal" style="line-height:150%"><b><span lang="EN-US" style="font-size:12.0pt;line-height:150%;font-family: 宋体">Github</span></b><b><span style="font-size:12.0pt;line-height:150%;font-family:宋体">教程<span lang="EN-US"></span></span></b></p><p class="MsoNormal" style="line-height:150%"><span style="font-size:12.0pt;line-height:150%;font-family:宋体"> <span lang="EN-US" mso-bidi-theme-font:minor-bidi="" new="" roman="" style="font-size:10.5pt;mso-bidi-font-size:11.0pt;font-family: 等线;mso-ascii-theme-font:minor-latin;mso-fareast-theme-font:minor-fareast; mso-hansi-theme-font:minor-latin;mso-bidi-font-family:" times="" zh-cn=""><span style="font-size:12.0pt;font-family:宋体">https://www.liaoxuefeng.com/</span></span></span></p></div></span></div><span style="font-size: 10.5pt; font-family: 等线;"></span></div>
返回至
Micro:bit系列教程25:自定义Micro:bit软件
。
导航
导航
首页
最近更改
随机页面
MediaWiki帮助
首页
首页
树莓派
主机
配件包
外壳
键鼠
电源
扩展板
显示屏
墨水屏
摄像模块
通信模块
继电器
电机驱动板
游戏机
产品分类
树莓派
Arduino
micro:bit
STM32
Espressif
WiFi模块
蓝牙模块
无线模块
LoRa模块
4G模块
GSM
GPRS
以太网
导航模块
北斗卫星
GPS
LCD
墨水屏
OLED
摄像头
USB模块
串口模块
RS232
RS485
CAN
传感器
温度模块
湿度模块
气压模块
继电器
电机模块
指纹模块
电平转换
音频模块
编程器
Wiki工具
Wiki工具
特殊页面
页面工具
页面工具
用户页面工具
更多
链入页面
相关更改
页面信息
页面日志