目录结构
先在./applications/sample/wifi-iot/app路径下新建一个目录(或一套目录结构),用于存放业务源码文件。
本例程:在app下新增业务led,其中hello_world.c为业务代码,BUILD.gn为编译脚本,具体规划目录结构如下:
.
└── applications
└── sample
└── wifi-iot
└── app
│── led
│ │── led.c
│ └── BUILD.gn
└── BUILD.gn
复制代码
业务代码。
新建./applications/sample/wifi-iot/app/led下的led.c文件,在led.c中新建业务入口函数led,并实现业务逻辑。并在代码最下方,使用HarmonyOS启动恢复模块接口SYS_RUN()启动业务。(SYS_RUN定义在ohos_init.h文件中)
-
BUILD.gn为编译脚本
-
led.c为业务逻辑代码所在文件
led.c
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#define LED_ON_TIME_US 2000000
#define LED_OFF_TIME_US 1000000
static void Led(void)
{
//GPIO引脚初始化
GpioInit();
//设置管脚功能为GPIO
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_GPIO);
//设置GPIO引脚方向为输出
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_DIR_OUT);
while (1) {
//设置GPIO09输出为低电平
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, 0);
//打印输出日志
printf("[DEMO] LED on.\n");
//延时一定时间,即亮灯的持续时间
usleep(LED_ON_TIME_US);
//设置GPIO09输出为高电平
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, 1);
//打印输出日志
printf("[DEMO] LED off.\n");
//延时一定时间,即灭灯的持续时间
usleep(LED_OFF_TIME_US);
}
}
SYS_RUN(Led);
复制代码
BUILD.gn(app/BUILD.gn)
static_library("bahuyang") {
sources = [
"led.c"
]
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/components/cmsis/2.0",
"//base/iot_hardware/interfaces/kits/wifiiot_lite",
]
}
复制代码
-
“bahuyang”:是生成静态库名称,可随意更改
-
“led.c”:代码文件
BUILD.gn()
# Copyright (c) 2020 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/lite/config/component/lite_component.gni")
import("//build/lite/config/component/lite_component.gni")
lite_component("app") {
features = [
#"startup",
"led:bahuyang"
]
}
复制代码
-
将”startup”注释,运行我们自己的文件
-
“led”:工程目录
-
bahuyang:静态库文件
编译烧录
编译
我在以前的文章里,详细讲解了怎样编译。大家可以回头看看。
我们进入终端面板下,在对应工程目录下,输入python build.py wifiiot
进行编译,当出现编译成功时,就代表编译完成。
烧录
利用HiBurn工具进行烧录,我在以前的文章里,详细讲过。
选定文件后,点击connect,按下开发板复位按钮,进行烧录。
烧录完成后,再次按下复位按键。
观察现象
选择波特率115200,连接相应串口,观看串口调试助手的结果。
LED灯实现闪烁。
后续
如果想了解更多物联网、智能家居项目知识,欢迎关注公众号,关注我的项目实战专栏。
编写不易,感谢支持。
本文使用 文章同步助手 同步
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END