`
一、前言
HarmonyOS HiSpark Wi-Fi IoT 套件开发之炫彩灯板
二、场景描述
场景设定:楼道
三、功能描述
感应照明灯,在光线比较暗的地方,感应到人体就会打开照明灯
四、功能实现
首先,通过人体红外感应检测人体运动
然后,通过光敏电阻检测光线亮度
最后,若满足有人且天黑的条件则亮灯
五、代码实现
- /*
- * 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.
- */
- /*
- * HiSpark_WiFi_IoT_DCL_VER.A板
- * GPIO_07 人体红外感应
- * GPIO_09 光敏电阻
- * GPIO_10 红色LED,高电平点亮
- * GPIO_11 绿色LED,高电平点亮
- * GPIO_12 蓝色LED,高电平点亮
- */
- #include <stdio.h>
- #include <unistd.h>
- #include "wifiiot_adc.h"
- #include "ohos_init.h"
- #include "cmsis_os2.h"
- #include "wifiiot_gpio.h"
- #include "wifiiot_gpio_ex.h"
- #define LED_INTERVAL_TIME_US 300000
- #define LED_TASK_STACK_SIZE 512
- #define LED_TASK_PRIO 25
- enum LedState {
- LED_ON = 0,
- LED_OFF,
- LED_SPARK,
- };
- enum LedState g_ledState = LED_OFF;
- static void *LedTask(const char *arg)
- {
- (void)arg;
- while (1) {
- WifiIotGpioValue rel=0;
- GpioGetInputVal(WIFI_IOT_IO_NAME_GPIO_7, &rel);
- unsigned short data = 0;
- AdcRead(WIFI_IOT_ADC_CHANNEL_4, &data, WIFI_IOT_ADC_EQU_MODEL_4, WIFI_IOT_ADC_CUR_BAIS_DEFAULT, 0);
- if(rel != 0 && data > 1500){//当感应到人体活动且天色较暗时,三色LED会闪烁
- g_ledState = LED_SPARK;
- // printf("GPIO_7=%d, GPIO_9=%d
- ",(int)rel,data);
- }
- else{
- g_ledState = LED_OFF;
- }
- switch (g_ledState) {
- case LED_ON:
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 1);
- usleep(LED_INTERVAL_TIME_US);
- break;
- case LED_OFF:
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 0);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 0);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 0);
- usleep(LED_INTERVAL_TIME_US);
- break;
- case LED_SPARK:
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 0);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 1);
- usleep(LED_INTERVAL_TIME_US);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_11, 0);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 1);
- usleep(LED_INTERVAL_TIME_US);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_12, 0);
- GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_10, 1);
- usleep(LED_INTERVAL_TIME_US);
- break;
- default:
- usleep(LED_INTERVAL_TIME_US);
- break;
- }
- }
- return NULL;
- }
- static void LedExampleEntry(void)
- {
- osThreadAttr_t attr;
- GpioInit();
- //设置IO为输入状态
- IoSetFunc(WIFI_IOT_IO_NAME_GPIO_7, WIFI_IOT_IO_FUNC_GPIO_7_GPIO);
- GpioSetDir(WIFI_IOT_IO_NAME_GPIO_7, WIFI_IOT_GPIO_DIR_IN);
- IoSetPull(WIFI_IOT_IO_NAME_GPIO_7, WIFI_IOT_IO_PULL_UP);
- IoSetFunc(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_IO_FUNC_GPIO_9_GPIO);
- GpioSetDir(WIFI_IOT_IO_NAME_GPIO_9, WIFI_IOT_GPIO_DIR_IN);
- //设置红色,绿色,蓝色 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);
- attr.name = "LedTask";
- 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)LedTask, NULL, &attr) == NULL) {
- printf("[LedExample] Falied to create LedTask!
- ");
- }
- }
- SYS_RUN(LedExampleEntry);
复制代码六、编译
七、烧录
八、运行
九、总结
在编程过程中需要参考API文档,了解每个函数的定义,还需要参考硬件电路图,了解每个管脚的定义。感谢社区的大佬们提供帮助
`