封装 ESP-IDF 组件并上传到 ESP 组件注册表

封装 ESP-IDF 组件并上传到 ESP 组件注册表

介绍

在 ESP-IDF 中,组件(Component)是用于组织代码的基本单元,类似于库。每个组件都是一个独立的模块,包含特定的功能,可以与其他组件一起构建项目。通过这种模块化设计,你的项目可以由多个组件组成,易于管理和复用。此外,ESP-IDF 提供了组件注册表,开发者可以在其中分享组件,也可以轻松使用其他人发布的组件。

创建组件

为了封装一个 ESP-IDF 组件,首先需要创建一个标准的目录结构。在你的项目根目录下,新建一个 components/ 文件夹,在里面创建你的组件文件夹。以 my_component为例,目录结构如下:

project_directory/
    ├── components/
    │   └── my_component/
    │       ├── CMakeLists.txt
    │       ├── include/
    │       │   └── my_component.h
    │       ├── my_component.c
    ├── main/
    └── CMakeLists.txt

或者也可以通过终端运行以下命令来创建

idf.py create-component my_component

如果你是使用VScode进行编程还可以在上方搜索框调用命令
在这里插入图片描述

以上方法都可以获得一个基本的组件框架,但如果要将组件发布到 ESP 组件注册表,还需要一些额为的配置文件

上传组件到 ESP 组件注册表

初始化Git 仓库

ESP-IDF 组件通常通过 Git 仓库进行管理。我们需要将组件代码推送到一个 Git 仓库

编写 idf_component.yml 文件

为了能够发布组件,我们需要创建 idf_component.yml 文件,该文件定义了组件的元数据。这个文件需要放在组件的根目录下。

version: 1.0.0
license: "MIT"
description: my_component
url: https://github.com/my_Namespace/my_component
files:
  exclude:
  - .git
dependencies:
  idf:
    version: ">=5.3.1"

my_Namespace为你github的名称

license: “MIT” 组件的许可协议,不需要可以删除,如果使用许可协议,需要添加LICENSE文件

发布到组件注册表

接下来,我们将这个组件上传到 ESP 组件注册表,以便分享给其他开发者使用。要将组件发布到 ESP 组件注册表,您需要提供身份验证令牌。方法是通过环境变量进行设置。

获取 API 令牌

首先,您需要从 Espressif 获取 API 令牌。您可以通过以下步骤获取:

  1. 登录到 ESP 组件注册表
  2. 导航到您的个人资料页面。
  3. 生成一个新的 API 令牌。
设置环境变量

将 API 令牌设置为环境变量 IDF_COMPONENT_API_TOKEN
在这里插入图片描述

发布组件

使用 命令行工具将组件发布到 ESP-IDF 组件注册表。

compote component upload --namespace [YOUR_NAMESPACE] --name test_cmp

YOUR_NAMESPACE为你的github名称

添加组件注册表中的组件

上传后,组件就可以在注册表里找到了,想要在项目中添加这个组件也很很简单

搜索框选择ESP显示组件注册表命令
在这里插入图片描述

搜索需要的组件,点击添加即可
在这里插入图片描述
在这里插入图片描述
当添加组件后,编译时会自动将组件下载到项目的 managed_components 目录中。需要注意的是,该目录中的内容是不可修改的。每个组件的目录下都会自动生成一个 .component_hash 文件,该文件记录了组件的哈希值。在项目构建时,系统会将此哈希值与项目根目录下的 dependencies.lock 文件中的哈希值进行比对。如果两者不一致,系统会自动重新下载该组件。

总结

参考资料
IDF组件管理

esp32-freertos-sdk 工具包 See the Getting Started guide links above for a detailed setup guide. This is a quick reference for common commands when working with ESP-IDF projects: Setup Build Environment (See Getting Started guide for a full list of required steps with details.) Install host build dependencies mentioned in Getting Started guide. Add tools/ directory to the PATH Run python -m pip install -r requirements.txt to install Python dependencies Configuring the Project idf.py menuconfig Opens a text-based configuration menu for the project. Use up & down arrow keys to navigate the menu. Use Enter key to go into a submenu, Escape key to go out or to exit. Type ? to see a help screen. Enter key exits the help screen. Use Space key, or Y and N keys to enable (Yes) and disable (No) configuration items with checkboxes "[*]" Pressing ? while highlighting a configuration item displays help about that item. Type / to search the configuration items. Once done configuring, press Escape multiple times to exit and say "Yes" to save the new configuration when prompted. Compiling the Project idf.py build ... will compile app, bootloader and generate a partition table based on the config. Flashing the Project When the build finishes, it will print a command line to use esptool.py to flash the chip. However you can also do this automatically by running: idf.py -p PORT flash Replace PORT with the name of your serial port (like COM3 on Windows, /dev/ttyUSB0 on Linux, or /dev/cu.usbserial-X on MacOS. If the -p option is left out, idf.py flash will try to flash the first available serial port. This will flash the entire project (app, bootloader and partition table) to a new chip. The settings for serial port flashing can be configured with idf.py menuconfig. You don't need to run idf.py build before running idf.py flash, idf.py flash will automatically rebuild anything which needs it. Viewing Serial Output The idf.py monitor target uses the idf_monitor tool to display se
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宁子希

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值