HarmonyOS小熊派 | HarmonyOS传感器驱动开发--E53_IS1人体红外感应

举报
Yuchuan 发表于 2021/07/26 20:08:30 2021/07/26
【摘要】 传感器驱动开发--E53_IS1人体红外感应

BearPi-HM_Nano开发板传感器驱动开发——E53_IS1人体红外感应

本示例将演示如何在BearPi-HM_Nano开发板上使用E53_IS1实现人体红外感应,当检测到有人走动时,蜂鸣器发出报警。

BearPi-HM_Nano

E53_IS1 API分析

本案例主要使用了以下API完成人体红外感应

1、InitE53IS1()

/***************************************************************
* 函数名称: InitE53IS1
* 说    明: 初始化E53_IS1
* 参    数: 无
* 返 回 值: 无
***************************************************************/
void InitE53IS1(void)

描述:

初始化E53_IS1

2、ReadDataE53IS1()

/***************************************************************
* 函数名称: ReadDataE53IS1
* 说    明: 读取数据
* 参    数: 无
* 返 回 值: 无
***************************************************************/
void ReadDataE53IS1(CallBackFuncE53IS1 func)

描述: 设置人体感应触发的回调函数

3、StatusSetBeep()

/***************************************************************
* 函数名称: StatusSetBeep
* 说    明: 蜂鸣器报警与否
* 参    数: status,ENUM枚举的数据
*									OFF,蜂鸣器
*									ON,开蜂鸣器
* 返 回 值: 无
***************************************************************/
void StatusSetBeep(E53_IS1_Status_ENUM status)

描述: 根据状态设置PWM波

硬件设计

本案例将用到 E53_IS1 红外感应扩展板与 BearPi-HM_Nano 开发板,其中E53_IS1扩展板原理图如下,当检测到人时,传感器会输出高电平,通过对GPIO_7的监测就能判断是否有人走动。

E53_IS1 红外感应扩展板与 BearPi-HM_Nano 开发板安装

软件设计

主要代码分析

首先调用 InitE53IS1()  函数初始化E53_SC1所接的引脚的功能,然后调用 ReadDataE53IS1() 函数设置触发的回调函数Beep_Alarm(),系统启动后会通过osEventFlagsWait()函数让 YuchuanE53IS1Task 任务一直等待事件标志位FLAGS_MSK1,当检测到人后,Beep_Alarm()函数会发送事件标志位 YuchuanE53IS1Task 任务继续运行开启蜂鸣器报警10秒钟 然后关闭蜂鸣器继续等待下一次触发事件。

yuchuan_e53_is1.c

#include <stdio.h>

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "e53_is1.h"

#define TASK_CB_MEM_SIZE 0U
#define TASK_STACK_MEM_SIZE 1024 * 4
#define TASK_PRIORITY 25
#define FLAGS_MSK1 0x00000001U

osEventFlagsId_t evt_id;

static void Beep_Alarm(char *arg)
{
    (void)arg;
    printf("获取到接口数据,触发事件!!!\n");
    // uint32_t osEventFlagsSet (osEventFlagsId_t ef_id, uint32_t flags);
    osEventFlagsSet(evt_id, FLAGS_MSK1);
}

/* E53接口任务 */
static void YuchuanE53IS1Task(void)
{
    // 初始化E53_IS1接口
    InitE53IS1();
    // 读取E53接口的数据
    ReadDataE53IS1(Beep_Alarm);
    while (1)
    {
        printf("阻塞等待,触发事件!!!\n");
        // uint32_t osEventFlagsWait (osEventFlagsId_t ef_id, uint32_t flags, uint32_t options, uint32_t timeout);
        osEventFlagsWait(evt_id, FLAGS_MSK1, osFlagsWaitAny, osWaitForever);
        printf("触发事件,根据状态设置PWM波输出!!!\n");
        StatusSetBeep(ON);
        osDelay(1000);
        StatusSetBeep(OFF);
    }
}

static void YuchuanE53IS1Entry(void)
{
    evt_id = osEventFlagsNew(NULL);
    if (evt_id == NULL)
    {
        printf("Falied To Create EventFlag !!!\n");
    }

    osThreadAttr_t threadAttr;
    threadAttr.attr_bits = 0U;
    threadAttr.cb_mem = NULL;
    threadAttr.cb_size = TASK_CB_MEM_SIZE;
    threadAttr.stack_mem = NULL;
    threadAttr.stack_size = TASK_STACK_MEM_SIZE;
    threadAttr.priority = TASK_PRIORITY;

    threadAttr.name = "YuchuanE53IS1Task";
    if (osThreadNew((osThreadFunc_t)YuchuanE53IS1Task, NULL, &threadAttr) == NULL)
    {
        printf("Falied To Create YuchuanE53IS1Task !!!\n");
    }
}

APP_FEATURE_INIT(YuchuanE53IS1Entry);

include/e53_is1.h

/*
 * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology 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.
 */

#ifndef __E53_IS2_H__
#define __E53_IS2_H__

typedef void (*CallBackFuncE53IS1) (char *arg);

typedef enum
{
    OFF = 0,
    ON
}E53_IS1_Status_ENUM;

// 初始化E53_IS1接口
void InitE53IS1(void);
// 读取E53接口的数据
void ReadDataE53IS1(CallBackFuncE53IS1 func);
// 根据状态设置灯的亮灭
void StatusSetBeep(E53_IS1_Status_ENUM status);

#endif /* __E53_IS2_H__ */

src/e53_is1.c

/*
 * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology 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.
 */

#include <stdio.h>

#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_pwm.h"
#include "e53_is1.h"

#define IOT_IO_LED_GPIO_7 7
#define IOT_IO_PWM1_GPIO_8 8
#define IOT_PWM_PORT_PWM1 1

/***************************************************************
* 函数名称: IoInitE53IS1
* 说    明: E53_SC2_GPIO初始化
* 参    数: 无
* 返 回 值: 无
***************************************************************/
static void IoInitE53IS1(void)
{
    // 初始化GPIO
    IoTGpioInit(IOT_IO_PWM1_GPIO_8);
    // 设置GPIO_8引脚为输出模式
    IoTGpioSetDir(IOT_IO_PWM1_GPIO_8, IOT_GPIO_DIR_OUT);
    // 设置GPIO_8引脚复用功能为PWM
    IoTGpioSetFunc(IOT_IO_PWM1_GPIO_8, IOT_GPIO_FUNC_GPIO_8_PWM1_OUT);
    // 初始化PWM1端口
    IoTPwmInit(IOT_PWM_PORT_PWM1);

    // 初始化GPIO
    IoTGpioInit(IOT_IO_LED_GPIO_7);
    // 设置GPIO的引脚为输入模式
    IoTGpioSetDir(IOT_IO_LED_GPIO_7, IOT_GPIO_DIR_IN);
    // 设置GPIO引脚功能为普通GPIO
    IoTGpioSetFunc(IOT_IO_LED_GPIO_7, IOT_GPIO_FUNC_GPIO_7_GPIO);
    // 设置GPIO引脚为上拉
    IoTGpioSetPull(IOT_IO_LED_GPIO_7, IOT_GPIO_PULL_UP);
}

/***************************************************************
* 函数名称: InitE53IS1
* 说    明: 初始化E53_IS1
* 参    数: 无
* 返 回 值: 无
***************************************************************/
void InitE53IS1(void)
{
    IoInitE53IS1();
}

/***************************************************************
* 函数名称: ReadDataE53IS1
* 说    明: 读取数据
* 参    数: 无
* 返 回 值: 无
***************************************************************/
void ReadDataE53IS1(CallBackFuncE53IS1 func)
{
    // unsigned int IoTGpioRegisterIsrFunc(unsigned int id, IotGpioIntType intType, IotGpioIntPolarity intPolarity, GpioIsrCallbackFunc func, char *arg);
    IoTGpioRegisterIsrFunc(IOT_IO_LED_GPIO_7, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_RISE_LEVEL_HIGH, func, NULL);
}

/***************************************************************
* 函数名称: StatusSetBeep
* 说    明: 蜂鸣器报警与否
* 参    数: status,ENUM枚举的数据
*									OFF,蜂鸣器
*									ON,开蜂鸣器
* 返 回 值: 无
***************************************************************/
void StatusSetBeep(E53_IS1_Status_ENUM status)
{
    if (status == ON)
    {
        // 输出PWM波
        // unsigned int IoTPwmStart(unsigned int port, unsigned short duty, unsigned int freq);
        IoTPwmStart(IOT_PWM_PORT_PWM1, 50, 4000);
    }
    if (status == OFF)
    {
        // unsigned int IoTPwmStop(unsigned int port);
        IoTPwmStop(IOT_PWM_PORT_PWM1);
    }
}

编译调试

//applications/sample/BearPi/BearPi-HM_Nano/sample/C5_YuchuanE53IS1/BUILD.gn

static_library("YuchuanE53IS1"){
    sources = [
        "yuchuan_e53_is1.c",
        "src/e53_is1.c",
    ]

    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/kal/cmsis",
        "//base/iot_hardware/peripheral/interfaces/kits",
        "include",
    ]
}

修改 BUILD.gn 文件

修改applications\BearPi\BearPi-HM_Nano\sample路径下 BUILD.gn 文件,指定 YuchuanE53IS1 参与编译。

# Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology 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")

lite_component("sample") {
    features = [
        #"A1_kernal_thread:thread_example",
        #"A2_kernel_timer:timer_example",
        #"A3_kernel_event:event_example",
        #"A4_kernel_mutex:mutex_example",
        #"A5_kernel_semaphore:semaphore_example",
        #"A6_kernel_message:message_example",

        #"B1_basic_led_blink:led_example",
        #"B2_basic_button:button_example",
        #"B3_basic_pwm_led:pwm_example",
        #"B4_basic_adc:adc_example", 
        #"B5_basic_i2c_nfc:i2c_example",
        #"B6_basic_uart:uart_example",
        
        #"C1_e53_sf1_mq2:e53_sf1_example",
        #"C2_e53_ia1_temp_humi_pls:e53_ia1_example",
        #"C3_e53_sc1_pls:e53_sc1_example",
        #"C4_e53_sc2_axis:e53_sc2_example",
        #"C5_e53_is1_infrared:e53_is1_example",

        #"D1_iot_wifi_ap:wifi_ap",
        #"D2_iot_wifi_sta_connect:wifi_sta_connect",        
        #"D3_iot_udp_client:udp_client",
        #"D4_iot_tcp_server:tcp_server",
        #"D5_iot_mqtt:iot_mqtt",        
        #"D6_iot_cloud_oc:oc_mqtt",
        #"D7_iot_cloud_onenet:onenet_mqtt",
        #"D8_iot_cloud_oc_smoke:cloud_oc_smoke",
        #"D9_iot_cloud_oc_light:cloud_oc_light",
        #"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover",
        #"D11_iot_cloud_oc_infrared:cloud_oc_infrared",
        #"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture",
        #"D13_iot_cloud_oc_gps:cloud_oc_gps",
        #"B1_YuchuanBasicLEDBlink:yuchuanLED",
        #"B2_YuchuanBasicButton:yuchuanButton",
        #"C1_YuchuanE53_SF1_MQ2:YuchuanE53SF1Mq2",
        #"C2_YuchuanE53IA1:yuchuanE53IA1",
        #"C3_YuchuanE53SC1:yuchuanE53SC1",
        #"LinuxCExample:linuxC",
        #"C4_YuchuanE53SC2:YuchuanE53SC2",
        "C5_YuchuanE53IS1:YuchuanE53IS1",
    ]
}

运行结果

示例代码编译烧录代码后,按下开发板的RESET按键,人员靠近开发板,蜂鸣器开始报警。

ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
formatting spiffs...
FileSystem mount ok.
wifi init success!

hiview init success.阻塞等待,触发事件!!!
00 00:00:00 0 132 D 0/HIVIEW: log limit init success.
00 00:00:00 0 132 I 1/SAMGR: Bootstrap core services(count:3).
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0714 TaskPool:0xe4b3c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0720 TaskPool:0xe4b5c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0bec TaskPool:0xe4b7c
00 00:00:00 0 164 I 1/SAMGR: Init service 0x4b0720 <time: 0ms> success!
00 00:00:00 0 64 I 1/SAMGR: Init service 0x4b0714 <time: 0ms> success!
00 00:00:00 0 8 I 1/SAMGR: Init service 0x4b0bec <time: 0ms> success!
00 00:00:00 0 8 I 1/SAMGR: Initialized all core system services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap system and application services(count:0).
00 00:00:00 0 64 I 1/SAMGR: Initialized all system and application services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。