HarmonyOS智慧设备开发--LED灯开关控制案例 原创

鸿蒙时代
发布于 2021-8-23 11:26
浏览
1收藏

一、技术相关

项目名称: led_kz
项目语言: C语言
开发板:Hi3861
工具:vscode

二、效果如下
HarmonyOS智慧设备开发--LED灯开关控制案例-鸿蒙开发者社区

三、开发过程

1.建立项目
在applications/sample/wifi-iot/app/下新建一个文件夹用于存放项目文件,在文件中创建一个led_st.c文件用于存放业务代码,和一个BUILD.gn用于存放编译配置。

2.编写代码
控制原理:
在Hi361开发板中,GPIO09控制LED灯,GPIO05与user按钮连接,读取GPIO05状态进而决定GPIO09的状态,从而实现LED灯控制。
while (1){
hi_gpio_value val = HI_GPIO_VALUE0;
hi_gpio_get_input_val(LED_KZ_GPIO, &val);
IoTGpioSetOutputVal(LED_Out_GPIO, val);
}

3.运行代码
修改applications/sample/wifi-iot/app/BUILD.gn文件,使其编译新建的led.c文件
import(“//build/lite/config/component/lite_component.gni”)
lite_component(“app”) {
features = [
“led_kz:led_kz”
]
}

然后进行编译烧录,按下res键运行项目后,按住user,led灯会随之亮起;松手就会熄灭

三、关键代码
led_kz.c
#include <stdio.h>
#include <unistd.h>
#include “ohos_init.h”
#include “cmsis_os2.h”
#include “hi_gpio.h”
#include “hi_io.h”

#define LED_TASK_STACK_SIZE 512
#define LED_TASK_PRIO 25
#define LED_Out_GPIO 9 // for hispark_pegasus
#define LED_KZ_GPIO 5

static void *kzLed(const char *arg)
{
while (1)
{
hi_gpio_value val = HI_GPIO_VALUE0;
hi_gpio_get_input_val(LED_KZ_GPIO, &val);
IoTGpioSetOutputVal(LED_Out_GPIO, val);
}

return NULL;

}

static void LedExampleEntry(void)
{
osThreadAttr_t attr;

hi_gpio_init();
hi_io_set_func(LED_Out_GPIO, HI_IO_FUNC_GPIO_9_GPIO);
hi_gpio_set_dir(LED_Out_GPIO, HI_GPIO_DIR_OUT);

hi_io_set_func(LED_KZ_GPIO, HI_IO_FUNC_GPIO_5_GPIO);
hi_gpio_set_dir(LED_KZ_GPIO, HI_GPIO_DIR_IN);

hi_io_set_pull(LED_KZ_GPIO, HI_IO_PULL_UP);

attr.name = "kzLed";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = LED_TASK_STACK_SIZE;
attr.priority = LED_TASK_PRIO;

if (osThreadNew((osThreadFunc_t)kzLed, NULL, &attr) == NULL) {
    printf("[LedExample] Falied to create LedTask!\n");
}

}

SYS_RUN(LedExampleEntry);

BUILD.gn
static_library(“led_kz”) {
sources = [
“led_kz.c”
]
include_dirs = [
“//utils/native/lite/include”,
“//kernel/liteos_m/components/cmsis/2.0”,
“//base/iot_hardware/peripheral/interfaces/kits”,
]
}

完整代码地址:https://gitee.com/jltfcloudcn/smart-devices/tree/master/led_st_kz

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
标签
HarmonyOS智慧设备开发--LED灯开关控制案例.docx 61.9K 39次下载
收藏 1
回复
举报
2条回复
按时间正序
/
按时间倒序
sunqihan
sunqihan

这是什么版本的源码?


回复
2023-7-30 16:50:15
鸿蒙时代
鸿蒙时代

很早的版本了。估计是2.X

回复
2023-8-25 11:37:01
回复
    相关推荐