本文参考了官网教程:https://mindplus.dfrobot.com.cn/ext-api
1、首先,我们从官网下载模板库,https://mindplus.dfrobot.com.cn/extensions-user
下载后解压,我们可以得到下列文件
删除掉.mpext后缀文件,将文件夹名字从ext-oled12864改为ext-lcd1602。
2、将images里的featured.png和icon.svg替换成自己需要的,featured.png需要600X372像素,可以右键打开方式选择画图——重新调整大小——像素——把保持纵横比的勾去掉——在水平和垂直输入600和372,然后保存即可。
3、修改config.json
删除其中files字段部分代码
修改name和description对应字段
修改author(作者)和email(邮箱)为自己的
修改id为这个扩展库唯一标识,英文数字或下划线,不要用中文和特殊符号,最好与文件夹后的名字相同
version为版本号,可以改成0.0.1,以后每次修改代码数字逐渐升级
board填写支持的板子,无法测试的不要写。
如下所示:
{
"name": {
"zh-cn": "LCD1602",
"en": "LCD1602"
},
"description": {
"zh-cn": "LCD1602",
"en": "LCD1602"
},
"author": "filan",
"email": "xx@aa.com",
"license": "MIT",
"isBoard": false,
"id": "oled12864",
"version": "0.0.1",
"platform": [
"win"
],
"asset": {
"arduinoC": {
"dir": "arduinoC/",
"version": "0.0.1",
"board": [
"arduino"
],
"main": "main.ts"
}
}
}
4、修改main.ts
//% color="#FF0033" iconWidth=50 iconHeight=40
namespace LCD1602{
//% block="LCD1602 init RS [RS] EN [EN] D4 [D4] D5 [D5] D6 [D6] D7 [D7]" blockType="command"
//% RS.shadow="dropdownRound" RS.options="RS"
//% EN.shadow="dropdownRound" EN.options="EN"
//% D4.shadow="dropdownRound" D4.options="D4"
//% D5.shadow="dropdownRound" D5.options="D5"
//% D6.shadow="dropdownRound" D6.options="D6"
//% D7.shadow="dropdownRound" D7.options="D7"
export function LCD1602Init(parameter: any, block: any) {
let rs=parameter.RS.code;
let en=parameter.EN.code;
let d4=parameter.D4.code;
let d5=parameter.D5.code;
let d6=parameter.D6.code;
let d7=parameter.D7.code;
Generator.addInclude("LCD1602Init","#include <LiquidCrystal.h>");
Generator.addObject(`LCD1602Init`,"LiquidCrystal",`lcd(${rs}, ${en}, ${d4}, ${d5},${d6}, ${d7});`);
Generator.addSetup(`LCD1602Init`,"lcd.begin(16, 2);");
}
//% block="LCD1602 Print Line [Y] Mess [SR] " blockType="command"
//% Y.shadow="dropdown" Y.options="Y"
//% SR.shadow="string" SR.defl="DFRobot"
export function LCD1602Initprintline(parameter: any, block: any) {
let y=parameter.Y.code;
let sr=parameter.SR.code;
Generator.addCode(`lcd.setCursor(0,${y});`);
Generator.addCode(`lcd.print(${sr});`);
}
}
5、locals文件夹:根据文件名对应不同的语言,若没有对应语言的翻译文件则以main.ts中block描述代码中的内容直接显示。
本库只有zh.json,内容如下:
{
"LCD1602.LCD1602Init|block": "初始化LCD1602 RS:[RS] EN:[EN] D4:[D4] D5:[D5] D6:[D6] D7:[D7]" ,
"LCD1602.LCD1602Initprintline|block": "LCD1602屏幕第[Y]行显示 [SR]"
}
6、menus:此文件夹中放置各种不同板子对应的下拉菜单内容。
本库Arduino.json如下
{
"RS": {
"menu": [
["0", "0"],
["1", "1"],
["2", "2"],
["3", "3"],
["4", "4"],
["5", "5"],
["6", "6"],
["7", "7"],
["8", "8"],
["9", "9"],
["10", "10"],
["11", "11"],
["12", "12"],
["13", "13"],
["A0", "A0"],
["A1", "A1"],
["A2", "A2"],
["A3", "A3"],
["A4", "A4"],
["A5", "A5"]
],
"default_LCD1602Init": "3"
},
"EN": {
"menu": [
["0", "0"],
["1", "1"],
["2", "2"],
["3", "3"],
["4", "4"],
["5", "5"],
["6", "6"],
["7", "7"],
["8", "8"],
["9", "9"],
["10", "10"],
["11", "11"],
["12", "12"],
["13", "13"],
["A0", "A0"],
["A1", "A1"],
["A2", "A2"],
["A3", "A3"],
["A4", "A4"],
["A5", "A5"]
],
"default_LCD1602Init": "5"
},
"D4": {
"menu": [
["0", "0"],
["1", "1"],
["2", "2"],
["3", "3"],
["4", "4"],
["5", "5"],
["6", "6"],
["7", "7"],
["8", "8"],
["9", "9"],
["10", "10"],
["11", "11"],
["12", "12"],
["13", "13"],
["A0", "A0"],
["A1", "A1"],
["A2", "A2"],
["A3", "A3"],
["A4", "A4"],
["A5", "A5"]
],
"default_LCD1602Init": "10"
},
"D5": {
"menu": [
["0", "0"],
["1", "1"],
["2", "2"],
["3", "3"],
["4", "4"],
["5", "5"],
["6", "6"],
["7", "7"],
["8", "8"],
["9", "9"],
["10", "10"],
["11", "11"],
["12", "12"],
["13", "13"],
["A0", "A0"],
["A1", "A1"],
["A2", "A2"],
["A3", "A3"],
["A4", "A4"],
["A5", "A5"]
],
"default_LCD1602Init": "11"
},
"D6": {
"menu": [
["0", "0"],
["1", "1"],
["2", "2"],
["3", "3"],
["4", "4"],
["5", "5"],
["6", "6"],
["7", "7"],
["8", "8"],
["9", "9"],
["10", "10"],
["11", "11"],
["12", "12"],
["13", "13"],
["A0", "A0"],
["A1", "A1"],
["A2", "A2"],
["A3", "A3"],
["A4", "A4"],
["A5", "A5"]
],
"default_LCD1602Init": "12"
},
"D7": {
"menu": [
["0", "0"],
["1", "1"],
["2", "2"],
["3", "3"],
["4", "4"],
["5", "5"],
["6", "6"],
["7", "7"],
["8", "8"],
["9", "9"],
["10", "10"],
["11", "11"],
["12", "12"],
["13", "13"],
["A0", "A0"],
["A1", "A1"],
["A2", "A2"],
["A3", "A3"],
["A4", "A4"],
["A5", "A5"]
],
"default_LCD1602Init": "13"
},
"Y": {
"menu": [
["1","0"],
["2","1"]
],
"default_LCD1602": "2"
},
"LR": {
"menu": [
["Left","Left"],
["Right","Right"]
],
"default_LCD1602InitLR": "Right"
}
}
7、libraries:放所需要的库,没有则删除。
8、在Mind+里导入用户库,选库文件夹里的json库,即可出现如下图:
可以选择的方块如下所示:
9、如果要导出分享,右键步骤8图1加载出来的库,点击导出,即可在库文件夹生成.mpext的后缀文件
重新导入会发现库图片左上角已经没有图标,
调试完成后,编写README.md文件(以markdown格式编辑本扩展库的教程)。
如果上传git,文件名不要修改否则会出错。
分享(推荐分享到 Mind+社区):
本地加载:直接分享给其他用户加载.mpext(注意不要解压从本地上传,否则是调试模式,从本地应该直接选择.mpext加载打开)。
网络加载:将.mpext文件导出到与config.json同级目录下,通过git将整个文件夹上传到github或者gitee(码云) (gith教程),将git链接分享给其他用户加载。
** 注:通过git分享时,1.6.2RC2.0需要将导出的.mpext文件(可以zip解压提取)中的config.json和libraries.zip放到开发文件夹对应位置,然后上传git,否则会导致加载出错。(后续版本将直接加载mpext文件无需此操作,注意上传git时mpext文件名不要修改) **