想要的组件还没有怎么办?用户可以将自己的算法,设备驱动等模块化代码快速转换为AliOS Things的组件,在应用中被调用,甚至可以分享给其他开发者使用。本文将重点介绍,如何使用aos-cube工具快速构建一个用户组件。
准备工作
1、安装开发环境,参考《Linux环境安装》等。
2、下载AliOS Things代码。
创建组件
使用 aos create component
创建一个组件的脚手架。例如:创建一个名为mycomp的组件,只需要在AliOS Things源代码目录下,使用以下命令:
aos create component -t peripherals mycomp
其中-t 指定的是组件的类型,本例中peripherals表示外设驱动。
更多详情可参考《构建命令详解》。
脚手架已包含了构建系统所必须的aos.mk、Config.in,组件的源代码框架和示例APP。
mycomp
├── aos.mk # 负责提供构建规则
├── Config.in # 负责提供组件配置
├── doc # 组件使用手册、API说明等文档
├── example # 测试组件功能的示例APP
│ ├── aos.mk
│ ├── app.config
│ ├── app_main.c
│ ├── Config.in
│ ├── main.c
│ └── README.md
├── inc # 组件的内部头文件
├── mycomp # 组件的对外头文件
│ └── mycomp.h
├── README.md # 组件的简要说明
└── src # 组件的源代码
└── mycomp.c
编写组件代码
组件开发者可基于脚手架,编写组件代码,及组件的使用示例APP。
例如,在组件的mycomp.c中增加一个函数 int mycomp_run(int a)
,同时将函数声明更新到对外的头文件mycomp.h中。
在组件的示例APP代码中,可以调用mycomp_run()
,完成指定的功能。
///////////////////////////////////////////
// src/mycomp.c
#include <stdio.h>
#include <aos/kernel.h>
int mycomp_init(void)
{
/*add your code*/
return 0;
}
int mycomp_run(int a) // 增加一个函数
{
printf("This is my first comp\n");
return (a * 2);
}
///////////////////////////////////////////
// mycomp/mycomp.h
#ifndef MYCOMP_API_H
#define MYCOMP_API_H
int mycomp_init(void);
int mycomp_run(int a); // 增加函数声明
#endif
///////////////////////////////////////////
// example/app_main.c
int application_start(int argc, char *argv[])
{
mycomp_init();
/*add your code*/
int a = mycomp_run(10); // 调用
printf("a is %d\n", a);
......
}
调试组件
完成组件的代码编写以后,可基于示例APP进行功能验证。
组件发布以后,应用开发者也可以使用该组件的示例APP测试组件,熟悉组件的功能。
使用 aos open
打开组件示例APP,进入该APP的工程目录,即可编译运行。
# 退出AliOS Things源码目录
$ cd ~
# 打开组件示例APP
$ aos open mycomp -b linuxhost
# 进入工程目录
$ cd mycompapp
# 编译
$ aos make
其中-b 指定board,此处以linuxhost为例。
在文档使用中是否遇到以下问题
更多建议
匿名提交