跳转至

通用 - Runtime接口文档

本文档汇总扩展开发中常用的 runtime 接口与扩展/设备钩子函数。每个条目均标注其对应的最低 runtimeVersion

1. API 接口

1.1 设备信息获取

  • getCurrentDeviceId()(0.0.1)
  • 功能:获取当前连接的硬件设备名称(字符串标识)
  • 用法:this.runtime.getCurrentDeviceId()
  • getDevice()(0.0.1)
  • 功能:返回当前设备对象,用于模块扩展开发中访问设备属性和方法
  • 用法:this.runtime.getDevice()

1.2 系统环境判断

  • isWin()(0.0.1)
  • 功能:判断是否为 Windows 系统
  • 用法:this.runtime.isWin()
  • isMac()(0.0.1)
  • 功能:判断是否为 macOS 系统
  • 用法:this.runtime.isMac()
  • isLinux()(0.0.1)
  • 功能:判断是否为 Linux 系统
  • 用法:this.runtime.isLinux()

1.3 串口操作

  • getSerialNumberByPort(port: string)(0.0.1)
  • 功能:根据串口端口号获取串口唯一序列号
  • 用法:this.runtime.getSerialNumberByPort(port)
  • 参数:port(字符串,如 "COM3""/dev/ttyUSB0"
  • 返回值:对应串口的唯一序列号(字符串)
  • pushOutputMessage(text: string)(0.0.1)
  • 功能:向串口终端实时推送输出信息(如日志或调试数据)
  • 用法:this.runtime.pushOutputMessage(text)
  • 参数:text(需推送的文本内容)

1.4 主板引脚接口

  • getBoardPinsByType(type: string, device?: object)(0.0.2)
  • 功能:按引脚能力类型获取当前主板可用引脚列表,用于构建积木下拉框(如数字读写、ADC、PWM、I2C、SPI、UART 等)。
  • 用法:
    • 小模块扩展:this.runtime.getBoardPinsByType("DIGITAL_OUT")
    • 主板扩展(初始化阶段可手动传 device):this.runtime.getBoardPinsByType("DIGITAL_OUT", this.device)
  • 说明:主板在初始化过程中需要调用 getInfo(),此时主板未完成注册,runtime 可能无法获取 device 对象;可通过第二个参数手动传入 device。

2. 钩子

扩展对象(index.js 导出的 class 实例)与设备对象(device.js)存在一组固定命名的函数,系统会在特定时机调用它们。

2.1 扩展对象(index.js)

函数名 描述 所属模式 类型 是否必需 runtimeVersion
setLocale(locale) 切换翻译语言 all 钩子 0.0.1
getCodePrimitives() 返回生成代码的 Block 方法集合 上传/micropython/python模式 方法 0.0.1
getPrimitives() 返回实时模式下 Block 的执行方法集合 实时模式 方法 0.0.1
getInfo() 返回 Block 的元信息(如分类、颜色等) all 方法 必须 0.0.1