#2020征文-开发板#Hi3861 炫彩灯板(DCL)开发

发布于 2021-1-7 17:35
浏览
0收藏

一、前言
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\n",(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!\n");
    }
}

SYS_RUN(LedExampleEntry);

六、编译

#2020征文-开发板#Hi3861 炫彩灯板(DCL)开发-开源基础软件社区

七、烧录

#2020征文-开发板#Hi3861 炫彩灯板(DCL)开发-开源基础软件社区

八、运行

#2020征文-开发板#Hi3861 炫彩灯板(DCL)开发-开源基础软件社区九、总结
在编程过程中需要参考API文档,了解每个函数的定义,还需要参考硬件电路图,了解每个管脚的定义。感谢社区的大佬们提供帮助

 

已于2021-1-7 17:35:26修改
收藏
回复
举报
回复
添加资源
添加资源将有机会获得更多曝光,你也可以直接关联已上传资源 去关联
    相关推荐