|
Hi3861板LED仿空调风速控制,
本帖最后由 pschen083 于 2020-10-9 23:40 编辑
对于刚接触Hi3861 HarmonyOS device小伙子不知如何开始编写应用程序,现在我为编写一些简单的入门实战范例,并附上代码和录屏,让大家快速上手HarmonyOS IOT device 应用开发;
1. 软件开发环境参考华为的官方文档:
https://device.harmonyos.com/cn/docs/start/introduce/oem_start_guide-0000001054913231
2. Hi3861板LED仿空调风速控制程序
/*
* HiSpark_WiFi_IoT_SSL板
* GPIO_8 Key
* GPIO_10 红色LED,高电平点亮
* GPIO_12 黄色LED,高电平点亮
* GPIO_11 绿色LED,高电平点亮
*/
#include <stdio.h>
#include <unistd.h>
#include “ohos_init.h“
#include “cmsis_os2.h“
#include “wifiiot_gpio.h“
#include “wifiiot_gpio_ex.h“
#include “wifiiot_watchdog.h“
//#include “hi_boot_err.h“
#define R_LED WIFI_IOT_GPIO_IDX_10
#define Y_LED WIFI_IOT_GPIO_IDX_12
#define G_LED WIFI_IOT_GPIO_IDX_11
static int g_keyState = 0, g_keyState_b = 0, g_keyDelay = 0;
static int g_ledStatus = 0;
static void *ledTask(const char *arg)
{
int delay = 0;
(void)arg;
printf(“ledTask start!\r\n“);
while (1)
{
if (g_keyState_b != g_keyState)
{
switch (g_keyState)
{
case 0:
{ //空调送风关闭
GpioSetOutputVal(R_LED, WIFI_IOT_GPIO_VALUE0);
GpioSetOutputVal(Y_LED, WIFI_IOT_GPIO_VALUE0);
GpioSetOutputVal(G_LED, WIFI_IOT_GPIO_VALUE0);
}
break;
case 1:
{ //空调手动送风--1档
GpioSetOutputVal(R_LED, WIFI_IOT_GPIO_VALUE0);
GpioSetOutputVal(Y_LED, WIFI_IOT_GPIO_VALUE0);
GpioSetOutputVal(G_LED, WIFI_IOT_GPIO_VALUE1);
}
break;
case 2:
{ //空调手动送风--2档
GpioSetOutputVal(R_LED, WIFI_IOT_GPIO_VALUE0);
GpioSetOutputVal(Y_LED, WIFI_IOT_GPIO_VALUE1);
GpioSetOutputVal(G_LED, WIFI_IOT_GPIO_VALUE1);
}
break;
case 3:
{ //空调手动送风--3档
GpioSetOutputVal(R_LED, WIFI_IOT_GPIO_VALUE1);
GpioSetOutputVal(Y_LED, WIFI_IOT_GPIO_VALUE1);
GpioSetOutputVal(G_LED, WIFI_IOT_GPIO_VALUE1);
}
break;
case 4:
{ //空调自动送风
g_ledStatus = 1;
}
break;
default:
break;
}
g_keyState_b = g_keyState;
}
if (g_keyState == 4)
{ //空调自动档送风-3个LED闪烁
if (++delay > 50)
{
delay=0;
if (g_ledStatus > 0)
{
g_ledStatus = 0;
}
else
{
g_ledStatus = 1;
}
GpioSetOutputVal(R_LED, g_ledStatus);
GpioSetOutputVal(Y_LED, g_ledStatus);
GpioSetOutputVal(G_LED, g_ledStatus);
}
}
usleep(5 * 1000);
if (g_keyDelay > 0)
{
g_keyDelay--;
}
}
return NULL;
}
static void OnKeyPressed(char *arg)
{
(void)arg;
if (g_keyDelay == 0)
{
g_keyDelay = 10;
++g_keyState;
if (g_keyState > 4)
{
g_keyState = 0;
}
}
/* WifiIotGpioValue *val;
if(GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_8, &val)== HI_ERR_SUCCESS){
printf(“val=%d“,*val);
} */
}
static void StartLedTask(void)
{
osThreadAttr_t attr;
GpioInit();
//设置红色,黄色,绿色 LED IO为输出状态
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_IO_FUNC_GPIO_10_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_10, WIFI_IOT_GPIO_DIR_OUT);
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_OUT);
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_OUT);
//设置KEY IO为输入状态,并许能中断
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_FUNC_GPIO_8_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_GPIO_DIR_IN);
IoSetPull(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_PULL_UP);
GpioRegisterIsRFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH, OnKeyPressed, NULL);
WatchDogDisable();
//设置任务参数,并Start一个ledTask任务线程
attr.name = “LedTask“;
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 2048;
attr.priority = osPriorityNormal;
if (osThreadNew((osThreadFunc_t)ledTask, NULL, &attr) == NULL)
{
printf(“[Demo1] Falied to create LedTask!\n“);
}
}
APP_FEATURE_INIT(StartLedTask); //在APP中初始化中回调StartLedTask(void)
3. 附上代码:
Hi3861板LED仿空调风速控制程序_代码.zip
(3.33 KB, 下载次数: 0) 昨天 23:39 上传 点击文件名下载附件
下载积分: 积分 -1 分
4. 学习视频
Hi3861板LED仿空调风速控制程序_视频.zip
(14.21 MB, 下载次数: 0) 昨天 23:36 上传 点击文件名下载附件
下载积分: 积分 -1 分 |
|