#2020征文-开发板#红绿黄灯板的使用
wifiiot套件的板子很多,今天先来写一篇关于使用红绿灯板的帖子。主要完成红绿灯板,LED、蜂鸣器、按键的控制。
# 一、硬件介绍
通过查看红绿灯板的原理图,红绿灯板主要有红、绿、黄LED灯各一个,蜂鸣器一个,按键一个。
经过查看原理图,它们使用主控的GPIO口分别为:
red --》 GPIO10
green --》 GPIO11
yellow --》 GPIO12
beep --》GPIO9
switch --》GPIO8
# 二、新建文件
之前一直做的都是MCU这方面的开发,习惯了使用IDE,对一些编译工具链都没什么了解。对于怎么在原来的工程目录下,新建文件不是很熟悉。但是这些都不是什么大问题,可以看原来的工程目录结构是怎么样的,按照它的模板进行新建就行了。
好像就是一个文件夹下存放.c .h文件和BUILD.gn。
好的,我们就那样来开干。在app目录下新建SSL文件夹,下面再新建c文件和BUILD.gn文件。
## 1、light.c
该C文件任务主要是控制红绿黄灯亮灭。把原来的led例程复制过来进行更改。
新建一个任务,红绿灯一秒亮,一秒灭循环闪烁。
/*
* 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.
*/
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#define LIGHT_INTERVAL_TIME_US 1000000
#define LIGHT_TASK_STACK_SIZE 512
#define LIGHT_TASK_PRIO 26
#define YELLOW_GPIO WIFI_IOT_IO_NAME_GPIO_12
#define GREEN_GPIO WIFI_IOT_IO_NAME_GPIO_11
#define RED_GPIO WIFI_IOT_IO_NAME_GPIO_10
static void *LightTask(const char *arg)
{
(void)arg;
while (1) {
GpioSetOutputVal(YELLOW_GPIO, 1);
GpioSetOutputVal(RED_GPIO,1);
GpioSetOutputVal(GREEN_GPIO,1);
usleep(LIGHT_INTERVAL_TIME_US);
GpioSetOutputVal(YELLOW_GPIO, 0);
GpioSetOutputVal(RED_GPIO,0);
GpioSetOutputVal(GREEN_GPIO,0);
usleep(LIGHT_INTERVAL_TIME_US);
}
return NULL;
}
static void LightTaskEntry(void)
{
osThreadAttr_t attr;
GpioInit();
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 = "LightTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = LIGHT_TASK_STACK_SIZE;
attr.priority = LIGHT_TASK_PRIO;
if (osThreadNew((osThreadFunc_t)LightTask, NULL, &attr) == NULL) {
printf("[LightTask] Falied to create LightTask!\n");
}
}
SYS_RUN(LightTaskEntry);
2、beep.c
该C文件任务主要任务是通过按下按键,控制蜂鸣器发声。
新建任务,按下按键,蜂鸣器发声,松开按键,蜂鸣器停止发声。
这里要注意一下,按键的引脚信号为输入信号,初始化的时候,需要先进行上拉模式设置,要不然会一直检测到低电平。
/*
* 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.
*/
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#define BEEP_INTERVAL_TIME_US 1000000
#define BEEP_TASK_STACK_SIZE 512
#define BEEP_TASK_PRIO 27
#define BEEP_GPIO WIFI_IOT_IO_NAME_GPIO_9
#define BUTTON_GPIO WIFI_IOT_IO_NAME_GPIO_8
#define BUTTON_CHECK_DELAY_US 5000
static void *BeepTask(const char *arg)
{
(void)arg;
while (1) {
WifiIotGpioValue buttonValue;
GpioGetInputVal(BUTTON_GPIO ,&buttonValue);
if(buttonValue == WIFI_IOT_GPIO_VALUE0){
GpioSetOutputVal(BEEP_GPIO, 1);
usleep(BUTTON_CHECK_DELAY_US);
GpioSetOutputVal(BEEP_GPIO, 0);
usleep(BUTTON_CHECK_DELAY_US);
}
usleep(BUTTON_CHECK_DELAY_US);
}
return NULL;
}
static void BeepTaskEntry(void)
{
osThreadAttr_t attr;
GpioInit();
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_FUNC_GPIO_8_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_GPIO_DIR_IN);
IoSetPull(WIFI_IOT_IO_NAME_GPIO_8,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_OUT);
attr.name = "BeepTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = BEEP_TASK_STACK_SIZE;
attr.priority = BEEP_TASK_PRIO;
if (osThreadNew((osThreadFunc_t)BeepTask, NULL, &attr) == NULL) {
printf("[BeepTask] Falied to create BeepTask!\n");
}
}
SYS_RUN(BeepTaskEntry);
3、BUILD.gn
对于我这种只习惯于IDE开发,对这类文件都不熟悉,但是这些都些东西都是有规律的,看其他的文件夹下的BUILD.gn是怎么编写的,然后复制更改。
# 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.
static_library("ssl_example") {
sources = [
"light.c",
"beep.c"
]
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/components/cmsis/2.0",
"//base/iot_hardware/inteRFaces/kits/wifiiot_lite",
]
}
这样还不行,还要修改app文件夹下的BUILD.gn。
# 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.
import("//build/lite/config/component/lite_component.gni")
lite_component("app") {
features = [
"startup",
"SSL:ssl_example",
]
}
三、编译下载
下载验证成功。