参考连老师的Hi3861 GPIO操作 点灯和按键实验:https://harmonyos.51cto.com/p…
参考唐老师的10月28日直播课

提示:此案例为单主板案例,主板不要放到底板上。

按键点亮主板的板载LED灯的步骤如下:

  1. 在\applications\sample\wifi-iot\app下新建文件夹“key_demo”,在key_demo文件夹下新建BUILD.gn和key_demo.c 两个文件,具体目录结构如下:

.

└── applications

    └── sample

        └── wifi-iot

            └── app

                │── key_demo

                │  │── BUILD.gn

                │  └── key_demo.c 

                └── BUILD.gn
  1. \applications\sample\wifi-iot\app\key_demo下的BUILD.gn代码如下:
static_library("key_demo") {
    sources = [
        "key_demo.c"
    ]


    include_dirs = [
        "//utils/native/lite/include",
        "//kernel/liteos_m/components/cmsis/2.0",
        "//base/iot_hardware/interfaces/kits/wifiiot_lite"
    ]
}
  1. \applications\sample\wifi-iot\app\key_demo下的key_demo.c代码如下:
#include <unistd.h>
#include "stdio.h"
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include <hi_types_base.h>
#include <hi_i2c.h>
#include <hi_early_debug.h>
#include <hi_stdlib.h>

static int volatile BtnPressed = 0;
static void OnButtonPressed(char* arg);
static void OnButtonReleased(char* arg);

static void OnButtonPressed(char* arg)
{
    (void)arg;
    printf("[Key_demo] OnButtonPressed()\n");
    BtnPressed = 1;
    GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_5,WIFI_IOT_INT_TYPE_EDGE,WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH,OnButtonReleased, NULL);
}

static void OnButtonReleased(char* arg)
{
    (void)arg;
    printf("[Key_demo] OnButtonReleased()\n");
    BtnPressed = 0;
    GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_5,WIFI_IOT_INT_TYPE_EDGE,WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,OnButtonPressed, NULL);
}

static void* Key_Task(const char* arg)
{
    (void)arg;
    while(1) 
    {
        if( BtnPressed ) 
        {
            GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, 0);
        }
        else
        {
            GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_9, 1);
        }           
    }
    return NULL;
}


static void key_demo(void)
{
    osThreadAttr_t attr = {0};
    GpioInit();
    //复用引脚为 GPIO
    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_OUT);
    //复用引脚为 GPIO
    IoSetFunc(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_IO_FUNC_GPIO_5_GPIO);
    //设置为输入
    GpioSetDir(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_GPIO_DIR_IN);
    IoSetPull(WIFI_IOT_IO_NAME_GPIO_5, WIFI_IOT_IO_PULL_UP);
    GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_5,WIFI_IOT_INT_TYPE_EDGE,WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW,OnButtonPressed, NULL);
    attr.name = "Key_Task";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 1024;
    attr.priority = osPriorityNormal;

    if (osThreadNew((osThreadFunc_t)Key_Task, NULL, &attr) == NULL)
    {
        printf("[key_demo] Falied to create KedTask!\n");
    }
}

SYS_RUN(key_demo);

保存,烧录重启后,按下主板上USER按钮点亮LED,释放按钮LED灯熄灭。

  1. 不要忘修改\applications\sample\wifi-iot\app下的BUILD.gn代码
import("//build/lite/config/component/lite_component.gni")

lite_component("app") {
    features = [
        "key_demo:key_demo",
    ]
}

注意:此案例为单主板案例,主板不要放到底板上。

鸿蒙HiSpark Wi-Fi IoT开发套件试用笔记 目录


作者:FFKJ_Net

想了解更多内容,请访问:
51CTO和华为官方战略合作共建的鸿蒙技术社区
https://harmonyos.51cto.com#jssq

发表评论

邮箱地址不会被公开。 必填项已用*标注