本帖最后由 l_xy 于 2020-11-2 18:47 编辑
提示:此案例为单主板案例,主板不要放到底板上。
按键点亮主板的板载LED灯的步骤如下:
1. 在applicationssamplewifi-iotapp下新建文件夹“key_demo”,在key_demo文件夹下新建BUILD.gn和key_demo.c 两个文件,具体目录结构如下:
.
└── applications
└── sample
└── wifi-iot
└── app
│── key_demo
│ │── BUILD.gn
│ └── key_demo.c
└── BUILD.gn
2. applicationssamplewifi-iotappkey_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"
- ]
- }
复制代码
3. applicationssamplewifi-iotappkey_demo下的key_demo.c代码如下:
- #include
- #include "stdio.h"
- #include "ohos_init.h"
- #include "cmsis_os2.h"
- #include "wifiiot_gpio.h"
- #include "wifiiot_gpio_ex.h"
- #include
- #include
- #include
- #include
- 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灯熄灭。
4. 不要忘修改applicationssamplewifi-iotapp下的BUILD.gn代码
- import("//build/lite/config/component/lite_component.gni")
- lite_component("app") {
- features = [
- "key_demo:key_demo",
- ]
- }
复制代码
注意:此案例为单主板案例,主板不要放到底板上。
文章来自51CTO社区
|