Arduino用户库快速模板流程:制作一个LED灯模块扩展

1、获取模板库

2、修改config.json

修改后如下:

{
  "name": {
    "zh-cn": "LED灯模块",
    "en": "LED Module"
  },
  "description": {
    "zh-cn": "简单的控制数字引脚控制的LED灯模块",
    "en": "Simple control LED lamp module controlled by digital pin"
  },
  "author": "xxx",
  "email": "xxx@qq.com",
  "license": "MIT",
  "isBoard": false,
  "id": "led",
  "platform": ["win","mac","web"],
  "version": "0.0.1",
  "asset": {
    "arduinoC": {
      "dir": "arduinoC/",
      "version": "0.0.1",
      "board": [
        "microbit",
        "esp32",
        "arduino"
      ],
      "main": "main.ts"
    }
  }
}

3、资源文件

_images:

完成后如下:

libraries

_locales

此文件夹放置翻译文件,如果仅中文可以不用操作此文件夹,本文无需修改。

_menus

此文件夹放置下拉菜单文件,例如引脚编号的下拉,默认放置了所有主控器的常见引脚,可以直接在main.ts中调用,本文无需修改。

4、效果预览

5、编写main.ts

打草稿:

首先我们设计一下积木块,控制LED灯需要一个执行模块,需要设置引脚开关状态以及亮度,据此列出需要的积木块和对应代码如下:

实现:


enum KAIGUAN {
    //% block="开"
    HIGH,
    //% block="关"
    LOW
}

//% color="#AA278D" iconWidth=50 iconHeight=40
namespace led {
    //% block="控制[YINJIAO]引脚的LED灯[KAIGUAN]" blockType="command"
    //% YINJIAO.shadow="dropdown" YINJIAO.options="PIN_DigitalWrite" 
    //% KAIGUAN.shadow="dropdown" KAIGUAN.options="KAIGUAN" KAIGUAN.defl="HIGH"
    export function kaiguan(parameter: any, block: any) {
        let YINJIAO = parameter.YINJIAO.code;
        let KAIGUAN = parameter.KAIGUAN.code;
        Generator.addCode(`digitalWrite(${YINJIAO},${KAIGUAN});`);
    }

    //% block="控制[YINJIAO]引脚的LED灯亮度为[LIANGDU]" blockType="command"
    //% YINJIAO.shadow="dropdown" YINJIAO.options="PIN_AnalogWrite" 
    //% LIANGDU.shadow="range"   LIANGDU.params.min=0    LIANGDU.params.max=255    LIANGDU.defl=255
    export function liangdu(parameter: any, block: any) {
        let YINJIAO = parameter.YINJIAO.code;
        let LIANGDU = parameter.LIANGDU.code;

        Generator.addCode(`analogWrite(${YINJIAO},${LIANGDU});`);
    }

}

加载测试,编程及上传,测试硬件功能

6、导出分享